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

查看:57
本文介绍了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 位于源代码管理中的 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.

这能做到吗?怎么样?

推荐答案

我希望 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 作用域,这样的依赖在很多情况下都不能很好地工作(例如在汇编中),它们带来的麻烦多于好处.

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://${project.basedir}/my-repo</url>
  </repository>
</repositories>

使用 install:install-filelocalRepositoryPath 参数:

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>

更新: 在使用 2.2 版插件时,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-插件文档

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

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>

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

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天全站免登陆