当前位置: 代码迷 >> 综合 >> java-Maven打包插件 Assembly 和 Shade 插件的不同之处
  详细解决方案

java-Maven打包插件 Assembly 和 Shade 插件的不同之处

热度:2   发布时间:2024-02-23 14:03:19.0

Maven Assembly Plugin 和 Shade Plugin 都可以用来在构建单一 Jar 包时,将所有 Dependency 打入这个最终生成的 Jar 中去。但是两者在具体的行为上有所不同:Assembly 插件不仅会将 Dependency 中的 Class 文件打入最终的 Jar 包,还会将 Dependency 中的资源文件,诸如 properties 文件打入最终的 Jar 包。当项目和其 Dependency 中有同名的资源文件是,就会发生冲突,项目中的同名文件便不会加入到最终的 Jar 包中。如果这个文件是一个关键的配置文件,便会导致问题。而 Shade Plugin 不存在这样的问题。

 

1、使用maven-shade-plugin插件打包(首选)

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>8</source>

<target>8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>3.2.1</version>

<!-- 不生成新的pom文件-->

<configuration>

<createDependencyReducedPom>false</createDependencyReducedPom>

</configuration>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>shade</goal>

</goals>

<configuration>

<transformers>

<transformer

implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

<mainClass>Run</mainClass>

</transformer>

</transformers>

</configuration>

</execution>

</executions>

</plugin>

</plugins>

</build>

 

2、使用maven-assembly-plugin插件打包

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>8</source>

<target>8</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-assembly-plugin</artifactId>

<version>3.1.1</version>

<configuration>

<archive>

<manifest>

<mainClass>Run</mainClass>

</manifest>

</archive>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

</configuration>

<executions>

<execution>

<id>make-assembly</id>

<phase>package</phase>

<goals>

<goal>single</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>