简体   繁体   English

我如何告诉 Spring 引导哪个主要 class 用于可执行文件 jar?

[英]How do I tell Spring Boot which main class to use for the executable jar?

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.我的项目有不止一个 class 和一个main方法。 How do I tell the Spring Boot Maven plugin which of the classes it should use as the main class?我如何告诉 Spring 引导 Maven 插件应该使用哪个类作为主要 class?

Add your start class in your pom:在你的 pom 中添加你的起始类:

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

or或者

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

For those using Gradle (instead of Maven) :对于那些使用 Gradle(而不是 Maven)的人:

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

If you do NOT use the spring-boot-starter-parent pom, then from the Spring documentation :如果您不使用 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>

For those using Gradle (instead of Maven), referencing here :对于那些使用 Gradle(而不是 Maven)的人,参考这里

The main class can also be configured explicitly using the task's mainClassName property:也可以使用任务的 mainClassName 属性显式配置主类:

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

Alternatively, the main class name can be configured project-wide using the mainClassName property of the Spring Boot DSL:或者,可以使用 Spring Boot DSL 的 mainClassName 属性在项目范围内配置主类名:

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

If you're using spring-boot-starter-parent in your pom, you simply add the following to your pom:如果您在 pom 中使用 spring-boot-starter-parent,只需将以下内容添加到 pom 中:

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

Then do your mvn package.然后做你的 mvn 包。

See this Spring docs page .请参阅此 Spring 文档页面

A very important aspect here is to mention that the directory structure has to be src/main/java/nameofyourpackage这里一个非常重要的方面是提到目录结构必须是 src/main/java/nameofyourpackage

I tried the following code in pom.xml and it worked for me我在 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>

Since Spring Boot 1.5, you can complete ignore the error-prone string literal in pom or build.gradle.从 Spring Boot 1.5 开始,你可以完全忽略 pom 或 build.gradle 中容易出错的字符串文字。 The repackaging tool (via maven or gradle plugin) will pick the one annotated with @SpringBootApplication for you.重新打包工具(通过 maven 或 gradle 插件)会为你挑选一个用@SpringBootApplication注释的@SpringBootApplication (Refer to this issue for detail: https://github.com/spring-projects/spring-boot/issues/6496 ) (详细参考本期: https : //github.com/spring-projects/spring-boot/issues/6496

I had renamed my project and it was still finding the old Application class on the build path.我重命名了我的项目,它仍然在构建路径上找到旧的Application类。 I removed it in the 'build' folder and all was fine.我在“build”文件夹中删除了它,一切都很好。

Have seen this issue with Java 1.9 and SpringBoot 1.5.x, when main-class is not specified explicitly.当未明确指定 main-class 时,在 Java 1.9 和 SpringBoot 1.5.x 中已经看到了这个问题。

With Java 1.8, it is able to find main-class without explicit property and 'mvn package' works fine.使用 Java 1.8,它可以在没有显式属性的情况下找到主类,并且“mvn 包”工作正常。

If you are using Grade, it is possible to apply the 'application' plugin rather than the 'java' plugin.如果您使用的是 Grade,则可以应用“application”插件而不是“java”插件。 This allows specifying the main class as below without using any Spring Boot Gradle plugin tasks.这允许在不使用任何 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'
}

As a nice benefit, one is able to run the application using gradle run with the classpath automatically configured by Gradle.一个不错的好处是,可以使用gradle run运行应用程序,并使用 Gradle 自动配置的类路径。 The plugin also packages the application as a TAR and/or ZIP including operating system specific start scripts.该插件还将应用程序打包为 TAR 和/或 ZIP,包括操作系统特定的启动脚本。

For Kotlin with Gradle:对于 Kotlin 和 Gradle:

springBoot {
  mainClass.set("com.example.MainKt")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM