问题
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]
回答1:
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
回答2:
Define single main class via start-class property
<properties>
<start-class>com.may.Application</start-class>
</properties>
Alternatively, define the main class in the 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>
回答3:
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>
回答4:
Sometimes this error is given when you do mvn install without doing mvn clean.
回答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]
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.
回答6:
From the command line you can use
... -Dstart-class=com.your.package.application.BookApplication
回答7:
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.
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.
回答8:
I had the same error. I had renamed my Application class containing main from foo.java to bar.java, cleaned and updated. So I only ever had one main.
The error went away when I deleted the old generated class, foo.class under the target folder.
来源:https://stackoverflow.com/questions/42840576/springboot-unable-to-find-a-single-main-class-from-the-following-candidates