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>