maven引用本地jar包
背景:
公司前置的项目包对接客户公司的sdk包,由于此前置项目完全定制化,且改sdk包非通用包,不好上传至公司的maven私服使用,故引入本地jar包,此处总结改方案
1. 代码引入第三方jar包
在项目中新建成lib文件夹,结构与srv平级
2. pom文件修改
<dependency>
<groupId>cn.xxx</groupId>
<artifactId>xxx</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/cn.xxx.xxx-1.0.0-SNAPSHOT.jar</systemPath>
</dependency>
打包插件配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>lib/cn.xxx.xxx-1.0.0-SNAPSHOT.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<excludeArtifactIds>
cn.xxx.xxx-1.0.0-SNAPSHOT.jar
</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy-resources-ext</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${basedir}/lib</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
该插件三个部分
- 将本地包打包至指定位置lib下
- 所有的jar包拷贝至target/lib下,出了改本地包
- 将所有的target/lib包拷贝至jar包外的lib包下(这样实现第三方jar包与业务包分离,而不是打包成一个整体,方便后续更新只更新指定的包)
至此,引入本地包可运行项目,打包也是正常
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App