最近在开发过程中,因需要引入三方合作公司的jar包,这些jar包并没有在maven仓库中,需要解决以下问题:
- 添加外部jar包
- 打包外部jar包
目前解上述问题,用到了以下几个方法:
一、将jar包安装到本地maven仓库中,然后再引入、打包。
mvn install:install-file -Dfile=E:\workspace\haha-1.0.jar -DgroupId=com.my.test -DartifactId=haha -Dversion=1.0 -Dpackaging=jar
-Dfile:jar文件所在路径
-DgroupId:包名
-DartifactId:项目名
-Dversion:版本号
这个方法只适合个人使用,公司使用不适合,需要给每个涉及项目的人发包独立安装
二、将jar包上传到私服maven仓库中,然后在pom中正常引入、打包。
该方法需要使用nexus搭建私服,一般公司都有搭建。可以通过mvn命令上传,也可以通过nexus管理页面上传。
三、通过pom直接引入本地jar
- 在项目根目录创建一个libs文件夹,将外部jar包放入该文件夹下
- 在pom中添加外部jar依赖
<dependency>
<!-- scope=system时,gav坐标可以不和依赖的jar包信息一样,但最好保持一致,满足一定的规范性和关联性 -->
<groupId>com.haonan</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- system表示此依赖是来自外部,不是maven仓库。-->
<scope>system</scope>
<!-- 当scope=system时,systemPath属性生效,systemPath为文件绝对路径 -->
<systemPath>${project.basedir}/libs/test-1.0-SNAPSHOT.jar</systemPath>
</dependency>
添加完依赖后,项目中就可以使用该jar包中的类和方法了。
- 打包外部jar包
上一步是将依赖引入了项目中,但是打包时不会打包到jar/war中,springboot项目中还需要在打包插件中添加如下配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
重点:<includeSystemScope>true</includeSystemScope>
如果有些配置文件不是spring boot的默认路径,那么需要借助resource配置:
<resource>
<!-- 要打包的目录,取该目录下的文件 -->
<directory>libs</directory>
<!-- 指定打包到那个目录下,默认是放到class目录下(如mybatis的mapper文件) -->
<targetPath>/BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
最后,综合来说,方法二和三都可以用于实际生产,根据自身情况或者喜好进行选择。