maven 常用打包方式汇总

通用的三种打包

1.方式一: 最小化打包 maven-jar-plugin

用途: 可以用来发布maven仓库 或最小化共享

一般不包含第三方依赖,
可以结合maven-dependency-plugin插件把其他依赖也一起打包

示例:

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>cn.note.swing.SpringViewApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

2. 方式二: 通用打包 maven-shade-plugin

用途: 普通工程打包为可执行jar ,相当于Fat Jar

shade打包一般够用, 但是如果常用的配置项不能满足需求时,可以使用maven-assembly-plugin

示例:

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <transformers>
                        <!--设置主函数-->
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>cn.note.swing.deploy.SingleAutoHelper</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

3.方式三: 可自定义扩展打包maven-assembly-plugin

用途: 可以自定义bin文件夹,嵌入一些bat文件或sh文件

相对于maven-shade-plugin比较复杂一些,但是对于特殊需要时 ,可以考虑使用

示例

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <!--程序入口 -->
                            <mainClass>cn.note.swing.SpringViewApplicationcation</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/*.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <!--文件名后缀 -->
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <descriptors>
                        <!--assembly配置文件路径,注意需要在项目中新建文件assembly/release.xml -->
                        <descriptor>${project.basedir}/assembly/release.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>

常用的配置信息:

字段解析
formats是assembly插件支持的打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个format
id是添加到打包文件名的标识符,用来做后缀。也就是说,如果按上面的配置,生成的文件就是artifactId − {artifactId}-artifactId−{version}-assembly.tar.gz
fileSets/fileSet用来设置一组文件在打包时的属性
directory源目录的路径
includes/excludes设定包含或排除哪些文件,支持通配符
fileMode指定该目录下的文件属性,采用Unix八进制描述法,默认值是064
outputDirectory生成目录的路径
files/file与fileSets大致相同,不过是指定单个文件,并且还可以通过destName属性来设置与源文件不同的名称
dependencySets/dependencySet用来设置工程依赖文件在打包时的属性,也与fileSets大致相同
dependencySet-unpack布尔值,false表示将依赖以原来的JAR形式打包,true则表示将依赖解成*.class文件的目录结构打包
dependencySet-scope表示符合哪个作用范围的依赖会被打包进去。compile与provided都不用管,一般是写runtime

特殊的spring-boot-maven-plugin打包方式

用途: 最常见应用于spring-boot项目

spring-boot-maven-plugin 为基于maven-assembly-plugin的封装,为了解决特殊的resources资源文件下的文件冲突问题. 如:spring.schemas和spring.handlers

示例:

  <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>cn.note.helper.NoteHelperApplication</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

参考文章:https://www.kongzid.com/archives/mvn1#fulu

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值