有時我們要用的 maven 依賴項在官方repo庫中找不到,然而我們從其他渠道獲得了依賴項中的所有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
我們以文件<not-yet-commons-ssl-0.3.17.jar>為例:
mvn install:install-file -Dfile=e:/not-yet-commons-ssl-0.3.17.jar -DgroupId=org.apache.commons -DartifactId=not-yet-commons-ssl -Dversion=0.3.17 -Dpackaging=jar -DgeneratePom=true
編輯項目pom文件,在依賴項中增加條目,并指定<scope>和<systemPath>。
還是以文件<not-yet-commons-ssl-0.3.17.jar>為例:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>not-yet-commons-ssl</artifactId> <version>0.3.17</version> <scope>system</scope> <systemPath>e:/not-yet-commons-ssl-0.3.17.jar</systemPath></dependency>
等等,編譯后出現一個警告:
[WARNING] 'dependencies.dependency.systemPath' for org.apache.commons:not-yet-commons-ssl:jar should use a variable instead of a hard-coded path e:/not-yet-commons-ssl-0.3.17.jar @ line 35, column 16
[WARNING]
[WARNING] It is highly recommended to fix these PRoblems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
原來是 maven 苦口婆心地告訴我們不要使用諸如 C:/Windows 這樣的絕對路徑,而應使用 ${java.home} 這樣的相對路徑(變量),否則降低了編譯可重現性,并威脅我們以后可能不再支持這種方式。
嗯,我們可以建立一個 maven 倉庫的環境變量來消除這個警告,如果你覺得有這個必要。
新聞熱點
疑難解答