可以使用下记几种:
1、maven-shade-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- <transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>-->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>xxx.Main</mainClass>
</transformer>
<!-- <transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>-->
</transformers>
</configuration>
</execution>
</executions>
</plugin>
会生成两个jar,一个不带依赖,一个包含依赖的。
2、maven-assembly-plugin(支持定制化打包方式)
<build>
<finalName>dropFileUpload</finalName>
<plugins>
<plugin>
<!-- 可以不写groupId,因为它默认就是这个-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>xx.MainClass</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<!-- <descriptors>
使用 descriptors,指定打包文件 assembly.xml,在该配置文件内指定打包操作。
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>-->
<!-- 官方提供的定制化打包方式,不想用,可以选择使用上面的指定个配置文件,在其中配置打包操作-->
<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>
2-1、assembly.xml(指定打包操作)
具体配置参考: Apache Maven Assembly Plugin – Assembly
示例:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<!-- 上记命名空间报错时,根据提示,将地址加入ignored即可 -->
<!-- id 标识符,添加到生成文件名称的后缀符 -->
<id>xxx</id>
<!-- maven-assembly-plugin 支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式 -->
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- 用来定制工程依赖 jar 包的打包方式,核心元素参考2-2 -->
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack> <!-- 解压 -->
</dependencySet>
</dependencySets>
<!-- 管理一组文件的存放位置,核心元素参考2-3 -->
<fileSets>
<fileSet>
<includes>
<include>filePath/**</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/xxx</directory>
<outputDirectory>/xxx</outputDirectory>
<excludes>
<exclude>xxx/</exclude>
</excludes>
</fileSet>
</fileSets>
<!-- 可以指定目的文件名到指定目录,其他和 fileSets 相同,核心元素参考2-4 -->
<files>
<file>
<source>README.txt</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
</assembly>
2-2、dependencySets
元素 | 类型 | 作用 |
outputDirectory | String | 指定包依赖目录,该目录是相对于根目录 |
includes/include* | List<String> | 包含依赖 |
excludes/exclude* | List<String> | 排除依赖 |
2-3、fileSets
元素 | 类型 | 作用 |
outputDirectory | String | 指定文件集合的输出目录,该目录是相对于根目录 |
includes/include* | List<String> | 包含文件 |
excludes/exclude* | List<String> | 排除文件 |
fileMode | String | 指定文件属性,使用八进制表达,分别为(User)(Group)(Other)所属属性,默认为 0644 |
2-4、files
元素 | 类型 | 作用 |
source | String | 源文件,相对路径或绝对路径 |
outputDirectory | String | 输出目录 |
destName | String | 目标文件名 |
fileMode | String | 设置文件 UNIX 属性 |

本文介绍了如何使用Maven的maven-shade-plugin和maven-assembly-plugin来打包Java项目。maven-shade-plugin可生成包含所有依赖的jar,而maven-assembly-plugin支持自定义打包配置,如包括依赖、指定主类等。两种插件都提供了方便的方式来处理项目的依赖和资源文件。
882

被折叠的 条评论
为什么被折叠?



