使用Maven创建包含依赖的可执行JAR文件
技术背景
在Java开发中,为了便于项目的分发和部署,我们常常需要将项目及其依赖打包成一个单独的可执行JAR文件。Maven作为一个强大的项目管理和构建工具,提供了多种插件来实现这一需求。
实现步骤
1. 使用maven-assembly-plugin
在pom.xml
中添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</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>
|
然后执行以下命令:
1
| mvn clean compile assembly:single
|
2. 使用maven-dependency-plugin和maven-jar-plugin
在pom.xml
中添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-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> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>theMainClass</mainClass> </manifest> </archive> </configuration> </plugin>
|
3. 使用maven-shade-plugin
在pom.xml
中添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> <excludes> <exclude>bouncycastle:bcprov-jdk15</exclude> </excludes> </artifactSet> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.main.MyMainClass</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>properties.properties</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer"> <resource>applicationContext.xml</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins>
|
核心代码
以下是一个完整的pom.xml
示例,使用maven-assembly-plugin
创建可执行JAR文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version>
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.example.Main</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> </project>
|
最佳实践
- 指定编译阶段:在执行打包命令前,确保先进行编译,例如
mvn clean compile assembly:single
,以包含最新的代码更改。 - 避免重复依赖:检查项目的依赖,避免重复引入相同的依赖,以减小JAR文件的大小。
- 配置输出文件名:可以通过配置插件的属性,如
appendAssemblyId
,避免生成的JAR文件名中出现不必要的后缀。
常见问题
1. 找不到主类
确保在pom.xml
中正确配置了主类,例如:
1 2 3
| <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest>
|
2. 依赖未正确打包
检查插件的配置是否正确,例如maven-assembly-plugin
的descriptorRef
是否为jar-with-dependencies
。
3. 资源文件冲突
当存在同名的资源文件时,可能会出现冲突。可以使用maven-shade-plugin
的资源转换器来解决这个问题,例如:
1 2 3
| <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>properties.properties</resource> </transformer>
|