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

为什么配置maven-assembly-plugin后执行mvn clean package无法生成fat jar

Maven执行mvn clean package无法生成带依赖fat jar的解决方案

问题根因

  • 配置结构错误:现有pom中<properties>块错误嵌套在了<dependencies>标签内部,属于语法不规范
  • 插件未生效:maven-assembly-plugin被放在了<pluginManagement>标签下,该标签仅用于多模块项目下统一管理插件的版本、配置规则,不会直接触发插件执行,所以打包阶段没有运行打fat jar的逻辑

修正后的完整pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xx.yy.zz</groupId>
  <artifactId>management-service</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>management-service</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.parameters>true</maven.compiler.parameters>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  
  <build>
      <pluginManagement>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.8.1</version>
              </plugin>
          </plugins>
      </pluginManagement>
      <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id> 
                <phase>package</phase> 
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
      </plugins>
  </build>
</project>

验证方法

执行mvn clean package命令后,target目录会生成两个jar包:

  • 普通jar包:management-service-1.0-SNAPSHOT.jar(无依赖)
  • fat jar包:management-service-1.0-SNAPSHOT-jar-with-dependencies.jar(包含全部依赖,可直接运行)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 11:36:02