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

Nuñ*_*ada 23 spring spring-mvc maven spring-profiles spring-boot

我使用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

我有一个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);
    }

}
Run Code Online (Sandbox Code Playgroud)

我用这个命令生成战争

 mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPP-KEY=pebloc
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

[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]
Run Code Online (Sandbox Code Playgroud)

Sea*_*oyd 35

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

<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>
Run Code Online (Sandbox Code Playgroud)

...

<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>
Run Code Online (Sandbox Code Playgroud)

请参阅spring-boot:repackage


She*_*hid 10

或者只是在插件配置中对主类进行硬编码.

        <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>
Run Code Online (Sandbox Code Playgroud)


Eug*_*yuk 10

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

<properties>
      <start-class>com.may.Application</start-class>
</properties>
Run Code Online (Sandbox Code Playgroud)

或者,在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>
Run Code Online (Sandbox Code Playgroud)

或通过个人资料

<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>
Run Code Online (Sandbox Code Playgroud)

  • 一处房产!很好的解决方案! (2认同)

Mat*_*ano 10

从命令行您可以使用

... -Dstart-class=com.your.package.application.BookApplication
Run Code Online (Sandbox Code Playgroud)


Hol*_*ndl 8

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

springBoot {
    mainClassName = 'your.org.bla.TestKt'
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢指点。似乎这已更改为仅“mainClass”。请参阅文档:https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable.configuring.main-class (6认同)

yıl*_*maz 6

有时,在不进行mvn clean的情况下进行mvn安装时会出现此错误。

  • Diddo 用于 Gradle。当项目已经构建一次时更改包名称时可能会发生这种情况。在这种情况下,清洁会产生奇迹。 (2认同)

vaq*_*han 5

[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] 
Run Code Online (Sandbox Code Playgroud)

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