1. 程式人生 > >Maven 新增第三方Jar包到本地repositories

Maven 新增第三方Jar包到本地repositories

由於有些Jar包不是開源的,在maven remote repositories找不到相應的包,所以得通過自有的Jar包在local repositories中新增jar。然後在pom.xml中新增相應的dependency,就能用本地的jar了。 

首先,在本地在新增Jar,它的格式為: 

Java程式碼  收藏程式碼
  1. mvn install:install-file  
  2.   -Dfile=<path-to-file>  
  3.   -DgroupId=<group-id>  
  4.   -DartifactId=<artifact-id>  
  5.   -Dversion=<version>  
  6.   -Dpackaging=<packaging>  
  7.   -DgeneratePom=true  
  8. Where: <path-to-file>  the path to the file to load  
  9.        <group-id>      the group that the file should be registered under  
  10.        <artifact-id>   the artifact name for the file  
  11.        <version>       the version of the file  
  12.        <packaging>     the packaging of the file e.g. jar  


然後將相應的jar拷到對應的目錄下,在pom.xml檔案中新增相應的denpendency,它的格式為: 
Java程式碼  收藏程式碼
  1. <dependency>  
  2.    <groupId>group-a</groupId>  
  3.    <artifactId>artifact-a</artifactId>  
  4.    <version>1.0</version>  
  5. </dependency  


下面提供一個樣例,這裡我要用到weblogic,本地有自有weblogic.jar,如是, 
1.
Java程式碼  收藏程式碼
  1. mvn install:install-file  
  2.   -Dfile=D:\lib\weblogic.jar  
  3.   -DgroupId=weblogic  
  4.   -DartifactId=weblogic  
  5.   -Dversion=10.3  
  6.   -Dpackaging=jar  
  7.   -DgeneratePom=true  


將weblogic.jar拷到C:\Documents and Settings\s\.m2\repository\weblogic\weblogic路徑下。 

2.
Java程式碼  收藏程式碼
  1. <dependency>  
  2.       <groupId>weblogic</groupId>  
  3.       <artifactId>weblogic</artifactId>  
  4.       <version>10.3</version>  
  5.    </dependency>  


然後在當前pom.xml路徑下執行mvn package. 

但奇怪的是編譯不通過,出現如下錯誤: 

Could not resolve dependencies for project com.sx.teest1:test1:jar:1.0-SNAPSHOT: Failure to find weblogic:weblogic:jar:10.3 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]


在網上找了很久是update機制的問題,但是在.setting.xml檔案後還是出現同樣的錯,苦思不得其解,然後比較了denpendency能使用的jar和這個denpendency的不同之處,最後發現剛才拷過來的jar沒有相應的改檔名,這裡由於剛才install的是10.3版本的jar,所以weblogic.jar應該改為weblogic-10.3.jar,改了之後再次執行就能通過了。看來還是自己粗心大意了。拷jar包的時候一定要相應的改jar包的名字。 

另外還發現在Install的時候檔案路徑名不要有空格,否則會出現一些詭異的錯誤會讓你崩潰的!