用shade插件build可执行jar包

之前一直用assembly插件来build可执行的jar包,但用assembly插件有个问题,就是在build有依赖到Spring Framework的jar的时候,像META-INF目录下面的如spring.handlers和spring.schemas文件就会被互相覆盖,初了Spring Framework,我们常用的SPI,如果有两个SPI的名字一样(当然,这是不好的practice),也就是说如果META-INF/services目录有两个名字一样的文件,也会被互相覆盖。这个时候,用shade插件就有办法避免。

Shade插件如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>YOUR_MAIN_CLASS_FULL_QUILIFIER_NAME</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> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/YOUR_SPI_SERVICE_FILE</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>

在configuration里面,指定AppendingTransformer来追加内容,而不是互相覆盖.

Written on 05 July 2018