You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Maven Shade或Assembly插件打包可执行Jar报无主清单属性错误

问题根源

你配置的两个打包插件均放在<pluginManagement>标签下,该标签仅用于统一定义插件版本、配置规则供子项目继承,本身不会触发插件执行,因此打包过程没有生成带主类声明的MANIFEST.MF文件,导致运行时报错。

修正方案

1. Maven Shade 插件修正

将maven-shade-plugin的完整配置从<pluginManagement>中移出,放到<build>下直接的<plugins>标签中,<pluginManagement>内仅保留插件版本声明即可,正确配置示例如下:

<build>
  <outputDirectory>${basedir}/target/classes</outputDirectory>
  <!-- 插件执行配置,放到pluginManagement外 -->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.moga.planning.problem.CapacityPlanningApp</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>

  <pluginManagement>
    <plugins>
      <!-- pluginManagement内仅保留版本声明即可,也可保留完整配置,执行配置必须在外层plugins -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
      </plugin>
      <!-- 其余原有插件的版本声明保留即可 -->
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <!-- 其余原有pluginManagement内的配置保持不变 -->
    </plugins>
  </pluginManagement>
</build>

2. Maven Assembly 插件修正

和Shade插件逻辑一致,将maven-assembly-plugin的执行配置放到<build>下直接的<plugins>标签中即可:

<build>
  <outputDirectory>${basedir}/target/classes</outputDirectory>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <archive>
              <manifest>
                <mainClass>com.moga.planning.problem.CapacityPlanningApp</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>

  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
      </plugin>
      <!-- 其余原有pluginManagement内的配置保持不变 -->
    </plugins>
  </pluginManagement>
</build>

验证步骤

  1. 执行mvn clean package重新打包
  2. 运行时选择正确的jar包:Shade方案生成的jar包和原项目名称一致,Assembly方案生成的jar包后缀为jar-with-dependencies,不要运行未带依赖的原始jar包
  3. 可手动解压jar包,查看META-INF/MANIFEST.MF文件,确认存在Main-Class: com.moga.planning.problem.CapacityPlanningApp配置,且该主类确实存在public static void main(String[] args)方法

内容的提问来源于stack exchange,提问作者DTK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 12:45:03