Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

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

I have an SpringBoot app. with these 2 classes:

@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

 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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have more than one main class, you need to explicitly configure the main class in 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...