![](/img/trans.png)
[英]Maven: add a dependency to a jar by relative path outside the project directory
[英]Maven: add a dependency to a jar by relative path
我有一個專有的 jar,我想將其作為依賴項添加到我的 pom 中。
但我不想將它添加到存儲庫中。 原因是我希望我常用的 maven 命令(例如mvn compile
等)開箱即用。 (不需要開發人員自己將其添加到某個存儲庫)。
我希望 jar 位於源代碼管理的第 3 方庫中,並通過 pom.xml 文件的相對路徑鏈接到它。
這可以做到嗎? 如何?
我希望 jar 位於源代碼管理中的 3rdparty lib 中,並通過 pom.xml 文件中的相對路徑鏈接到它。
如果你真的想要這個(理解,如果你不能使用公司存儲庫),那么我的建議是使用項目本地的“文件存儲庫”,而不是使用system
范圍的依賴項。 應該避免system
作用域,這種依賴在許多情況下(例如在匯編中)不能很好地工作,它們造成的麻煩多於好處。
因此,相反,聲明一個項目本地存儲庫:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${project.basedir}/my-repo</url>
</repository>
</repositories>
使用帶有localRepositoryPath
參數的install:install-file
第三方庫:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \\ -DartifactId=<myArtifactId> -Dversion=<myVersion> \\ -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
更新:在使用 2.2 版插件時, install:install-file
似乎忽略了localRepositoryPath
。 但是,它適用於 2.3 版及更高版本的插件。 所以使用插件的全限定名來指定版本:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
最后,像任何其他依賴項一樣聲明它(但沒有system
范圍):
<dependency>
<groupId>your.group.id</groupId>
<artifactId>3rdparty</artifactId>
<version>X.Y.Z</version>
</dependency>
恕我直言,這是一個比使用system
范圍更好的解決方案,因為您的依賴項將被視為一個好公民(例如,它將被包含在程序集中等等)。
現在,我不得不提到在公司環境中處理這種情況的“正確方法”(這里可能不是這種情況)是使用公司存儲庫。
使用system
范圍。 ${basedir}
是你的 pom 目錄。
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
但是,建議您將 jar 安裝在存儲庫中,而不是將其提交到 SCM - 畢竟這是 maven 試圖消除的。
除了我之前在Can I add jars to maven 2 build classpath without install它們?
這將在使用多模塊構建時繞過限制,特別是如果下載的 JAR 在父項目之外的子項目中被引用。 這也通過創建 POM 和 SHA1 文件作為構建的一部分來減少設置工作。 它還允許文件駐留在項目中的任何位置,而無需修復名稱或遵循 maven 存儲庫結構。
這使用了 maven-install-plugin。 為此,您需要設置一個多模塊項目並擁有一個代表構建的新項目,以將文件安裝到本地存儲庫中並確保第一個。
您的多模塊項目 pom.xml 將如下所示:
<packaging>pom</packaging>
<modules>
<!-- The repository module must be first in order to ensure
that the local repository is populated -->
<module>repository</module>
<module>... other modules ...</module>
</modules>
然后,repository/pom.xml 文件將包含加載作為項目一部分的 JAR 的定義。 以下是 pom.xml 文件的一些片段。
<artifactId>repository</artifactId>
<packaging>pom</packaging>
pom 包裝阻止它進行任何測試或編譯或生成任何 jar 文件。 pom.xml 的內容位於使用 maven-install-plugin 的構建部分。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>com.ibm.db2:db2jcc</id>
<phase>verify</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc</artifactId>
<version>9.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/src/jars/db2jcc.jar</file>
<createChecksum>true</createChecksum>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>...</execution>
</executions>
</plugin>
</plugins>
</build>
要安裝多個文件,只需添加更多執行。
這對我有用:假設我有這種依賴性
<dependency>
<groupId>com.company.app</groupId>
<artifactId>my-library</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/my-library.jar</systemPath>
</dependency>
然后,像這樣手動添加系統依賴項的類路徑
<Class-Path>libs/my-library-1.0.jar</Class-Path>
完整配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Build-Jdk>${jdk.version}</Build-Jdk>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Title>${project.name} Library</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Class-Path>libs/my-library-1.0.jar</Class-Path>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.company.app.MainClass</mainClass>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
基本上,將其添加到 pom.xml 中:
...
<repositories>
<repository>
<id>lib_id</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.mylibrary</groupId>
<artifactId>mylibraryname</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
我們切換到 gradle,這在 gradle 中效果更好;)。 我們只是指定一個文件夾,我們可以將 jars 放入這樣的臨時情況。 我們仍然在典型的依賴管理部分(即與 maven 相同)定義了大部分 jar。 這只是我們定義的另一種依賴。
所以基本上現在我們可以將任何我們想要的 jar 放入我們的 lib 目錄中進行臨時測試,如果它不是某個地方的 Maven 存儲庫。
Pascal 發布的解決方案的一個小補充
當我遵循這條路線時,我在安裝 ojdbc jar 時在 maven 中出錯。
[INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) @ validator ---
[INFO] pom.xml not found in ojdbc14.jar
添加-DpomFile后,問題解決。
$ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \
-DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \
-DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
我遇到了同樣的問題,它只需刪除 DlocalRepositoryPath 參數並在 Dfile 參數中從當前位置定義正確的路徑即可:
mvn install:install-file -Dfile=./repo/com/tridion/cd_core/1.0/cd_core-1.0.jar -DgroupId=com.tridion -DartifactId=cd_core -Dversion=1.0 -Dpackaging=jar
注:Apache Maven 3.8.6
您可以使用 eclipse 生成可運行的 Jar : Export/Runable Jar 文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.