【Maven】使用maven-jar、maven-assembly、maven-shade优雅的实现第三方依赖一同打Jar包

本文详细介绍了Maven中maven-jar-plugin、maven-shade-plugin和maven-assembly-plugin三种打包工具的使用方法,包括常规jar打包、shade打包(包括依赖管理、重命名和签名处理)、以及assembly插件的定制化打包,尤其关注IDEA中的实现和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.前言

maven提供的打包插件有如下三种

  • maven-jar-plugin maven 默认打包插件【springboot默认使用该方式打包】,用来创建 project jar
  • maven-shade-plugin 用来打可执行包,executable(fat) jar
  • maven-assembly-plugin 支持定制化打包方式,例如 apache 项目的打包方式

二.常规Jar 打包:maven-jar-plugin

  • 使用maven-jar-plugin插件, 默认的打包方式,用来打普通的project JAR包 .
<build>
   <plugins>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <!-- 可执行jar包需要指定入口函数 然后通过java -jar执行 -->
                        <mainClass>类的全路径名称</mainClass>
                        <!-- 是否添加依赖的jar路径配置 -->
                        <addClasspath>true</addClasspath>
                        <!-- 依赖的jar包存放位置,和生成的jar放在同一级目录下 -->
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
                <!-- 不打包com.artisan.excludes下面的所有类 -->
                <excludes>com/artisan/excludes/*</excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 上面配置使用这个 jar包的时候就需要在它同一级的创建一个lib目录来存放。 可以使用includes或excludes选择的打包某些内容

三.Shade 打包:maven-shade-plugin

  • 插件:使用maven-shade-plugin插件

    • maven-shade-plugin提供了两大基本功能:

      • 将依赖的jar包打包到当前jar包(常规打包是不会将所依赖jar包打进来的);
      • 对依赖的jar包进行重命名(用于类的隔离);

1.如何使用

  • maven-shade-plugin 只存在一个goal :shade: ,需要将其绑定到 package的生命周期上面 上
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!--默认情况下,通过mvn package生成的jar包中因为没有指定Main-Class属性,因此并不能使用-jar配置直接运行。需要配置Main-Class。-->
                            <!--mainClass可有可无,加上的话则直接生成可运行jar包 通过java -jar xxx.jar执行-->
                            <!--<transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.App</mainClass>
                                </transformer>
                            </transformers>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 打包后target 目录下会生成可执行 Jar包。该包里面包含项目中使用了和没使用的的所有第三方包代码

2.将部分jar包添加或排除

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                
                <executions>
                    <execution>
                        <phase>package</phase>
                        
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        
                        <configuration>
                        <!--这里-->
                            <artifactSet>
                                <excludes>
                                    <exclude>jmock:*</exclude>
                                    <exclude>*:xml-apis</exclude>
                                    <exclude>org.apache.maven:lib:tests</exclude>
                                    <exclude>log4j:log4j:jar:</exclude>
                                </excludes>
                                <includes>
                                    <include>junit:junit</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  • jar包以 groupId : artifactId[[:type] : classifier]的形式表示
  • 1.3版本后插件支持通配符 ‘*’ and ‘?’

3.将依赖jar包内部资源添加或排除

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        
                        <goals>
                            <goal>shade</goal>
                        </goals>
    
                        <configuration>
                           <!--这里-->
                            <filters>
                                <filter>
                                    <artifact>junit:junit</artifact>
                                    <includes>
                                        <include>junit/framework/**</include>
                                        <include>org/junit/**</include>
                                    </includes>
                                    <excludes>
                                        <exclude>org/junit/experimental/**</exclude>
                                        <exclude>org/junit/runners/**</exclude>
                                    </excludes>
                                </filter>
                                
                                <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>

4.自动将所有不使用的类排除

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        
                        <configuration>
   							<minimizeJar>true</minimizeJar> <!--这里-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

5.将依赖的类重命名并打包进来 (隔离方案)

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>org.codehaus.plexus.util</pattern>
                                    <shadedPattern>org.shaded.plexus.util</shadedPattern>
                                    <excludes>
                                        <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                                        <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                                    </excludes>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  • org.codehaus.plexus.util重命名为org.shaded.plexus.util,原始jar包中的org.codehaus.plexus.util.xml.Xpp3Domorg.codehaus.plexus.util.xml.pull不会被重命名到目的包中

6.修改包的后缀名

  • 会生成一个以 “-oyjp”为结尾的jar包
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>oyjp</shadedClassifierName> <!--这里 -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

7.异常:Invalid signature file digest for Manifest main attributes

  • 原因:有些jar包生成时,会 使用jarsigner生成文件签名(完成性校验),分为两个文件存放在META-INF目录下:
a signature file, with a .SF extension;
a signature block file, with a .DSA, .RSA, or .EC extension;

在生成jar时,将这些排除掉,不再进行完成性校验,如下所示:

<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>

四.Assembly 打包方式:maven-assembly-plugin

  • 使用maven-assembly-plugin插件 。

  • 日常使用比较多的是maven-assembly-plugin插件

    • 例如:大数据项目中往往有很多shell脚本、SQL脚本、.properties及.xml配置项等,采用assembly插件可以让输出的结构清晰而标准化
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>${maven-assembly-plugin.version}<version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <!-- 绑定到package生命周期 -->
                    <phase>package</phase>
                    <goals>
                        <!-- 只运行一次 -->
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 配置描述符文件 -->
                <descriptor>src/main/assembly/assembly.xml</descriptor>
                
                <!-- 也可以使用Maven预配置的描述符,默认打包所有
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs> -->
            </configuration>
        </plugin>
    </plugins>
</build>

src/main/assembly/编写描述符文件assembly.xml

<assembly>
    <id>assembly</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/sql</directory>
            <includes>
                <include>*.sql</include>
            </includes>
            <outputDirectory>sql</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/classes/</directory>
            <includes>
                <include>*.properties</include>
                <include>*.xml</include>
                <include>*.txt</include>
            </includes>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}.jar</source>
            <outputDirectory>.</outputDirectory>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

在这里插入图片描述

五.IDEA使用 Maven Assembly 插件的具体实现

1.修改pom.xml文件

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <!--mainClass可有可无,加上的话则直接生成可运行jar包 通过java -jar xxx.jar执行-->
                        <manifest>
                            <mainClass>Main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 在执行 Maven 打包命令 (mvn clean package) 会在target 目录下找到一个包含所有依赖项的可执行 *-with-dependencies.jar包
    • 注: 最终的 jar包可能非常大,因为它包含了所有依赖包。如果只想打包应用程序本身而不包含其他依赖,可以考虑编写描述符文件或者使用 Maven Shade 插件来进行定制

2.使用assembly打包 先使用clean清除 然后使用assembly打包
在这里插入图片描述

3.打完包会在target目录下生成两个jar包,如果你有用maven引用外部jar,使用*-with-dependencies.jar包即可。

在这里插入图片描述

4.如果要打包的项目依赖其他项目打包的jar,需要添加file -> project-structure... -> Libraries,点击+号添加对应的jar到项目,还需要再pom.xml再引用一下`
在这里插入图片描述
5.如果项目中使用junit,导致打包失败

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墩墩分墩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值