Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage
failed:
Unable to find a single main class from the following candidates
我的项目有多个带有 main
方法的类。我该如何告诉Spring Boot Maven插件应该使用哪个类作为主类?
Execution default of goal
org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage
failed:
Unable to find a single main class from the following candidates
我的项目有多个带有 main
方法的类。我该如何告诉Spring Boot Maven插件应该使用哪个类作为主类?
在 pom 文件中添加起始类:
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
或者<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
mvn clean package -Dstart-class=com.foo.Application
,如果想要动态指定使用哪个主类。 - zhuguoweimvn spring-boot:run -Dstart-class=com.foo.Application
。仅当您没有在pom的插件中指定mainClass时才有效。 - Gerardo Roza对于那些使用 Gradle (而不是 Maven)的人:
springBoot {
mainClass = "com.example.Main"
}
Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension
。 - Thunderforge如果您不使用spring-boot-starter-parent pom,则从Spring文档中可以得知:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.3.RELEASE</version>
<configuration>
<mainClass>my.package.MyStartClass</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
对于使用Gradle(而不是Maven)的人,参考Spring Boot Gradle Plugin参考指南(截至Gradle 7.6和Spring Boot 3.0.0):
The main class can also be configured explicitly using the task’s
mainClass
property:
tasks.named("bootJar") { mainClass = 'com.example.ExampleApplication' }
Alternatively, the main class name can be configured project-wide using the
mainClass
property of the Spring Boot DSL:
springBoot { mainClass = 'com.example.ExampleApplication' }
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后执行 mvn package 命令。
参见这个Spring文档页面。
非常重要的一点是需要确保目录结构为src/main/java/你的包名称。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>myPackage.HelloWorld</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javaw.exe</executable>
</configuration>
</plugin>
</plugins>
从Spring Boot 1.5开始,您可以完全忽略pom或build.gradle中容易出错的字符串字面值。重新打包工具(通过maven或gradle插件)将为您挑选一个使用@SpringBootApplication
注释的版本。(有关详细信息,请参见此问题:https://github.com/spring-projects/spring-boot/issues/6496)
我曾经重命名了我的项目,但它仍然在构建路径中找到旧的Application
类。我在“build”文件夹中将其删除后,问题得到解决。
对于使用Gradle的Kotlin:
springBoot {
mainClass.set("com.example.MainKt")
}
在Java 1.8中,即使没有显式的属性,它也能找到主类(main-class),'mvn package'也可以正常工作。