使用maven-assembly-plugin如何将system 依赖范围的jar以class 方式打包进 jar包中

 更新时间:2023年06月09日 10:12:52   作者:java爱好者  
这篇文章主要介绍了使用maven-assembly-plugin如何将system 依赖范围的jar以class 方式打包进 jar包中,本文给大家分享完美解决思路,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

List item 背景描述

服务A 有本地系统依赖(scope = system)如果服务A作为普通服务使用没有任何问题,但如果将服务A 以jar 包方式 提供给 服务B使用,那么服务B在编译的时候就有可能报错,因为找不到服务A 依赖的本地Jar。

解决问题思路:将服务A依赖的本地Jar 以class 方式直接打包进输出jar包中,这样服务B在使用服务A时,就不会报找不到本地依赖的问题了;

那要将本地依赖jar 打包进输出jar 中需要使用到 maven-assembly-plugin 插件;

1、添加插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <attach>false</attach>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这里使用 assembly.xml 配置文件方式配置打包详细

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 https://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>system</scope>
            <includes>
                <include>com.lop:CommonQueryOrderApi</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <includes>
                <include>com.lop.open:lop-opensdk-support</include>
                <!-- 这个是本地项目,不知道为什么取消本项目就不能打进jar 包中 -->
                <include>com.aimlin.local:express</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

2、本地依赖配置

   <dependency>
        <groupId>com.lop</groupId>
        <artifactId>CommonQueryOrderApi</artifactId>
        <version>${CommonQueryOrderApi.version}</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/CommonQueryOrderApi.jar</systemPath>
        <optional>true</optional>
    </dependency>

说明:
对于本地依赖添加 <scope>system</scope> 配置项,指定为本地依赖;另外还需要设置 <optional>true</optional> 防止依赖传递个服务B;

问题:

如果在编译过程出现

[WARNING] Artifact: com.xxx:xxx:jar:1.0.0 references the same file as the assembly

destination file. Moving it to a temporary location for inclusion.

这个问题一般都是 默认 maven 已经生成了jar ,这里使用maven-assembly-plugin 将原来的jar替换了,就会提示该异常,解决方案:排除到默认编译器生成jar 即可

<!-- 排除默认jar生成器 -->
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <executions>
         <execution>
             <id>default-jar</id>
             <!-- 这里将 jar 插件 生命周期绑定到打包之后,注意 不能为none,否则将不能install和 deploy jar 文件 -->
             <phase>install</phase>
         </execution>
     </executions>
 </plugin>

[WARNING] Configuration option 'appendAssemblyId' is set to false.
Instead of attaching the assembly file: /Users/xxx.jar, it will become the file for  main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: /Users/xxx/target/classes
with assembly file: /Users/xxx.jar

出现这种告警,一般都是appendAssemblyId 设置为false ,并且 <attach>true</attach> 导致的,只需要设置 <attach>false</attach>即可取消告警,同时打包方式应该指定为:single

<configuration>
     <appendAssemblyId>false</appendAssemblyId>
     <attach>false</attach>
 </configuration>
 <executions>
     <execution>
         <id>make-assembly</id>
         <phase>package</phase>
         <goals>
             <goal>single</goal>
         </goals>
     </execution>
 </executions>

默认 maven-assembly-plugin 生成的jar 不会覆盖原来的jar,如果需要覆盖原来的jar 则需要配置:<appendAssemblyId>false</appendAssemblyId> 则会只生成一个jar文件

assembly.xml 文件说明

指定生成jar 方式为: <id>jar-with-dependencies</id> 追加依赖模式;

  • <includeBaseDirectory>false</includeBaseDirectory> 是否将跟目录打包到根目录中,因为我们是将三方jar包中的class 输出到jar中,所以不能有根目录;
  • <outputDirectory>/</outputDirectory> 指定输出目录为根目录
  • <unpack>true</unpack> 需要解压缩jar包,添加到jar包中依赖是以class 路径方式写入的;
  • <scope>system</scope> 指定maven scope 范围,只会过滤 scope = system级别的依赖到jar 包;
  • <scope>runtime</scope> 运行时jar 依赖

到此这篇关于使用maven-assembly-plugin将 system 依赖范围的jar以class 方式打包进 jar包中的文章就介绍到这了,更多相关maven-assembly-plugin打jar包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot如何使用Scala进行开发的实现

    SpringBoot如何使用Scala进行开发的实现

    这篇文章主要介绍了SpringBoot如何使用Scala进行开发的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Java之判断2000~2023年有哪些年份是闰年并打印输出

    Java之判断2000~2023年有哪些年份是闰年并打印输出

    这篇文章主要介绍了Java之判断2000~2023年有哪些年份是闰年并打印输出,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • SpringBoot实现启动类的存放位置

    SpringBoot实现启动类的存放位置

    这篇文章主要介绍了SpringBoot实现启动类的存放位置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 每日六道java新手入门面试题,通往自由的道路第二天

    每日六道java新手入门面试题,通往自由的道路第二天

    这篇文章主要为大家分享了最有价值的6道java面试题,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,对hashCode方法的设计、垃圾收集的堆和代进行剖析,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • SpringMVC结合Jcrop实现图片裁剪

    SpringMVC结合Jcrop实现图片裁剪

    这篇文章主要介绍了SpringMVC结合Jcrop实现图片裁剪的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Spring Boot 异步HTTP客户端配置使用

    Spring Boot 异步HTTP客户端配置使用

    本文详细介绍SpringBoot中使用WebClient实现异步HTTP请求的完整方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-12-12
  • Java中的泛型

    Java中的泛型

    这篇文章主要介绍为何要泛型,如何使用泛型,自定义泛型的方法,泛型类的子类等多方面介绍了JAVA中的泛型,需要的小伙伴请看下文
    2021-08-08
  • java读取wav文件(波形文件)并绘制波形图的方法

    java读取wav文件(波形文件)并绘制波形图的方法

    这篇文章主要介绍了java读取wav文件(波形文件)并绘制波形图的方法,涉及java操作多媒体音频文件转换的相关技巧,需要的朋友可以参考下
    2015-06-06
  • drools中query的用法小结

    drools中query的用法小结

    这篇文章主要介绍了drools中query的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Java基本类型包装类概述与Integer类、Character类用法分析

    Java基本类型包装类概述与Integer类、Character类用法分析

    这篇文章主要介绍了Java基本类型包装类概述与Integer类、Character类用法,结合实例形式分析了java基本数据类型与字符串转换相关操作技巧,需要的朋友可以参考下
    2019-03-03

最新评论