java - Maven : add a dependency to a jar by relati

我有一个专有的 jar,我想将它作为依赖项添加到我的 pom 中。

但我不想将它添加到存储库中。原因是我希望我常用的 maven 命令(例如 mvn compile 等)能够开箱即用。 (无需开发人员自行将其添加到某个存储库中)。

我希望 jar 位于源代码控制中的 3rdparty 库中,并通过 pom.xml 文件中的相对路径链接到它。

这可以吗?怎么样?

最佳答案

I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.

如果您真的想要这个(请理解,如果您不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”并且不使用 system 范围依赖。应该避免 system 作用域,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们带来的麻烦多于好处。

因此,改为声明项目本地的存储库:

<repositories>
  <repository>
    <id>my-local-repo</id>
    <url>file://${project.basedir}/my-repo</url>
  </repository>
</repositories>

使用 install:install-filelocalRepositoryPath 在其中安装您的第三方库参数:

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>

maven-install-plugin documentation

最后,像任何其他依赖项一样声明它(但没有 system 范围):

<dependency>
  <groupId>your.group.id</groupId>
  <artifactId>3rdparty</artifactId>
  <version>X.Y.Z</version>
</dependency>

恕我直言,这是一个比使用 system 范围更好的解决方案,因为您的依赖项将被视为一个好公民(例如,它将被包含在程序集中等等)。

现在,我不得不提一下,在公司环境中处理这种情况的“正确方法”(可能不是这里的情况)是使用公司存储库。

关于java - Maven : add a dependency to a jar by relative path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2229757/

相关文章:

javascript - 让 Grunt 为不同的设置生成 index.html

visual-studio - obj 和 bin 文件夹(由 Visual Studio 创建)用

build - Gradle buildscript 依赖项

java - NoClassDefFoundError - Eclipse 和 Android

objective-c - iOS - 构建失败,CocoaPods 找不到头文件

node.js - Node 包 ( Grunt ) 已安装但不可用

macos - 在 Mac OS X 上安装/升级 gradle

java - "mvn clean install"与 "mvn install"有何不同?

c# - 外部VS2013构建错误 "error MSB4019: The imported pro

android - 如何使用 gradle 在 APK 文件名中设置 versionName?