How do I tell Spring Boot which main class to use

2019-01-04 06:25发布

Execution default of goal 
org.springframework.boot:spring-boot-maven-plugin:1.0.1.RELEASE:repackage 
failed: 
Unable to find a single main class from the following candidates

My project has more than one class with a main method. How do I tell the Spring Boot Maven plugin which of the classes it should use as the main class?

9条回答
疯言疯语
2楼-- · 2019-01-04 06:43

I had renamed my project and it was still finding the old Application class on the build path. I removed it in the 'build' folder and all was fine.

查看更多
倾城 Initia
3楼-- · 2019-01-04 06:46

If you're using spring-boot-starter-parent in your pom, you simply add the following to your pom:

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

Then do your mvn package.

See this Spring docs page.

A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage

查看更多
女痞
4楼-- · 2019-01-04 06:47

For those using Gradle (instead of Maven) :

springBoot {
    mainClass = "com.example.Main"
}
查看更多
迷人小祖宗
5楼-- · 2019-01-04 06:52

Since Spring Boot 1.5, you can complete ignore the error-prone string literal in pom or build.gradle. The repackaging tool (via maven or gradle plugin) will pick the one annotated with @SpringBootApplication for you. (Refer to this issue for detail: https://github.com/spring-projects/spring-boot/issues/6496 )

查看更多
闹够了就滚
6楼-- · 2019-01-04 06:55

Add your start class in your pom:

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
查看更多
Bombasti
7楼-- · 2019-01-04 07:00

I tried the following code in pom.xml and it worked for me

<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>myPackage.HelloWorld</mainClass> 
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <fork>true</fork>
            <executable>D:\jdk1.8\bin\javaw.exe</executable>
        </configuration>
    </plugin>
</plugins>

查看更多
登录 后发表回答