一、安裝到本地倉庫:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true
其中,
<path-to-file> 是jar包的絕對路徑,
<group-id>,<artifact-id>,<version>,<packaging> 是jar包的坐標信息,
-DgeneratePom=true 是指定是否生成pom文件。
例如,如果您有一個名為mylib.jar的jar包,它的坐標信息是:
<groupId>com.mycompany</groupId>
<artifactId>mylib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
并且它位于您的C盤根目錄下,那么您可以使用以下命令將它安裝到本地倉庫中:
mvn install:install-file -Dfile=C:/mylib.jar -DgroupId=com.mycompany -DartifactId=mylib -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
二、安裝到私有倉庫:
與安裝到本地倉庫不同,如果安裝到私有倉庫,則需使用deploy:deploy-file,且需要加上-DrepositoryId、-Durl
mvn deploy:deploy-file -Dfile=C:/mylib.jar -DgroupId=com.mycompany -DartifactId=mylib -Dversion=1.0 -Dpackaging=jar -DrepositoryId=your drepository id -Durl=your drepository id
-DrepositoryId參數(shù)指定了遠程倉庫的id,它必須與pom.xml中配置的repository元素的id屬性一致。
-Durl參數(shù)指定了遠程倉庫的url,它必須與pom.xml中配置的repository元素的url屬性一致。
這樣,Maven才能正確地訪問和上傳遠程倉庫。
如果你的私有倉庫是加密的,你需要在maven的setting.xml配置:
<servers>
<server>
<id>xxxxxxxx</id>
<username>xxxx</username>
<password>xxxxxxxx</password>
</server>
</servers>
三、使用方式
安裝成功后,您可以在您的項目中像使用其他依賴一樣使用它:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mylib</artifactId>
<version>1.0</version>
</dependency>