使用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包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java任务定时执行器案例的实现

    Java任务定时执行器案例的实现

    定时器会执行指定的任务,本文主要介绍了Java任务定时执行器案例的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • Mybatis批量删除多表

    Mybatis批量删除多表

    MyBatis的作用我想不用多说,今天说说MyBatis中的批量删除操作。 需要的朋友一起看看吧
    2017-10-10
  • SpringBoot+MySQL+Jpa实现对数据库的增删改查和分页详解

    SpringBoot+MySQL+Jpa实现对数据库的增删改查和分页详解

    这篇文章主要介绍了SpringBoot+MySQL+Jpa实现对数据库的增删改查和分页详解,需要的朋友可以参考下
    2020-02-02
  • Java concurrency集合之LinkedBlockingDeque_动力节点Java学院整理

    Java concurrency集合之LinkedBlockingDeque_动力节点Java学院整理

    LinkedBlockingDeque是双向链表实现的双向并发阻塞队列。该阻塞队列同时支持FIFO和FILO两种操作方式,即可以从队列的头和尾同时操作(插入/删除);并且,该阻塞队列是支持线程安全。
    2017-06-06
  • 浅谈Spring Context加载方式

    浅谈Spring Context加载方式

    这篇文章主要介绍了浅谈Spring Context加载方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • java实现点击按钮事件弹出子窗口

    java实现点击按钮事件弹出子窗口

    这篇文章主要为大家详细介绍了java实现点击按钮事件弹出子窗口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • SpringBoot集成企业微信开发的实现

    SpringBoot集成企业微信开发的实现

    本文将详细介绍如何使用 Spring Boot 集成企业微信开发,通过企业微信 API 可以实现企业内部的一些自动化业务流程,提高工作效率,感兴趣的可以了解一下
    2023-07-07
  • 泛谈Java中的不可变数据结构

    泛谈Java中的不可变数据结构

    开发人员通常认为拥有final引用,或者val在Kotlin或Scala中,足以使对象不可变。这篇博客文章深入研究了不可变引用和不可变数据结构,下面小编来和大家一起学习它
    2019-05-05
  • Java非阻塞I/O模型之NIO相关知识总结

    Java非阻塞I/O模型之NIO相关知识总结

    在了解NIO (Non-Block I/O) 非阻塞I/O模型之前,我们可以先了解一下原始的BIO(Block I/O) 阻塞I/O模型,NIO模型能够以非阻塞的方式更好的利用服务器资源,需要的朋友可以参考下
    2021-05-05
  • MyBatisPlus自定义JsonTypeHandler实现自动转化JSON问题

    MyBatisPlus自定义JsonTypeHandler实现自动转化JSON问题

    这篇文章主要介绍了MyBatisPlus自定义JsonTypeHandler实现自动转化JSON问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论