maven-shade-plugin使用

官网
https://maven.apache.org/plugins/maven-shade-plugin/

本文基于maven-shade-plugin的3.6.1版本

maven-shade-plugin简介

maven-shade-plugin定义了一个Maven Goal: Shade,在package这个阶段执行

作用:官网就下面这一句话

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

简单来说,uber-jar就是一个包含了所有东西的jar包。参考下面这个SO:https://stackoverflow.com/questions/13620281/what-is-the-maven-shade-plugin-used-for-and-why-would-you-want-to-relocate-java

注意:应避免使用uber-jar作为Maven依赖项,因为它破坏了Maven的依赖解析功能。通常,我们只为实际部署或手动分发的最终工件创建一个超级jar,而不是用于放入Maven存储库。

基本使用

一般配置如下:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.6.1</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <!-- 主要是配置这里的内容 -->
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

由于xml配置比较多,后面只贴 <configuration> 标签的内容

选择放入jar包中的内容

https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html

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

对于所选依赖关系中包含哪些类的更细粒度控制,使用<filter>标签,可以控制每个gav坐标中某些类/资源是否应该包含在shaded的jar中,其中artifact表示单个gav坐标,值的形式是:
groupId:artifactId[[:type]:classifier],支持通配符匹配

<configuration>
    <filters>
        <filter>
            <artifact>junit:junit</artifact>
            <includes>
                <include>junit/framework/**</include>
                <include>org/junit/**</include>
            </includes>
            <excludes>
                <exclude>org/junit/experimental/**</exclude>
                <exclude>org/junit/runners/**</exclude>
            </excludes>
        </filter>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
</configuration>

minimizeJar

除了用户指定的过滤器外,还可以配置为自动删除项目未使用的所有依赖类,从而最大限度地减少由此产生的uber JAR。从1.6版本开始,minimizeJar将遵守过滤规则,也就是说,如果专门标记为包含在过滤器中的类include了,但是没有用到,此时minimizeJar将不会过滤这些类。

<configuration>
  <minimizeJar>true</minimizeJar>
</configuration>

请注意,为artifact中的类指定包含过滤器会隐式排除该工件中所有未指定的类。<excludeDefaults>false<\excludeDefaults>将覆盖此行为,以便仍然包含所有未指定的类。

<configuration>
  <minimizeJar>true</minimizeJar>
  <filters>
    <filter>
       <artifact>log4j:log4j</artifact>
       <includes>
           <include>**</include>
       </includes>
    </filter>
    <filter>
       <artifact>commons-logging:commons-logging</artifact>
       <includes>
           <include>**</include>
       </includes>
    </filter>
    <filter>
       <artifact>foo:bar</artifact>
       <excludeDefaults>false</excludeDefaults>
       <includes>
           <include>foo/Bar.class</include>
       </includes>
    </filter>
  </filters>
</configuration>

重定位类文件

https://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html

如果uber JAR被作为其他项目的依赖项,那么直接将工件依赖项中的类包含在uber JAR中可能会由于类路径上的重复类而导致类加载冲突。为了解决这个问题,可以重新定位类,以创建其字节码的私有副本:

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

这个配置的作用是通过移动相应的JAR文件并重写受影响的字节码,将类从包org.codehaus.plexus.util及其子包移动到包org.shaded.plexus.util中,但是Xpp3Dom类和其他一些类将保留在其原始包中。

也可以使用include标签缩小模式范围,下面这个配置表示org.codehaus.plexus.util这个包下只移动org.codehaud.plexus.util.io包下的类

<relocation>
  <pattern>org.codehaus.plexus.util</pattern>
  <shadedPattern>org.shaded.plexus.util</shadedPattern>
  <includes>
    <include>org.codehaud.plexus.util.io.*</include>
  </includes>
</relocation>

可执行jar包

https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
      <mainClass>org.sonatype.haven.HavenCli</mainClass>
      <Build-Number>123</Build-Number>
    </transformer>
  </transformers>
</configuration>

资源转换

https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

自定义Shade实现

https://maven.apache.org/plugins/maven-shade-plugin/examples/use-shader-other-impl.html

maven-jarjar-plugin

posted @ 2025-12-19 22:45  vonlinee  阅读(4)  评论(0)    收藏  举报