spring SpringBoot:无法从以下候选中找到单个主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42840576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:23:00  来源:igfitidea点击:

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

springmavenspring-mvcspring-bootspring-profiles

提问by Nu?ito de la Calzada

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine.Technologies used: Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8

我使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序。使用的技术:Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat Embed 8.5.6 , Maven 3, Java 8

I have an SpringBoot app. with these 2 classes:

我有一个 SpringBoot 应用程序。有这两个类:

@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);
    }

}

I generate the war with this command

我用这个命令生成War

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

But I got this error:

但我收到了这个错误:

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

回答by Sean Patrick Floyd

If you have more than one main class, you need to explicitly configure the main classin each profile:

如果您有多个主类,则需要在每个配置文件中显式配置主类

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

See spring-boot:repackage

参见spring-boot:repackage

回答by Eugene Maysyuk

Define single main class via start-class property

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

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

Alternatively, define the main class in the spring-boot-maven-plugin

或者,在 spring-boot-maven-plugin 中定义主类

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.may.Application</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Or via profiles

或通过个人资料

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

回答by Sheikh Abdul Wahid

or simply hard code the main class in plugin configuration.

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

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

回答by y?lmaz

Sometimes this error is given when you do mvn install without doing mvn clean.

有时当您执行 mvn install 而不执行 mvn clean 时会出现此错误。

回答by vaquar khan

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

One possible reason to getting following error is more than one main methods in your code, Spring boot allow only one main method across all class in same project.

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

回答by sapy

If you have a Spring Boot Application generally there is only one main class that has @SpringBootApplication, If you add another mainmethod in the application some where , the gradle build process gets confused . So add the following at the root level of your build.gradlefile

如果你有一个 Spring Boot 应用程序,通常只有一个主类@SpringBootApplication,如果你main在应用程序的某个地方添加另一个方法,gradle 构建过程就会变得混乱。因此,在build.gradle文件的根级别添加以下内容

apply plugin: 'application'
mainClassName = 'packageNameAfter.srcMainJavaFolder.YourClassName'

回答by Matteo Gaggiano

From the command line you can use

您可以从命令行使用

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

回答by Hany Sakr

I was building a spring boot library project, so I don't need a main starter class, so I faced the same issue while building the project with maven, so I removed the maven plugin to compile successfully

我在构建一个spring boot库项目,所以不需要一个主要的starter类,所以在用maven构建项目时遇到了同样的问题,所以我去掉了maven插件编译成功

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

For a more detailed example. find it here https://spring.io/guides/gs/multi-module/

一个更详细的例子。在这里找到它 https://spring.io/guides/gs/multi-module/

回答by Holger Brandl

To disambiguate the entry point of a spring boot application, you can add the following directive to your Gradle build file

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

springBoot {
    mainClassName = 'your.org.bla.TestKt'
}

回答by Tadele Ayelegn

Check your application if you have multiple classes with public static void main(String[] args). Remove the one that you do not need. Specially if you generate the project with https://start.spring.io/it comes with a class with main method in it.

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

If you do need all of the classes with main method just explicitly mention which one would to be boos-trapped up on application start up by mentioning it in the pom.

如果您确实需要所有带有 main 方法的类,只需通过在 pom.xml 中提及它来明确地提及在应用程序启动时会被困住的类。