Maven 新增第三方Jar包到本地repositories
由於有些Jar包不是開源的,在maven remote repositories找不到相應的包,所以得通過自有的Jar包在local repositories中新增jar。然後在pom.xml中新增相應的dependency,就能用本地的jar了。
首先,在本地在新增Jar,它的格式為:
- mvn install:install-file
- -Dfile=<path-to-file>
- -DgroupId=<group-id>
- -DartifactId=<artifact-id>
-
-Dversion=<version>
- -Dpackaging=<packaging>
- -DgeneratePom=true
- Where: <path-to-file> the path to the file to load
- <group-id> the group that the file should be registered under
- <artifact-id> the artifact name for the file
-
<version> the version of the file
- <packaging> the packaging of the file e.g. jar
然後將相應的jar拷到對應的目錄下,在pom.xml檔案中新增相應的denpendency,它的格式為:
Java程式碼
- <dependency>
- <groupId>group-a</groupId>
- <artifactId>artifact-a</artifactId>
- <version>1.0</version>
-
</dependency
下面提供一個樣例,這裡我要用到weblogic,本地有自有weblogic.jar,如是,
1.
Java程式碼
- mvn install:install-file
- -Dfile=D:\lib\weblogic.jar
- -DgroupId=weblogic
- -DartifactId=weblogic
- -Dversion=10.3
- -Dpackaging=jar
- -DgeneratePom=true
將weblogic.jar拷到C:\Documents and Settings\s\.m2\repository\weblogic\weblogic路徑下。
2.
Java程式碼
- <dependency>
- <groupId>weblogic</groupId>
- <artifactId>weblogic</artifactId>
- <version>10.3</version>
- </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的時候檔案路徑名不要有空格,否則會出現一些詭異的錯誤會讓你崩潰的!