知識庫 / Spring / Spring Boot RSS 訂閱

修復 Spring Boot 中“無主 Manifest 屬性”問題

Spring Boot
HongKong
4
11:43 AM · Dec 06 ,2025

1. 概述

當我們在 Spring Boot 可執行 JAR 中遇到 “缺少主清單屬性” 消息時,通常是因為缺少 Main-Class 元數據屬性的聲明,該屬性位於 META-INF 文件夾下的 MANIFEST.MF 文件中。

在本教程中,我們將重點關注導致此問題的根本原因以及如何解決它。

2. 當問題發生時

通常,如果我們的 pom 從 Spring Initializr 中獲取,我們就不太會遇到問題。但是,如果我們手動構建項目,通過將 spring-boot-starter-parent 添加到我們的 pom.xml 文件中,我們可能會遇到此問題。 我們可以通過嘗試清理 jar 包的構建來重現它:

$ mvn clean package

我們會在運行 JAR 文件時遇到錯誤。

$ java -jar target\spring-boot-artifacts-2.jar
no main manifest attribute, in target\spring-boot-artifacts-2.jar

在此示例中,MANIFEST.MF 文件的內容如下:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Baeldung
Build-Jdk: 11.0.13

3. 使用 Maven 插件修復

The Maven plugin provides a convenient way to fix issues in your project. It can automatically resolve many common problems, such as dependency conflicts, version mismatches, and build errors.

Here's how to use the Maven plugin to fix your project:

  1. Identify the Issues: First, you need to identify the specific issues that need to be fixed. The Maven plugin can help you identify these issues by running a build and displaying the error messages.

  2. Run the Maven Plugin: Once you've identified the issues, you can run the Maven plugin to fix them. You can do this by using the following command:

    mvn clean install
    

    This command will clean your project, compile the code, and run the tests. If any issues are found, the Maven plugin will attempt to resolve them automatically.

  3. Verify the Fix: After the Maven plugin has finished running, verify that the issues have been fixed. You can do this by running a build and checking the output for any errors.

  4. Customizing the Maven Plugin: You can customize the Maven plugin by setting properties in your pom.xml file. For example, you can set the maven.compiler.source and maven.compiler.target properties to specify the Java version to use for compiling your code.

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    

    You can also configure the Maven plugin to use different goals and configurations. Refer to the Maven documentation for more information on how to configure the Maven plugin.

3.1. 添加插件

最常見的問題是,我們沒有將 <a title="spring-boot-maven-plugin" href="https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin">spring-boot-maven-plugin</a> 聲明添加到我們的 `pom.xml 文件中。

我們將 <em>plugin </em> 定義 添加到我們的 <em>pom.xml</em> 文件中,在plugins 標籤下聲明 <em>Main-Class</em> 聲明:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>com.baeldung.demo.DemoApplication</mainClass>
            <layout>JAR</layout>
        </configuration>
    </plugin>
</plugins>

然而,這可能不足以解決我們的問題。即使在重新構建並運行我們的 JAR 文件後,我們仍然可能收到 “缺少主可執行屬性” 的消息。

讓我們看看我們有哪些額外的配置和替代方案來解決這個問題。

3.2. Maven 插件執行目標

讓我們在 configuration 標籤之後,將 repackage 目標添加到 spring-boot-maven-plugin 聲明中:

<executions>
    <execution>
        <goals>
            <goal>repackage</goal>
        </goals>
    </execution>
</executions>

3.3 Maven 屬性和內聯命令執行目標

或者,通過將屬性 <em >start-class</em > 添加到 <em >pom.xml</em > 文件的 <em >properties</em >> 標籤中,可以提供更大的靈活性,從而更靈活地控制構建過程。

<properties>
    <start-class>com.baeldung.demo.DemoApplication</start-class>
</properties>

現在我們需要使用 Maven 內聯命令 <em >spring-boot:repackage</em >> 執行目標,來構建 JAR 包:

$ mvn package spring-boot:repackage

4. 檢查 MANIFEST.MF 文件內容

讓我們應用我們的解決方案,構建 jar 包,然後檢查 MANIFEST.MF 文件。

我們會注意到 Main-ClassStart-Class 屬性的存在:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Baeldung
Build-Jdk: 11.0.13
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.baeldung.demo.DemoApplication
Spring-Boot-Version: 2.7.5
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx

現在執行 JAR 包,“缺少主清單屬性” 錯誤不再出現,應用程序已成功運行。

5. 結論

在本文中,我們學習瞭如何解決在執行 Spring Boot 可執行 JAR 時出現的 “缺少主 manifest 屬性” 錯誤。

我們演示了此問題可能來自手動創建的 pom.xml 文件,並展示瞭如何添加和配置 Spring Maven 插件以解決它。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.