Maven Assembly Plugin 和 Shade Plugin 都可以用来在构建单一 Jar 包时,将所有 Dependency 打入这个最终生成的 Jar 中去。但是两者在具体的行为上有所不同:Assembly 插件不仅会将 Dependency 中的 Class 文件打入最终的 Jar 包,还会将 Dependency 中的资源文件,诸如 properties 文件打入最终的 Jar 包。当项目和其 Dependency 中有同名的资源文件是,就会发生冲突,项目中的同名文件便不会加入到最终的 Jar 包中。如果这个文件是一个关键的配置文件,便会导致问题。而 Shade Plugin 不存在这样的问题。
下面列出 Assembly 和 Shade Plugin 的使用方法:
Assembly:
Shade:
下面列出 Assembly 和 Shade Plugin 的使用方法:
Assembly:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Shade:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.alcatel.ias.im.msrp.io.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>