【maven】两种方法解决本地第三方jar包引用问题

时间:2021-05-03 19:02:18

第一种,将本地jar包导入local repository里面。Maven official 文档中如是说:

http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

Guide to installing 3rd party JARs

Although rarely, but sometimes you will have 3rd party JARs that you need to put in your local repository for use in your builds, since they don't exist in any public repository like Maven Central. The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Apache Maven. To make this easier, and less error prone, we have provide a goal in the maven-install-pluginwhich should make this relatively painless. To install a JAR in the local repository use the following command:

1.1

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

上面1.1的方法,适用于non-mavenized jar,就是没有pom文件的jar包(如果jar包是用maven创建的,那么jar包中必然包括它的pom.xml和pom.properties文件),这个jar可能时用ant或者直接用jar工具做出来的。这里的group-id, artifact-id,version可以随便给,这里指定了什么值,你的工程中的pom文件中的dependency就需要怎么写。packaging一般是jar(直接赋值jar即可),另外该命令可以在没有pom.xml的目录中执行成功。下面是个例子:

mvn install:install-file -Dfile=./binary_search-1.0.3.jar -DgroupId=test.search \
-DartifactId=binarySearch -Dversion=1.0.3 -Dpackaging=jar

#0> cat ~/.m2/repository/test/search/binarySearch/1.0.1/binarySearch-1.0.1.pom
#<?xml version="1.0" encoding="UTF-8"?>
#<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
# xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
# <modelVersion>4.0.0</modelVersion>
# <groupId>test.search</groupId>
# <artifactId>binarySearch</artifactId>
# <version>1.0.1</version>
# <description>POM was created from install:install-file</description>
#</project>
我的工程文件中这样定义依赖的

        <dependency>
<!--if add it into the </dependencyManagement>, it does not work-->
<groupId>test.search</groupId>
<artifactId>binarySearch</artifactId>
<version>1.0.3</version>
</dependency>


注意,这些dependency应该加在<dependencies>标签下,绝不能加到    <dependencyManagement>     <dependencies>标签,否则仍然是引用不了这个第三方的jar。
    

1.2 If there's a pom-file as well, you can install it with the following command:

mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

上面这个方式,是知道jar也知道jar的pom文件,但我们在自己工程文件中,仍然像上面一样加入dependency的。

1.3  With version 2.5 of the maven-install-plugin it gets even better. If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default. In that case, all you need to do is:

mvn install:install-file -Dfile=<path-to-file>

上面这个方式,适合与maven创建的第三方jar,这个jar还不存在任意repository中。


2. 第二种方式,就是不运行上面的命令,而是直接导入3rd-party jars。我们自己工程的pom.xml可以这样写:

  <dependency>
<!--if add it into the </dependencyManagement>, it does not work-->
<groupId>test.search</groupId>
<artifactId>binarySearch</artifactId>
<version>1.0.3</version>
<systemPath>${basedir}/tmp/binary_search.jar</systemPath>
<scope>system</scope>
</dependency>

上面的groupId, artifactId,version可以任意给定,反正也不会用到这三个属性,但是systemPath是第三方jar的在本project中的路径,scope也必须是system。


经过上面的配置,我们就可以像使用maven public repository中的jar一样,使用第三方的本地jar了。