SpringBoot:无法从以下候选人中找到单个主要 class

SpringBoot: Unable to find a single main class from the following candidates

我使用 Spring 初始化程序生成了一个 Spring 启动 Web 应用程序,嵌入 Tomcat,使用 Thymeleaf 模板 engine.Technologies: Spring 引导 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6、Maven 3、Java8

我有一个 Spring启动应用程序。这些 2 类:

@Profile("!war")
@SpringBootApplication
@Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class BookApplication {

    public static void main(String[] args) {
        SpringApplication.run(BookApplication.class, args);
    }
}

@Profile("war")
@Import({SecurityConfig.class ,PersistenceConfig.class})
@SpringBootApplication
public class BookApplicationWar extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BookApplicationWar.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(BookApplicationWar.class, args);
    }

}

我用这个命令生成 war

 mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPP-KEY=pebloc

但是我得到了这个错误:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project book: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.tdk.BookApplication, com.tdk.BookApplicationWar] -> [Help 1]

如果您有多个主 class,您需要在每个配置文件中明确 configure the main class

<profiles>
    <profile>
        <id>profile1</id>
        <properties>
          <spring.boot.mainclass>com.SomeClass</spring.boot.mainclass>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
          <spring.boot.mainclass>com.SomeOtherClass</spring.boot.mainclass>
        </properties>
    </profile>
</profiles>

...

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.2.RELEASE</version>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
        <configuration>
          <mainClass>${spring.boot.mainclass}</mainClass>
        </configuration>
      </execution>
    </executions>
    ...
</plugin>

spring-boot:repackage

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project book: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.tdk.BookApplication, com.tdk.BookApplicationWar] 

出现以下错误的一个可能原因是您的代码中有多个主要方法,Spring boot 只允许在同一项目中的所有 class 中使用一个主要方法。

有时在执行 mvn install 而未执行 mvn clean 时会出现此错误。

或者简单地在插件配置中硬编码主要 class。

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>your.main.class.MainClass</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>

如果您有多个 class 具有 public static void main(String[] args),请检查您的应用程序。删除不需要的那个。特别是如果您使用 https://start.spring.io/ 生成项目,它会附带一个带有 main 方法的 class 。

如果您确实需要所有带有 main 方法的 classes,只需在 pom.xml 文件中明确提及哪一个将在应用程序启动时被 boos-trapped。

通过 start-class 定义单个主 class 属性

<properties>
      <start-class>com.may.Application</start-class>
</properties>

或者,在 spring-boot-maven-plugin

中定义主要 class
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.may.Application</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

或通过配置文件

<profiles>
        <profile>
            <id>profile1</id>
            <properties>
                <spring.boot.mainclass>com.may.Application1</spring.boot.mainclass>
            </properties>
        </profile>
        <profile>
            <id>profile2</id>
            <properties>
                <spring.boot.mainclass>com.may.Application2</spring.boot.mainclass>
            </properties>
        </profile>
</profiles>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>${spring.boot.mainclass}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

我有同样的错误。我已将包含 main 的应用程序 class 从 foo.java 重命名为 bar.java,进行了清理和更新。所以我只有一个主菜。

当我删除目标文件夹下旧生成的 class、foo.class 时,错误消失了。

您可以从命令行使用

... -Dstart-class=com.your.package.application.BookApplication

我正在构建一个 spring 引导库项目,所以我不需要主启动程序 class, 所以我在用maven构建项目时遇到了同样的问题,所以我删除了maven插件编译成功

<!--
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
-->

查看更详细的示例。在这里找到 https://spring.io/guides/gs/multi-module/

如果你有一个 Spring 引导应用程序,通常只有一个主要的 class 有 @SpringBootApplication ,如果你在应用程序的某个地方添加另一个 main 方法,gradle 构建过程变得混乱。因此,在 build.gradle 文件

的根级别添加以下内容
apply plugin: 'application'
mainClassName = 'packageNameAfter.srcMainJavaFolder.YourClassName'

要消除 spring 引导应用程序入口点的歧义,您可以将以下指令添加到 Gradle 构建文件

springBoot {
    mainClassName = 'your.org.bla.TestKt'
}
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>your.main.class.MainClass</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Elementconfiguration必须在plugin里面一个!

您不需要使用配置文件来避免错误。您可以像这样配置 spring-boot-maven-plugin:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.6.3</version> <!-- use the corresponding version of your spring boot -->
        <executions>
            <execution>
                <id>repackage-application1</id>
                <phase>package</phase>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <mainClass>com.yourpackage1.Application1</mainClass>
                    <finalName>app1</finalName>
                    <outputDirectory>target/apps</outputDirectory>
                </configuration>
            </execution>
            <execution>
                <id>repackage-application2</id>
                <phase>package</phase>
                <goals>
                    <goal>repackage</goal>
                </goals>
                <configuration>
                    <mainClass>com.yourpackage2.Application2</mainClass>
                    <finalName>app2</finalName>
                    <outputDirectory>target/apps</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>

这将在您的目标文件夹中创建 app1.jar 和 app2.jar。

重要说明:如果您继承自spring-boot-starter-parent,它默认执行spring-boot-maven-plugin,默认配置只需要一个spring 应用。如果您在一个模块中有两个应用程序,则该默认配置将失败并出现错误“无法找到单个主 class”错误。要修复您的构建,您需要停用父 pom 中的默认插件执行。一种方法是在父 pom 中排除一个插件,你可以看看 maven exclude plugin defined in parent pom(我自己没试过)