java-maven打包的3种方式(shade、assembly、maven-jar-plugin和maven-dependency-plugin)

本文详细介绍了Java项目使用Maven进行打包的三种常见方式:shade插件、assembly插件和maven-jar-plugin。通过这些插件,可以将依赖库整合、创建可执行jar、定制打包内容,满足不同项目的打包需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用maven-shade-plugin插件打包

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>8</source>

<target>8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>3.2.1</version>

<!-- 不生成新的pom文件-->

<configuration>

<createDependencyReducedPom>false</createDependencyReducedPom>

</configuration>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>shade</goal>

</goals>

<configuration>

<transformers>

<transformer

implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

<mainClass>Run</mainClass>

</transformer>

</transformers>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>



2、使用maven-assembly-plugin插件打包

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>8</source>

<target>8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-assembly-plugin</artifactId>

<version>3.1.1</version>

<configuration>

<archive>

<manifest>

<mainClass>Run</mainClass>

</manifest>

</archive>

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



3、使用maven-jar-plugin和maven-dependency-plugin插件打包

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>8</source>

<target>8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-jar-plugin</artifactId>

<version>3.1.2</version>

<configuration>

<archive>

<manifest>

<addClasspath>true</addClasspath>

<classpathPrefix>lib/</classpathPrefix>

<mainClass>Run</mainClass>

</manifest>

</archive>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<version>3.1.1</version>

<executions>

<execution>

<id>copy-dependencies</id>

<phase>package</phase>

<goals>

<goal>copy-dependencies</goal>

</goals>

<configuration>

<outputDirectory>${project.build.directory}/lib</outputDirectory>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>

### 配置 Maven Shade Plugin 减小生成的 JAR 包大小 #### 使用 `maven-shade-plugin` 实现 JAR 包优化 `maven-shade-plugin` 是一种强大的工具,可用于创建包含所有依赖项的 Uber-JAR 并对其进行优化。以下是通过该插件减小生成 JAR 包大小的关键配置方法: --- #### 1. **移除未使用的类** 利用 `minimizeJar` 参数可以让插件分析项目及其依赖关系,并剔除非必要的类文件。这种方法能显著减少最终 JAR 的体积。 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <minimizeJar>true</minimizeJar> <!-- 移除未使用的类 --> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> ``` 上述配置中启用了 `minimizeJar` 功能[^1],并通过过滤器排除了一些不必要的元数据文件(如签名文件),进一步缩小了 JAR 大小。 --- #### 2. **去除冗余依赖** 在某些情况下,可能存在多余的依赖项或者重复的库版本。这些都可以通过调整 POM 文件中的依赖范围或使用 `dependencyManagement` 来清理。例如,将由运行时环境提供的依赖标记为 `provided` 可有效降低打包后的体积[^2]。 ```xml <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> <!-- 不将其打入JAR--> </dependency> </dependencies> ``` --- #### 3. **解决依赖冲突与重定位类** 当存在多个相同名称但不同版本的类时,可能会引发冲突问题。这时可以借助 `maven-shade-plugin` 提供的 `relocations` 功能重新命名特定包路径下的类,避免潜在的风险[^3]。 ```xml <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.MainApp</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> </transformers> <relocations> <relocation> <pattern>com.mysql.cj</pattern> <shadedPattern>shaded.com.mysql.cj</shadedPattern> </relocation> </relocations> ``` 这里展示了如何对 MySQL 驱动程序的相关类进行重定位操作[^3]。 --- #### 4. **分离依赖至独立目录** 另一种策略是仅将核心业务逻辑放入主 JAR 中,而把其他第三方库复制到单独的子文件夹下。这样既保持了主要模块的小巧轻便,又便于维护分发[^4]。 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> ``` 以上片段说明了如何将外部依赖集中存储在一个名为 `lib` 的子目录里[^4]。 --- #### 5. **结合 Spring Boot 特性** 如果是基于 Spring Boot 构建的应用程序,则还可以考虑采用官方推荐的方式——即保留基础框架部分不变的同时按需定制附加组件[^5]。尽管如此做可能稍微复杂一点,但从长远来看确实有助于控制整体规模增长趋势。 --- ### 总结 综上所述,合理运用 `maven-shade-plugin` 各种特性以及配合其它辅助手段完全可以达到预期目标:生产更加紧凑高效的可执行单元形式!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌翻天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值