如何解决多模块项目中模块之间的依赖关系? [英] How to resolve dependencies between modules within multi-module project?

查看:943
本文介绍了如何解决多模块项目中模块之间的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven一段时间后,我对Maven引入构建体系结构的许多功能感到特别兴奋,特别是依赖管理.但是,我一次又一次遇到一个问题-Maven如何解决多模块项目之间的依赖关系.我想知道这是否是当前Maven实施的重大缺陷,并且/或者是否有令人满意的解决方法.

After working with Maven for a while, I am thrilled by the many features that Maven brings into the build architecture, particularly the dependency management. However, I have run into one issue again and again - how Maven resolves dependencies between multi-module projects. I am wondering if this is the big flaw of the current Maven implementation and/or if there is any satisfactory workaround.

假设我有一个多模块Maven项目.父pom包含三个模块-moduleA(jar),moduleB(jar)和moduleC(war). B取决于A,C取决于B.足够简单吗?现在,我想在父项目中运行mvn dependency:go-offline,它应该解决所有依赖关系并将它们带入本地.m2目录.它失败是因为Maven抱怨说,当它作用于模块B时,它无法解决对模块A的依赖关系.因为所有这些模块都属于一个groupId,所以我什至尝试使用-DexcludeGroupIds=x.y.z排除这些模块依赖性,但是在同一点它仍然失败.

Let's say I have a multi-module Maven project. The Parent pom contains three modules -- moduleA (jar), moduleB (jar), and moduleC(war). B depends on A and C depends on B. Simple enough? Now that I want to run the mvn dependency:go-offline at the parent project, which is supposed to resolve all the dependencies and bring them into the local .m2 directory. It fails because Maven complains that it cannot solve dependency for moduleA when it is acting on moduleB. Because all these modules belong to one groupId, I even try to use -DexcludeGroupIds=x.y.z to exclude these module dependencies, but it still fails at the same point.

我理解为什么Maven会抱怨-尚未构建moduleA,因此在执行脱机目标时,我的本地或内部存储库中没有moduleA:jar工件.但是恕我直言,插件应以不同的方式对待这些模块间的依赖关系.在这种情况下,它应该简单地忽略它.可能有人争辩说,我可以简单地执行mvn clean install,这会将moduleA:jar安装到本地存储库中.在那之后,运行mvn dependency:go-offline肯定可以工作.但是,这种解决方法无法达到此脱机目标的目的.这个插件使我们能够解决依赖关系并将其拉入本地存储库,而无需构建整个项目.在另一种情况下,我使用了dependency:copy-dependencies目标,它也有相同的问题.

I understand why Maven is complaining - moduleA is not built yet and thus there is no moduleA:jar artifact in my local or internal repository when go-offline goal is executed. But IMHO the plugin should treat these inter-module dependencies differently. In this case, it should simply ignore it. One might argues that I can simply do mvn clean install, which will install moduleA:jar into the local repository. After that, running mvn dependency:go-offline will work for sure. But that workaround defeats the purpose of this go-offline goal. This plugin allows us to resolve and pull dependencies into our local repository without building the whole project. I used dependency:copy-dependencies goal in another case and it has the same issue.

在其他情况下,我也遇到了类似的问题:"mvn清洁生成源"无法解决依赖关系.当我运行mvn clean compile时,一切正常,但是当我运行mvn clean generate-source时,它失败了,因为Maven无法解决模块间的依赖性.在这种情况下,是由antrun插件中的@requiresDependencyResolution引起的.

I also ran into similar issue in other scenarios: "mvn clean generate-source" could not resolve dependencies. When I ran mvn clean compile, everything works fine, but when I ran mvn clean generate-source, it fails because Maven cannot resolve inter-module dependency. In that case, the was caused by @requiresDependencyResolution in the antrun plugin.

由于antrun插件和依赖插件都在Maven世界中非常流行,因此我确信我不是唯一一个遇到此问题的人.有人找到任何解决方案/解决方法吗?

Since both antrun plugin and dependency plugin are very popular in the Maven world, I am sure I am not the only one who have run into this issue. Anyone finds any solution/workaround?

推荐答案

我怀疑Maven是否会实现这种功能.尽管您的项目有一个共同的父母,并且彼此依赖,但是Maven可能不知道在哪里可以找到这些项目以进行构建.它还无法确定是否仅需要构建项目,或者您是否为依赖项指定了错误的版本号.

I doubt such functionality would ever be possible with Maven. Whilst your projects share a common parent and depend upon each other, Maven cannot possibly know where to find these projects in order to build them. It also cannot determine whether the projects just need to be built, or whether you've specified the wrong version number for your dependency.

这篇关于如何解决多模块项目中模块之间的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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