问题描述
我有一个项目,我需要在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需要哪些传递依赖项,因为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-依赖机制简介
注意:粗体是我的.项目文件通常是 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
自动生成的 .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安装传递依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!