Maven:通过相对路径添加对jar的依赖 [英] Maven: add a dependency to a jar by relative path

查看:532
本文介绍了Maven:通过相对路径添加对jar的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a proprietary jar that I want to add to my pom as a dependency.

但是我不想将它添加到存储库。原因是我想要我通常的maven命令,例如 mvn compile 等,以便开箱即用。 (没有开发人员的要求,自己将它添加到一些存储库)。

But I don't want to add it to a repository. The reason is that I want my usual maven commands such as mvn compile, etc, to work out of the box. (Without demanding from the developers a to add it to some repository by themselves).

我想让jar在源代码控制中位于第三方lib,并链接到通过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.

可以这样做吗?如何?

推荐答案


我想让jar在源代码控制中的第三方lib,并链接通过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.

如果你真的想要这个(明白,如果你不能使用公司存储库),那么我的建议是在项目本地使用文件存储库,并且不使用 c c c> c> c> c> c。应避免使用系统,这种依赖在许多情况下(例如在装配中)不能正常工作,它们比利益带来更多的麻烦。

If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not use a system scoped dependency. The system scoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.

相反,在项目本地声明一个存储库:

So, instead, declare a repository local to the project:

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

使用 install安装第三方库:install-file localRepositoryPath 参数:

Install your third party lib in there using install:install-file with the localRepositoryPath parameter:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \ 
                         -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>

更新:似乎 install:install-file 忽略 localRepositoryPath 插件。但是,它适用于2.3及更高版本的插件。所以使用插件的全限定名来指定版本:

Update: It appears that install:install-file ignores the localRepositoryPath when using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:

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文档

最后,声明它像任何其他依赖关系(但没有系统范围):

Finally, declare it like any other dependency (but without the system scope):

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

这是一个比使用系统更好的解决方案范围,因为你的依赖将被视为一个好的公民(例如它将被包括在一个集会等等)。

This is IMHO a better solution than using a system scope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).

现在,我必须提到在公司环境中处理这种情况的正确方法(可能不是这种情况)将是使用公司存储库。

Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.

这篇关于Maven:通过相对路径添加对jar的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆