手動實現將本地jar添加到Maven倉庫

手動將本地jar添加到Maven倉庫

將jar添加到本地倉庫的做法

以下面pom.xml依賴的jar包為例:

實際項目中pom.xml依賴寫法:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>

Maven 安裝 JAR 包的命令是:

mvn install:install-file 
-Dfile=jar包的位置 
-DgroupId=上面的groupId 
-DartifactId=上面的artifactId 
-Dversion=上面的version 
-Dpackaging=jar

例如我的這個spring-context-support-3.1.0.RELEASE.jar 文件放在瞭"D:\mvn\"中

則命令為:

mvn install:install-file 
-Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar 

-DgroupId=org.springframework 

-DartifactId=spring-context-support 

-Dversion=3.1.0.RELEASE 

-Dpackaging=jar

註意:任何路徑和名稱不要有中文和空格,以防出現莫名其妙的錯誤。

還可以解決本地倉庫是從別人那邊復制的,但是需要的jar包中央倉庫不存在,導致的執行package時出現以下異常

Failed to execute goal on project relayserver: Could not resolve dependencies for project com.xxx:xxx:war:1.0-SNAPSHOT: Failure to find xxx.xxx:xxx:jar:1.0 in http://maven.aliyun
.com/nexus/content/groups/public was cached in the local repository, resolution will not be reattempted until the update interval of nexus-aliyun has elapsed or updates are forced 

不講jar包添加到本地倉庫也可在maven工程中使用外部jar包做法

假設將包htmlparser.jar放入瞭項目下的lib目錄中 :

-> ${project}/lib/htmlparser.jar

則pom.xml文件中依賴可以如下:

<dependency>
    <groupId>com.htmlparser</groupId>
    <artifactId>htmlparser</artifactId>
    <version>2.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/htmlparser.jar</systemPath>
</dependency>

手動向maven倉庫添加jar包的cmd命令

手動向maven庫裡面添加jar包,要以管理員身份進行添加,在jar包目錄打開cmd

mvn install:install-file -Dfile=文件絕對路徑 -DgroupId=maven倉庫下面的路徑 -DartifactId=jar包名-Dversion=jar包版本 -Dpackaging=jar -DgeneratePom=true

例如:

mvn install:install-file -Dfile=E:/quartz-2.2.1.jar -DgroupId=org.quartz-scheduler -DartifactId=quertz -Dversion=2.2.1 -Dpackaging=jar -DgeneratePom=true

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: