点击上方蓝色“汤圆学Java”,选择“设为星标”
点击菜单栏“激活码”,获取IDEA最新激活码,持续更新!
前言
有时候我们在项目中,会用到一些本地 jar 包文件,比如隔壁公司自己打包的;
此时无法从maven远程仓库拉取;
那么我们可以考虑把 jar 文件安装到本地 maven 库中,然后再添加依赖。
步骤
1. 添加 jar 文件到项目中
在 resources 目录中创建一个 lib 目录,将本地 jar 放进去,如下所示的 demo.jar
2. 安装 jar 包到 maven 本地仓库
这里我们可以利用 maven-install-plugin 插件来安装, pom.xml如下:
?<build>
??<plugins>
???<plugin>
????<groupId>org.springframework.bootgroupId>
????<artifactId>spring-boot-maven-pluginartifactId>
???plugin>
???<plugin>
????<groupId>org.apache.maven.pluginsgroupId>
????<artifactId>maven-install-pluginartifactId>
????<version>2.5.2version>
????<executions>
?????<execution>
??????<id>install-demo-jarid>
??????
??????<phase>cleanphase>
??????<configuration>
???????
???????<file>${project.basedir}/src/main/resources/lib/demo.jarfile>
???????
???????<groupId>com.javalovergroupId>
???????<artifactId>demoartifactId>
???????<version>1.0version>
???????<packaging>jarpackaging>
???????<generatePom>truegeneratePom>
??????configuration>
??????<goals>
??????
???????<goal>install-filegoal>
??????goals>
?????execution>
????executions>
???plugin>
??plugins>
?build>
运行mvn clean 后,会打印如下日志:
[INFO]?--------------------------<?com.example:demo?>--------------------------
[INFO]?Building?demo?0.0.1-SNAPSHOT
[INFO]?--------------------------------[?jar?]---------------------------------
[INFO]?
[INFO]?---?maven-clean-plugin:3.1.0:clean?(default-clean)?@?demo?---
[INFO]?
[INFO]?---?maven-install-plugin:2.5.2:install-file?(install-demo-jar)?@?demo?---
[INFO]?pom.xml?not?found?in?demo.jar
[INFO]?Installing?/Users/jalon/Project/SpringBootProject/demo/src/main/resources/lib/demo.jar?to?/Users/jalon/.m2/repository/com/javalover/demo/1.0/demo-1.0.jar
[INFO]?Installing?/var/folders/t6/99ks0lsn0fx2j6kt4n46f_rh0000gn/T/mvninstall6930618999486285918.pom?to?/Users/jalon/.m2/repository/com/javalover/demo/1.0/demo-1.0.pom
[INFO]?------------------------------------------------------------------------
[INFO]?BUILD?SUCCESS
[INFO]?------------------------------------------------------------------------
[INFO]?Total?time:??1.316?s
[INFO]?Finished?at:?2021-09-26T22:12:47+08:00
[INFO]?------------------------------------------------------------------------
重点是这一行:
[INFO]?Installing?/Users/jalon/Project/SpringBootProject/demo/src/main/resources/lib/demo.jar?to?/Users/jalon/.m2/repository/com/javalover/demo/1.0/demo-1.0.jar`
可以看到,将我们本地的 demo.jar 安装到了 maven 本地仓库中
3. 添加依赖
??<dependency>
???<groupId>com.javalovergroupId>
???<artifactId>demoartifactId>
???<version>1.0version>
??dependency>
此时程序就可以正常使用 demo.jar 包了,而且 maven 打包也会把 demo.jar 打包进去
参考
Apache Maven Install Plugin – Introduction
maven插件maven-install-plugin - Mars.wang
IDEA最新激活码,点击菜单栏【激活码】即可获取
|