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"
}

我已经重命名了我的项目,它仍然在构建路径上找到旧的Application类。我在“构建”文件夹中删除了它,一切正常。

在pom中添加你的起始类:

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>

or

<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>

我在Java 1.9和SpringBoot 1.5中见过这个问题。X,当main-class未显式指定时。

使用Java 1.8,它能够找到没有显式属性的main-class,并且'mvn package'工作得很好。

我在pom.xml中尝试了以下代码,它对我有效

<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>