有时我们打jar包时需要将dependency中的所有依赖打入一个jar包中,包括用systempath修饰的依赖,这个时候我们需要对assembly插件进行一些特殊配置。
pom.xml如下所示:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<!--程序入口-->
<mainClass>com.xu.Hello</mainClass>
</manifest>
<manifestEntries>
<Class-Path>lib/*.jar</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<!--文件名后缀-->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors>
<!--assembly配置文件路径,注意需要在项目中新建文件assembly/release.xml-->
<descriptor>${basedir}/assembly/release.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
在项目根目录下新建文件夹assembly,然后在该文件夹中新建文件release.xml
文件内容如下所示:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>release</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
在项目的根目录下运行命令即可
mvn package assembly:single