1. 程式人生 > >Maven兩種方法解決本地第三方jar包引用問題

Maven兩種方法解決本地第三方jar包引用問題



第一種:

將本地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的目錄中執行成功。下面是個例子:

[html] view plain copy print?
  1. mvn install:install-file -Dfile=./binary_search-1.0.3.jar -DgroupId=test.search \  
  2. -DartifactId=binarySearch-Dversion=1.0.3 -Dpackaging=jar
  3. #0> cat ~/.m2/repository/test/search/binarySearch/1.0.1/binarySearch-1.0.1.pom  
  4. #<?xmlversion="1.0"encoding="UTF-8"?>
  5. #<projectxsi: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"
  6. #    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  7. #  <modelVersion>4.0.0</modelVersion>
  8. #  <groupId>test.search</groupId>
  9. #  <artifactId>binarySearch</artifactId>
  10. #  <version>1.0.1</version>
  11. #  <description>POM was created from install:install-file</description>
  12. #</project>
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>
我的工程檔案中這樣定義依賴的 [html] view plain copy print?
  1. <dependency>
  2.     <!--if add it into the </dependencyManagement>, it does not work-->
  3.     <groupId>test.search</groupId>
  4.     <artifactId>binarySearch</artifactId>
  5.     <version>1.0.3</version>
  6. </dependency>
        <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中。

第二種方式:

就是不執行上面的命令,而是直接匯入3rd-party jars。我們自己工程的pom.xml可以這樣寫:

[html] view plain copy print?
  1. <dependency>
  2.           <!--if add it into the </dependencyManagement>, it does not work-->
  3.           <groupId>test.search</groupId>
  4.           <artifactId>binarySearch</artifactId>
  5.           <version>1.0.3</version>
  6.           <systemPath>${basedir}/tmp/binary_search.jar</systemPath>
  7.           <scope>system</scope>
  8.       </dependency>
  <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了。