如何告诉Spring Boot哪个主类用于可执行jar?

Thi*_*ilo 168 java spring executable-jar maven spring-boot

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

我的项目有多个带有main方法的类.我如何告诉Spring Boot Maven插件它应该用作哪个类作为主类?

lud*_*_rj 250

在你的pom中添加你的起始类:

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您使用spring-boot-starter-parent pom,则此答案是正确的.在这种情况下,"start-class"属性应用于spring-boot-maven-plugin的"mainClass"配置参数(如果不使用启动器,可以直接执行). (29认同)
  • 谢谢@ludo_rj,我发现这也有效:`mvn clean package -Dstart-class = com.foo.Application`,如果想动态指定使用哪个主类 (16认同)
  • 还要补充一点,@zhuguowei 提到的参数也适用于 Spring Boot Maven 插件:`mvn spring-boot:run -Dstart-class=com.foo.Application`。仅当您未在 pom 的插件中指定 mainClass 时才有效 (4认同)

小智 125

对于那些使用Gradle(而不是Maven)的人:

springBoot {
    mainClass = "com.example.Main"
}
Run Code Online (Sandbox Code Playgroud)

  • Spring Boot 2.x给出了一个错误`无法为org.springframework.boot.gradle.dsl.SpringBootExtension`类型的对象设置未知属性'mainClass'. (2认同)

Ren*_*aud 68

如果你不使用spring-boot-starter-parent pom,那么从Spring文档:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.1.3.RELEASE</version>
    <configuration>
        <mainClass>my.package.MyStartClass</mainClass>
        <layout>ZIP</layout>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)


Sha*_* Lu 17

对于那些使用Gradle(而不是Maven)的人,请在此处引用:

也可以使用任务的mainClassName属性显式配置主类:

bootJar {
    mainClassName = 'com.example.ExampleApplication'
}
Run Code Online (Sandbox Code Playgroud)

或者,可以使用Spring Boot DSL的mainClassName属性在项目范围内配置主类名:

springBoot {
    mainClassName = 'com.example.ExampleApplication'
}
Run Code Online (Sandbox Code Playgroud)


moj*_*ave 12

如果你在你的pom中使用spring-boot-starter-parent,你只需将以下内容添加到你的pom中:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

然后做你的mvn包.

请参阅此Spring文档页面.

这里一个非常重要的方面是提到目录结构必须是src/main/java/nameofyourpackage


小智 6

我在 pom.xml 中尝试了以下代码,它对我有用

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


tan*_*an9 5

从 Spring Boot 1.5 开始,你可以完全忽略 pom 或 build.gradle 中容易出错的字符串文字。重新打包工具(通过 maven 或 gradle 插件)会@SpringBootApplication为你选择一个带注释的。(详细参考本期:https : //github.com/spring-projects/spring-boot/issues/6496