我有一个项目,我需要在 Maven 上安装一个库,以便我可以在该项目上使用它。我遇到的问题是,上述库(例如 libA)本身有一个依赖项 libB,它也是第三方库。
我已使用以下代码将两者添加到我的本地存储库:
mvn install:install-file -Dfile=VerBDConverter.jar -DgroupId=verbdconverter
-DartifactId=verbdconverter -Dversion=1.00 -Dpackaging=jar -DgeneratePom=true
对 lib 2 做同样的事情。问题是,当我转到项目的 pom 并为 libA 添加
问题:应该是,毕竟Maven应该获取libA的依赖,但是没有。
最佳答案
It should be, after all, Maven should get libA's dependencies, but it didn't.
不,在你的情况下,Maven 不会突然知道 libA 需要哪些传递依赖项,因为 libA 是手动安装的,并且在任何地方都没有 libB 的踪迹。
通常,传递依赖项是在 .pom
文件的 dependencies
部分中定义的依赖项,作为已部署应用程序的一部分可用。 .pom
文件本质上是原始 pom.xml
文件的副本,重命名以反射(reflect)库名称(即 artifactId-version.jar
,然后是 artifactId-version.pom
)。
当解析一个依赖时,maven 也会检查它的 .pom
文件,并因此获得关于它的依赖的信息(这成为传递依赖)并为它构建(和获取)所需的依赖图(也就是说,为每个声明的依赖项重复相同的过程。
来自官方Maven - Introduction to the dependency mechanism
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
文件,一旦相关 Artifact 上传到 Maven 存储库(或安装到本地 Maven 缓存)。
根据您的问题,您使用了 -DgeneratePom=true
,因此您没有传递 libA' pom.xml
文件,但是一个新文件是 automatically generated
Generate a minimal POM for the artifact if none is supplied via the parameter
pomFile
. Defaults totrue
if there is no existing POM in the local repository yet.
自动生成的 .pom
文件将几乎是空的(Maven 坐标(groupId、artifactId、版本)但其中没有 dependencies
部分),因此 Maven 会将 libA 视为没有传递依赖的库:它找不到任何东西,也猜不到。
因此您有四种解决方案(按推荐顺序):
- 在公司环境中,设置一个 enterprise Maven repository (如 Arifactory、Nexus 或 Apache Archivia)并正确部署这些库,或者
- 使用
pomFile
重新安装 libA选项,或 - 手动将
dependencies
部分添加到生成的.pom
文件中,并向其中添加libB,或者 - 在您的消费者
pom.xml
文件中显式声明 libB
进一步阅读 SO:
关于java - Maven 安装传递依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36559692/