1. 程式人生 > >maven指定本地jar包

maven指定本地jar包

一、怎麼新增jar到本地倉庫呢?
步驟:
1.cmd命令進入該jar包所在路徑
2.執行命令:
mvn install:install-file -Dfile=lucene-queryparser-4.6.1.jar -DgroupId=org.apache.lucene -DartifactId=lucene-queryparser -Dversion=4.6.1 -Dpackaging=jar
其中:-DgroupId和-DartifactId的作用是指定了這個jar包在repository的安裝路徑,只是用來告訴專案去這個路徑下尋找這個名稱的jar包。
比如:
mvn install:install-file -Dfile=hadoop-hdfs-2.2.0.jar -DgroupId=org.apache.hadoop -DartifactId=hadoop-hdfs -Dversion=2.2.0 -Dpackaging=jar
就是指把hadoop-hdfs-2.2.0.jar安裝到repository\org.apache.hadoop\hadoop-hdfs\2.2.0目錄下,執行完命令後,如果需要在專案中使用這個jar,則在pom.xml中新增如下配置即可:
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.2.0</version>
</dependency>

注意在每個引數前有個-D

二、怎麼在pom.xml中新增專案中libs下的jar呢,而不是從本地倉庫中新增?

1、首先將要新增的jar包複製到專案中的libs資料夾下

2、然後在pom.xml中新增如下程式碼:

<dependency>

<groupId>htmlunit</groupId>

<artifactId>htmlunit</artifactId>

<version>2.21-OSGi</version>

<scope>system</scope>

<systemPath>${project.basedir}/libs/htmlunit-2.21-OSGi.jar</systemPath>

</dependency>

注意scope元素和systemPath元素,其中systemPath元素指定的就是jar包在專案中的路徑。

注意libs資料夾下的這個jar包不需要Add to Build Path
下面是maven中央倉庫的地址:http://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit/2.21

可以在這裡搜尋想要的jar包,然後複製對應的依賴程式碼到你專案中的pom.xml中,則對應的jar包將下載到你本地的maven倉庫中,以提供給你使用。