![](/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.