在maven项目中解决第三方jar包依赖的问题

在maven项目中,对于那些在maven仓库中不存在的第三方jar,依赖解决通常有如下解决方法:

方法1:直接将jar包拷贝到项目指定目录下,然后在pom文件中指定依赖类型为system,如:

1 <dependencies>
2     <dependency>
3         <groupId>xxx</groupId>
4         <artifactId>xxx</artifactId>
5         <version>6.0</version>
6         <scope>system</scope>
7         <systemPath>${project.basedir}/libs/xxx-1.0.jar</systemPath>
8     </dependency>
9 </dependencies>
View Code

请注意:scope为system的依赖,在打包时不会自动打包到最终jar包中的,必须在resources节点中明确指定需要一起package的资源:

1 <resources>
2     <resource>
3         <targetPath>lib/</targetPath>
4         <directory>lib/</directory>
5         <includes>
6             <include>**/my-jar.jar</include>
7         </includes>
8     </resource>
9 </resources>
View Code

这个方法在解决单个项目依赖是可以的。

但是,如果项目中存在多个模块,且在多个模块中都需要依赖指定的第三方jar包,那在不同的模块中都进行这样的配置有失妥当,jar包要来回拷贝多次。

 

方法2:新建一个maven模块项目,专门使用这个项目来解决依赖第三方jar包的问题(前提:需要把依赖的第三方jar包install到本地仓库)

例如:
(1)新建 xxx-3rd模块,用于配置所要依赖的第三方jar包,配置依赖的方式跟处理单个项目依赖方式一样,参考方法1。
(2)在其他需要依赖第三方jar包的模块中引入对xxx-3rd模块的依赖,这样根据maven传递依赖的特性,就可以很好地解决在多个模块中同时依赖第三方jar包的问题。

1 <dependencies>
2     <dependency>
3         <groupId>xxx</groupId>
4         <artifactId>xxx-3rd</artifactId>
5         <version>1.0.0</version>
6     </dependency>
7 </dependencies>
View Code

【 参考】

http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html

原文地址:https://www.cnblogs.com/nuccch/p/6122938.html