【问题标题】:How do I tell Spring Boot which main class to use for the executable jar?如何告诉 Spring Boot 哪个主类用于可执行 jar?
【发布时间】:2014-06-06 16:11:58
【问题描述】:
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

我的项目有不止一个类具有main 方法。如何告诉 Spring Boot Maven 插件它应该使用哪个类作为主类?

【问题讨论】:

  • java -cp myjar.jar MyClass
  • @Evgeni:这是一个运行时标志。它没有那么远。构建失败。

标签: java spring maven executable-jar spring-boot


【解决方案1】:

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

<properties>
    <!-- The main class to start by executing java -jar -->
    <start-class>com.mycorp.starter.HelloWorldApplication</start-class>
</properties>

<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>             
        <configuration>    
            <mainClass>com.mycorp.starter.HelloWorldApplication</mainClass>
        </configuration>
    </plugin>
</plugins>
</build>

【讨论】:

  • 无需禁用任何东西。还是我错过了什么?
  • 请注意,如果您使用 spring-boot-starter-parent pom,则此答案是正确的。在这种情况下,“start-class”属性将应用于 spring-boot-maven-plugin 的“mainClass”配置参数(如果您不使用启动器,您可以直接执行此操作)。
  • 感谢@ludo_rj,我发现这也有效:mvn clean package -Dstart-class=com.foo.Application,如果想动态指定使用哪个主类
  • 还要补充一点,@zhguuowei 提到的参数同样适用于 Spring Boot Maven 插件:mvn spring-boot:run -Dstart-class=com.foo.Application。仅当您没有在 pom 的插件中指定 mainClass 时才有效
  • 两者都不适合我。我还认为这是一个“与”而不是一个或?我在 MANIFEST.MF 中正确地看到了 Start-Class:,但是 spring 启动了一个不同的带注释的 @SpringBootApplication 主类。我实际上需要那个类来引导一些东西,所以我不太喜欢更改注释。简单地删除它无论如何都不起作用。 Spring 似乎启动了它找到的第一个 main() 。我正在使用 spring-boot-starter-parent 2.2.0.M3。
【解决方案2】:

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

springBoot {
    mainClass = "com.example.Main"
}

【讨论】:

  • Spring Boot 2.x 给出错误Could not set unknown property 'mainClass' for object of type org.springframework.boot.gradle.dsl.SpringBootExtension
  • 答案在本页下方:stackoverflow.com/a/49716696/75672
【解决方案3】:

如果你不使用 spring-boot-starter-parent pom,那么来自Spring documentation:

<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>

【讨论】:

  • 主类配置可以省略
  • @zhaozhi 你能解释一下为什么/如何吗?
【解决方案4】:

对于那些使用 Gradle(而不是 Maven)的人,请参考 here

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

bootJar {
    mainClass = 'com.example.ExampleApplication'
}

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

springBoot {
    mainClass = 'com.example.ExampleApplication'
}

【讨论】:

    【解决方案5】:

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

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

    然后做你的 mvn 包。

    this Spring docs page

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

    【讨论】:

    • 复制 .java 类的包要求后,我发现此解决方案无需修改 pom.xml 即可工作。
    【解决方案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>
    

    【讨论】:

    • 我尝试使用您在我的 multimodules maven 项目中提到的 spring-boot-maven-plugin 配置,该配置由多个 Spring Boot 项目组成,并包括 Spring Boot 作为 BOM 依赖项,它就像一个魅力。关于 maven-compiler-plugin,我没有指定任何内容,因为我不希望我的 POM 平台依赖。 Maven自动fork,所以我觉得你可以忽略这个配置。
    【解决方案7】:

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

    【讨论】:

      【解决方案8】:

      我已重命名我的项目,但它仍在构建路径上找到旧的 Application 类。我在“build”文件夹中删除了它,一切都很好。

      【讨论】:

        【解决方案9】:

        在 Java 1.9 和 SpringBoot 1.5.x 未明确指定 main-class 时,已看到此问题。

        使用 Java 1.8,它能够找到没有显式属性的主类,并且“mvn 包”工作正常。

        【讨论】:

          【解决方案10】:

          如果您使用 Grade,则可以应用 'application' plugin 而不是“java”插件。这允许在不使用任何 Spring Boot Gradle 插件任务的情况下指定主类,如下所示。

          plugins {
            id 'org.springframework.boot' version '2.3.3.RELEASE'
            id 'io.spring.dependency-management' version '1.0.10.RELEASE'
            id 'application'
          }
          application {
            mainClassName = 'com.example.ExampleApplication'
          }
          

          作为一个很好的好处,可以使用 gradle run 运行应用程序,并使用 Gradle 自动配置的类路径。该插件还将应用程序打包为 TAR 和/或 ZIP,包括操作系统特定的启动脚本。

          【讨论】:

            猜你喜欢
            • 2019-05-04
            • 1970-01-01
            • 2016-07-10
            • 2018-05-26
            • 2018-04-06
            • 2014-12-18
            • 1970-01-01
            • 2017-02-04
            相关资源
            最近更新 更多