当前位置: 首页 > news >正文

Maven打包时将本地 jar 加入 classpath

在使用 maven 编译项目时,我们有时候会需要引入本地的 jar 包作为依赖(不部署到本地仓库),一般会使用 scope 为 system 的标签来引入,如下所示:

<dependency><groupId>com.example</groupId><artifactId>system-dependency</artifactId><version>1.0.0</version><scope>system</scope><systemPath>${basedir}/lib/system-dependency.jar</systemPath>
</dependency>

此时,pom 中往往还有其他的依赖,是从远端的 maven 仓库下载。如果我们需要同时将本地 jar 和远端下载的 jar 同时注册到 classpath 中,我们可以这么配置插件。

一、配置maven-dependency-plugin

首先利用maven-dependency-plugin插件,将 runtime 和 system 的依赖都拷贝到指定路径,这里是选择的路径是target/lib:

            <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>3.4.0</version><executions><execution><id>copy-runtime-dependency</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><excludeTransitive>false</excludeTransitive><outputDirectory>${project.build.directory}/lib</outputDirectory><includeScope>runtime</includeScope></configuration></execution><execution><id>copy-system-dependency</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><excludeTransitive>false</excludeTransitive><outputDirectory>${project.build.directory}/lib</outputDirectory><includeScope>system</includeScope></configuration></execution></executions></plugin>

二、配置maven-jar-plugin

然后利用maven-jar-plugin插件将这些所有依赖的 jar 都注册到 classpath中,如下所示:

            <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.3.0</version><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>com.xx.YourMain</mainClass></manifest><manifestEntries><Class-Path>lib/system-dependency.jar</Class-Path></manifestEntries></archive></configuration></plugin>

这里的lib/system-dependency.jar对应的就是我们通过maven-dependency-plugin插件拷贝之后的新 jar,而不是最初的 systemPath 路径。

三、编译 & 执行

最终通过mvn clean package命令编译之后,就可以在我们自己的 jar的MANIFEST.MF文件中查看,所有的 jar 都被写到classpath,包括本地 jar。然后我们就可以通过 java -jar xxx.jar直接执行我们自己的程序。

http://www.lryc.cn/news/409146.html

相关文章:

  • Spring Boot打出的jar包为什么可以独立运行
  • “微软蓝屏”事件:网络安全与稳定性的深刻反思
  • 【技术升级】Docker环境下Nacos平滑升级攻略,安全配置一步到位
  • [题解]CF1401E.Divide Square(codeforces 05)
  • 软考高级第四版备考--第32天(新一代信息技术及应用)
  • 【RabbitMQ】MQ相关概念
  • 【MySQL是怎样运行的 | 第二篇】MySQL三大日志文件
  • 视图、存储过程、触发器
  • 【学习笔记】解决Serial Communication Library编译问题
  • 在 Windows 环境下实现负载均衡:提升系统性能与可靠性的关键技术
  • 【Linux】-----工具篇(自动化构建工具make/makefile)
  • 图的遍历:深度优先搜索(DFS)
  • 普元EOS学习笔记-某些版本的EOS提供的maven获取依赖失败的问题解决
  • Pycharm + Pyside6
  • 强化学习之价值迭代算法动态规划求解悬崖漫步环境(CliffWalking)最优策略及最优状态价值函数
  • javascript deriveKey和deriveBits()由主密钥派生出新的密钥进行加密
  • 基于微信小程序的自习室选座系统/基于Java的自习室选座系统/自习室管理系统的设计与实现
  • echarts所遇到的问题,个人记录
  • Skyeye云智能制造企业版源代码全部开放
  • Springboot 整合Elasticsearch
  • WeNet环境配置与aishell模型训练
  • 【C++的剃刀】我不允许你还不会AVL树
  • React搭建Vite项目及各种项目配置
  • Linux Vim教程:多文件编辑与窗口管理
  • C语言进阶 11.结构体
  • Vue--解决error:0308010C:digital envelope routines::unsupported
  • go-kratos 学习笔记(6) 数据库gorm使用
  • 记录:vite打包报错 error during build: Error: Parse error @:1:1
  • Python 消费Kafka手动提交 批量存入Elasticsearch
  • oracle 基础知识表的主键