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

我的项目有一个主方法的多个类。我如何告诉Spring Boot Maven插件它应该使用哪个类作为主类?


当前回答

对于那些使用Gradle(而不是Maven)的人,请参考Spring Boot Gradle插件参考指南(从Gradle 7.6和Spring Boot 3.0.0开始):

主类也可以使用任务的mainClass属性显式配置: tasks.named (bootJar) { mainClass = 'com.example.ExampleApplication' } 或者,主类名可以使用Spring Boot DSL的mainClass属性在项目范围内配置: springBoot { mainClass = 'com.example.ExampleApplication' }

其他回答

对于那些使用Gradle(而不是Maven)的人:

springBoot {
    mainClass = "com.example.Main"
}

如果你不使用Spring -boot-start -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>

Kotlin with Gradle:

springBoot {
  mainClass.set("com.example.MainKt")
}

对于那些使用Gradle(而不是Maven)的人,请参考Spring Boot Gradle插件参考指南(从Gradle 7.6和Spring Boot 3.0.0开始):

主类也可以使用任务的mainClass属性显式配置: tasks.named (bootJar) { mainClass = 'com.example.ExampleApplication' } 或者,主类名可以使用Spring Boot DSL的mainClass属性在项目范围内配置: springBoot { mainClass = 'com.example.ExampleApplication' }

从Spring Boot 1.5开始,你可以完全忽略pom或build.gradle中容易出错的字符串文字。重新打包工具(通过maven或gradle插件)将为你选择带有@SpringBootApplication注释的工具。(详见此问题:https://github.com/spring-projects/spring-boot/issues/6496)