Maven插件 maven-shade-plugin

原文:
https://my.oschina.net/u/2377110/blog/1585553

网上有一些 maven-shade-plugin 替代 maven-assembly-plugin 的文章,原因是代 maven-assembly-plugin 打出的 jar 包中要么是不能设置 Main-Class,要么 spring 的 META-INF/spring.*文件相互覆盖了。对于这两个问题,maven-assembly-plugin 在当前的版本(3.1.0)中都可以解决了(方法见https://my.oschina.net/u/2377110/blog/1584205)。

实际上这两个插件所针对的用途其实是有差异的,而它们与 maven 默认的 maven-jar-plugin 都是打包插件,简单的区别如下:

plugin function
maven-jar-plugin maven 默认打包插件,用来创建 project jar
maven-shade-plugin 用来打可执行包,包含依赖,以及对依赖进行取舍过滤
maven-assembly-plugin 支持定制化打包方式,更多是对项目目录的重新组装

当你只想将项目打成一个可执行包时,maven-shade-plugin 非常适合。一般情况下,pom 文件中 shade 插件配置如下。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.lcifn.Application</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

shade 插件绑定的是 package 生命周期目标,并设置 com.lcifn.Application 为 Main-Class,以及将 META-INF/spring.*文件合并(追加而非覆盖),并过滤掉所有依赖的 META/INF 中 SF,DSA,RSA 后缀文件。这里涉及到 filter 配置和 transformer 配置。

filters 和 artifactSet

Filter 操作在打包时将 jar 包中的内容排除。它是以 groupId:artifactId 为标识,在 filter 内部可以使用<include>/<exclude>更细致地控制,既可以移除代码文件,也可以移除配置文件。

<!-- 按package过滤junit包 -->
<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>
    </filters>
</configuration>

如果想将整个 jar 包都过滤掉,可以使用<artifactSet>,也是指定 groupId:artifactId 的标识。

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

<minimizeJar>可以和<filters>共同使用

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

资源转换

在打包时,存在将多个构件中的 class 文件或资源文件聚合的需求。shade 插件提供了丰富的 Transformer 工具类。这里介绍一些常用的 Transformer。

ManifestResourceTransformer

往 MANIFEST 文件中写入 Main-Class 是可执行包的必要条件。ManifestResourceTransformer 可以轻松实现。

<configuration>
	<transformers>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
			<mainClass>com.lcifn.Application</mainClass>
		</transformer>
	</transformers>
</configuration>

AppendingTransformer

用来处理多个 jar 包中存在重名的配置文件的合并,尤其是 spring。

<configuration>
	<transformers>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			<resource>META-INF/spring.handlers</resource>
		</transformer>
		<transformer
			implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			<resource>META-INF/spring.schemas</resource>
		</transformer>
	</transformers>
</configuration>

ServicesResourceTransformer

JDK 的服务发现机制是基于 META-INF/services/目录的,如果同一接口存在多个实现需要合并 ,则可以使用此 Transformer。

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
  </transformers>
</configuration>

更多的 Transformer 见http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

原始构件与 shade 构件

默认情况下,shade 插件会覆盖基于项目的 jar 包,而生成包含所有依赖的 jar 包。但有时需要原始的 jar 包和 shade 后的 jar 包同时被部署,可以配置如下。

<configuration>
  <shadedArtifactAttached>true</shadedArtifactAttached>
  <!-- 名称会作为后缀在shade构件jar包后 -->
  <shadedClassifierName>jackofall</shadedClassifierName>
</configuration>

参考:

http://maven.apache.org/plugins/maven-shade-plugin/index.html
http://www.jianshu.com/p/14bcb17b99e0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章