Maven安装传递依赖项 [英] Maven install transitive dependencies

查看:109
本文介绍了Maven安装传递依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我需要在Maven上安装一个库,以便可以在上述项目中使用它.我遇到的问题是,所说的库(例如libA)本身具有一个依赖项libB,它也是第三方的依赖关系.

I have a project, and I need to install a library on Maven so that I can use it on said project. The problem I ran into is that said library, say libA, has a dependency itself, libB, which is also a third party one.

我已使用以下代码将两者都添加到了本地存储库中:

I have added both to my local repository by using this code:

mvn install:install-file -Dfile=VerBDConverter.jar -DgroupId=verbdconverter
-DartifactId=verbdconverter -Dversion=1.00 -Dpackaging=jar -DgeneratePom=true  

对于lib 2也是一样的.问题是,当我转到项目的pom并添加<时,就会出现问题.依赖>对于libA,Maven不会选择libB.

Did the same for the lib 2. Problem is that, when I go to my project's pom and add the < dependency > for libA, Maven is not picking up libB.

问题:毕竟应该是Maven应该获得libA的依赖关系,但事实并非如此.

Question: It should be, after all, Maven should get libA's dependencies, but it didn't.

推荐答案

毕竟,应该是Maven应该获得libA的依赖关系,但事实并非如此.

It should be, after all, Maven should get libA's dependencies, but it didn't.

不,在您的情况下,Maven不会突然知道libA需要哪些传递依赖项,因为libA是手动安装的,并且在任何地方都没有libB的痕迹.

No, in your case Maven will not know out of the blue which transitive dependencies libA would require, because libA was manually installed and there is no trace of libB anywhere.

通常,可传递依赖项是在 .pom 文件的 dependencies 部分中定义的依赖关系,该文件可作为已部署应用程序的一部分提供. .pom 文件本质上是原始 pom.xml 文件的副本,已重命名以反映库名称(即 artifactId-version.jar ,然后是 artifactId-version.pom ).

Normally, transitive dependencies are dependencies defined in the dependencies section of the .pom file available as part of a deployed application. The .pom file is essentially a copy of the original pom.xml file, renamed to reflect the library name (i.e. artifactId-version.jar, then artifactId-version.pom).

解析依赖项时,maven还将检查其 .pom 文件,从而获取有关其依赖项的信息(成为传递性依赖项)并为其构建(并获取)所需的依赖关系图(也就是说,为每个声明的依赖项重复相同的过程.

When resolving a dependency, maven will also check its .pom file and as such get information about its dependencies (which become transitive dependencies) and build (and fetch) the required dependencies graph for it (that is, re-iterate the same process for each and every declared dependency).

来自官方 Maven-依赖机制简介

通过从指定的远程存储库中读取依赖项的项目文件,可以方便地使用此功能.通常,这些项目的所有依赖项都会在您的项目中使用,就像该项目从其父项或从其依赖项继承的任何依赖项一样.

This feature is facilitated by reading the project files of your dependencies from the remote repositories specified. In general, all dependencies of those projects are used in your project, as are any that the project inherits from its parents, or from its dependencies, and so on.

注意:粗体是我的.项目文件通常是 pom.xml 文件,一旦相关工件上传到Maven存储库(或安装到Maven存储库中),它们便重命名为 *.pom 文件.本地Maven缓存).

Note: bold is mine. project files are normally the pom.xml files, renamed into *.pom files once related artifacts are uploaded to a Maven repository (or installed into the local Maven cache).

根据您的问题,您使用了 -DgeneratePom = true ,因此您没有传递libA的 pom.xml 文件,但是一个新文件是自动生成

From your question, you used -DgeneratePom=true, hence you didn't pass libA' pom.xml file, but a new one was automatically generated

如果没有通过参数 pomFile 提供任何内容,则为工件生成最小POM.如果本地存储库中还没有现有的POM,则默认为 true .

Generate a minimal POM for the artifact if none is supplied via the parameter pomFile. Defaults to true if there is no existing POM in the local repository yet.

自动生成的 .pom 文件将几乎为空(Maven坐标(groupId,artifactId,版本),但是其中没有 dependencies 部分),因此Maven将libA视为没有传递依赖项的库:它找不到任何内容,也无法猜测.

The autogenerated .pom file will be almost empty (Maven coordinates (groupId, artifactId, version) but no dependencies section in it), hence Maven will treat libA as library with no transitive dependencies: it cannot find any, it cannot guess neither.

因此,您有四个解决方案(按推荐顺序):

You hence have four solutions (in order of recommendation):

  • 在公司环境中,设置企业Maven存储库(例如Arifactory,Nexus或Apache Archivia),并在其中正确部署这些库,或者
  • 使用>重新安装libA> pomFile 选项,或
  • 手动将 dependencies 部分添加到生成的 .pom 文件中,并在其中添加libB,或者
  • 在使用者的 pom.xml 文件中明确声明libB
  • In corporate environment, set-up an enterprise Maven repository (like Arifactory, Nexus or Apache Archivia) and properly deploy these libraries to it, or
  • Re-install libA using the pomFile option, or
  • Manually add the dependencies section to the generated .pom file and add libB to it, or
  • Explicitely declare libB in your consumer pom.xml file

要进一步阅读SO:

这篇关于Maven安装传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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