maven-shade-plugin

maven-shade-plugin是在package阶段用于创建带有依赖的shaded大包的工具。它能包含jar依赖、进行重命名、过滤内容、排除或包含特定jar,并解决资源冲突。通过filters和artifactSet可以精细控制打包过程,minimizeJar选项可自动移除未使用的依赖。relocations用于处理命名冲突,transformers则解决资源合并问题,提供多种资源转换策略。shadedClassifierName可自定义生成jar的后缀名。

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

maven-shade-plugin:

官方对此plugin的描述:
shade:shade is bound to the package phase and is used to create a shaded jar.
通俗理解为在package阶段打包出一个需要使用的大包。

使用的时候通常用于:
1,将需要的jar包的依赖也打进来最终的shaded大包中,因为默认的打包不会包含jar包的依赖。
2,进行重命名,特定添加、特定排除等。

结构:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

configurations部分:
filters:
将jar包中的内容排除。它是以groupId:artifactId为标识,在filter内部可以使用/更细致地控制,既可以移除代码文件,也可以移除配置文件。
如:

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

artifactSet:
将整个jar包移除掉,也是指定groupId:artifactId的标识。如Hadoop官方代码中

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

minimizeJar:
是否将项目中没有使用的依赖自动移除

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

relocations:
用于重命名,可以解决冲突问题。

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

transformers:
官方描述:Aggregating classes/resources from several artifacts into one uber JAR is straight forward as long as there is no overlap. Otherwise, some kind of logic to merge resources from several JARs is required. This is where resource transformers kick in.
主要是打包不同项目的资源进同一个JAR时的一些冲突等问题,是方便打包可以使用的工具,有很多种。http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

<transformers>
                    <!-- Needed until MSHADE-182 -->
                    <transformer implementation="org.apache.hadoop.maven.plugin.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                      <resources>
                        <resource>LICENSE</resource>
                        <resource>LICENSE.txt</resource>
                        <resource>NOTICE</resource>
                        <resource>NOTICE.txt</resource>
                        <resource>Grizzly_THIRDPARTYLICENSEREADME.txt</resource>
                        <resource>LICENSE.dom-documentation.txt</resource>
                        <resource>LICENSE.dom-software.txt</resource>
                        <resource>LICENSE.dom-documentation.txt</resource>
                        <resource>LICENSE.sax.txt</resource>
                      </resources>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                      <resource>META-INF/LICENSE.txt</resource>
                      <file>${basedir}/../../LICENSE.txt</file>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                      <resource>META-INF/NOTICE.txt</resource>
                      <file>${basedir}/../../NOTICE.txt</file>
                    </transformer>
                  </transformers>

shadedClassifierName:
用于指定生成的jar包后缀,默认是shaded后缀。
例如指定test后缀。

<configuration>
  <shadedArtifactAttached>true</shadedArtifactAttached>
  <shadedClassifierName>test</shadedClassifierName> <!-- Any name that makes sense -->
</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值