java程序要打包可执行的jar,可以利用maven-assembly-plugin插件;
1)如果是需要将程序所有依赖的jar打包到同一个jar中,可以在pom.xml添加如下依赖:
<plugin>使用上述方法,用jar-with-dependencies作为<descriptorRef>标签的值,则表示使用了maven自带的归档格式来生成带包含所有依赖的jar压缩包。
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.xxx.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
执行maven package后则生成包含所有依赖库的可执行Jar包,直接执行生成的jar包:java -jar xxx.jar则可执行程序。
需要注意的是必须要在mainClass中指定主类的路径。
2)程序运行所需的配置文件与可执行jar同时打包部署
大部分程序运行时都会有依赖的配置文件,有时候会需要将配置文件与fat jar一起打包部署,本例子用的是自定义的maven assembly xml来实现的:
pom.xml:
<plugin>上面 maven-assembly-plugin插件的配置说明:maven打包根据两个自定义的描述文件来配置最终打包生产的文件结构:
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.xxx.MainClass</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/assembly/assembly-fat-jar.xml</descriptor>
<descriptor>src/assembly/assembly-conf.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly-fat-jar.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
这个配置将在程序的target目录生成xxx--jar-with-dependencies.jar可执行程序,相当于前面例子<descriptorRef>jar-with-dependencies</descriptorRef>的功能,这个jar在下面的配置中打包会用到。
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"这个配置将产生名字为xxx-fat-jar.tgz的压缩包,即程序最终打包的包。包含三个目录:conf/ 、sampleData/ 、lib/ 。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>fat-jar</id>
<formats>
<format>tgz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<baseDirectory>pojectDirectory</baseDirectory>
<fileSets>
<fileSet>
<includes>
<include>conf/**</include>
<include>sampleData/**</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>target/xxx-jar-with-dependencies.jar</source>
<outputDirectory>lib</outputDirectory>
</file>
</files>
</assembly>
其中<include>conf/**</include>和<include>sampleData/**</include>是程序中需要被打包的配置文件目录,可根据需要修改。
<file><source>target/xxx--jar-with-dependencies.jar</source><outputDirectory>lib</outputDirectory></file>这段是将前面生产的jar打包到压缩文件中的lib目录下。