在使用Java的时候经常会做打包操作,有的时候一个小功能的实现可能会依赖一些jar包,
需求是:只要提供一个可运行的jar即可,因此就有必要将所有的依赖jar都打包到同一个可运行的jar中,这样可以更加简洁的提供一个可运行jar程序,
使用Maven可以实现这一需求,pom.xml中设置打包plugin,具体配置如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cn.xx.Aes.xxEncode</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</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>
配置之后,从新clean package,即可在target目录下生成Aes-0.0.1-SNAPSHOT-jar-with-dependencies.jar,该jar可以直接拷贝去运行,
> java -jar Aes-0.0.1-SNAPSHOT-jar-with-dependencies.jar