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

我的項目有不止一個 class 和一個main方法。 我如何告訴 Spring 引導 Maven 插件應該使用哪個類作為主要 class?

在你的 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>

對於那些使用 Gradle(而不是 Maven)的人:

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

如果您不使用 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>

對於那些使用 Gradle(而不是 Maven)的人,參考這里

也可以使用任務的 mainClassName 屬性顯式配置主類:

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

或者,可以使用 Spring Boot DSL 的 mainClassName 屬性在項目范圍內配置主類名:

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

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

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

然后做你的 mvn 包。

請參閱此 Spring 文檔頁面

這里一個非常重要的方面是提到目錄結構必須是 src/main/java/nameofyourpackage

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

從 Spring Boot 1.5 開始,你可以完全忽略 pom 或 build.gradle 中容易出錯的字符串文字。 重新打包工具(通過 maven 或 gradle 插件)會為你挑選一個用@SpringBootApplication注釋的@SpringBootApplication (詳細參考本期: https : //github.com/spring-projects/spring-boot/issues/6496

我重命名了我的項目,它仍然在構建路徑上找到舊的Application類。 我在“build”文件夾中刪除了它,一切都很好。

當未明確指定 main-class 時,在 Java 1.9 和 SpringBoot 1.5.x 中已經看到了這個問題。

使用 Java 1.8,它可以在沒有顯式屬性的情況下找到主類,並且“mvn 包”工作正常。

如果您使用的是 Grade,則可以應用“application”插件而不是“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,包括操作系統特定的啟動腳本。

對於 Kotlin 和 Gradle:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM