因客观原因,我们可能需要在maven项目中引入本地jar包,一般有两种方式:
1、修改pom,即scope要设置为system,systemPath指定jar包位置;
2、将依赖安装到本地repository中;
第一种方法,代码没有问题,但是maven打包的时候不能将外部的jar包打入,所以建议使用第二种方法;
1、pom中scope要设置为system,systemPath指定jar包位置:
以fastjson-1.2.62.jar为例:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
<scope>system</scope>
<systemPath>${basedir}/lib/fastjson-1.2.62.jar</systemPath>
</dependency>
2、将依赖安装到本地repository中:
cd到jar包的存储目录,执行以下命令:
mvn install:install-file -Dfile=fastjson-1.2.62.jar -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.2.62 -Dpackaging=jar
依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>