Maven Shade Plugin 插件使用说明
1 Maven Shade Plugin 地址
https://maven.apache.org/plugins/maven-shade-plugin/index.html
2 Maven Shade Plugin 特点
- maven-shade-plugin ,它可以在构建过程中创建 一个包含所有依赖项的Jar文件。
- 能够对某些依赖项的包进行遮蔽处理——即对其进行重命名。
- 可打包可执行的jar包(配置Main Class)和不可执行的jar包(不配置 Main Class)
3 maven-shade-plugin 的用法
3.1 maven-shade-plugin 的基本用法
3.1.1 不配置 Main Class,打包成不可执行的jar包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
不可执行的 JAR 文件主要用作 Java 类库(Library JAR),它不包含可以直接运行的主类。这样的 JAR 文件通常被引入到其他
Java 项目中作为依赖项使用
3.1.1 配置 Main Class,打包可执行的jar包
官方原文地址: https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html
在configuration->transformers->transformer->mainClass 节点中设置主类,使生成的 JAR 文件可以直接通过 java -jar 命令运行,
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.sonatype.haven.HavenCli</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
可执行的 JAR 文件包含一个可以直接运行的 Java 应用程序。要使 JAR 文件可执行,它必须包含一个 Main-Class
属性,该属性指定要运行的主类(main class)
3.2 选择、排除依赖Jar包的文件
官网原文:https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
下面的 POM 代码片段展示了如何控制哪些项目依赖项应被包含/排除在超集 JAR 文件中:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.3 修改Class文件路径
使用 relocations 可以将某些类的路径修改,以防止不同依赖中的类冲突:
官网地址:https://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.codehaus.plexus.util</pattern>
<shadedPattern>org.shaded.plexus.util</shadedPattern>
<excludes>
<exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
<exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
</excludes>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这会指示插件将位于“org.codehaus.plexus.util”及其子包中的类移动到“org.shaded.plexus.util”这个包中。而类“Xpp3Dom”以及pull.*相关文件保留在其原始的包中。
还可以通过使用“include”标签来缩小该模式的范围:
<project>
...
<relocation>
<pattern>org.codehaus.plexus.util</pattern>
<shadedPattern>org.shaded.plexus.util</shadedPattern>
<includes>
<include>org.codehaud.plexus.util.io.*</include>
</includes>
</relocation>
...
</project>
4 maven-shade-plugin 会自动创建 dependency-reduced-pom.xml
在使用 Maven 的 maven-shade-plugin 插件时,确实会生成一个名为 dependency-reduced-pom.xml 的文件。这个文件是 Maven 项目的一个特殊版本,主要用于解决依赖冲突和简化最终的依赖结构。
4.1 为什么需要 dependency-reduced-pom.xml
依赖冲突解决:在大型项目中,可能会有多个库包含相同版本的依赖,但由于不同的作用域(compile, provided, runtime 等)或不同的版本,这些依赖可能会冲突。maven-shade-plugin 在创建 uber-jar 时,会尝试解决这些冲突,并生成一个简化后的 POM 文件,该文件只包含实际需要的依赖版本。
简化依赖结构:生成的 dependency-reduced-pom.xml 文件只包含最终项目中实际使用的依赖项和它们的版本,这对于确保构建的可重复性和清晰性非常有用。
4.1.1 生成
当你运行带有 maven-shade-plugin 的 Maven 构建时,插件会自动生成这个文件,在构建过程中,dependency-reduced-pom.xml 会被创建在 target/ 目录下。
4.1.2 使用
虽然通常不需要手动操作这个文件,但它对于理解项目的最终依赖结构非常有帮助。你可以在需要时查看这个文件来了解项目中实际包含哪些库和它们的版本。例如,如果你需要将项目部署到不支持完整 Maven 仓库的环境中,这个文件可以帮助你手动管理依赖。
注意事项
- 不要修改:通常不建议手动修改 dependency-reduced-pom.xml,因为它代表了构建过程中的一个中间产物。任何修改都可能影响构建的一致性和可重复性。
- 版本控制:如果你需要保留这个文件以便将来参考,可以考虑将其添加到项目的版本控制中,尽管这通常是可选的。
通过使用 maven-shade-plugin 和其生成的 dependency-reduced-pom.xml,你可以有效地管理和分发包含所有必需依赖的单一 JAR 文件,这对于部署和分发应用程序非常有用。
4.2 配置不生成
在 maven-shade-plugin所在的 标签下(或者 plugin-executions-execution 标签狭隘)添加
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
5682

被折叠的 条评论
为什么被折叠?



