Maven - 将 pom 的所有子模块作为依赖项包含在另一个模块中

发布于 2024-12-11 10:57:35 字数 266 浏览 0 评论 0原文

我们有一个 Maven 模块,其设置如下:

a (parent)
  -> b (submodule)
  -> c (submodule)
  -> d (submodule)

这个子模块列表设置为随着时间的推移而增长(到 20 个左右的列表)。我们有另一个模块,它将包含 a 的所有子模块作为依赖项。有没有一种巧妙的方法可以做到这一点,而不必手动保持子模块列表与依赖项列表同步。即有没有办法将一个和所有子模块作为依赖项包含在内?

We have a maven module that is set up as so:

a (parent)
  -> b (submodule)
  -> c (submodule)
  -> d (submodule)

This list of submodules is set to grow as time goes on (to a list of 20 or so). We have another module that will include as dependencies all submodules of a. Is there a neat way of doing this, rather than having to manually keep the submodule list in sync with the dependencies list. I.e. is there any way of including a and all submodules as a dependency?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2024-12-18 10:57:35

您在这里有几个选择:

  1. 不改变。列出每个 pom 中的所有依赖项。但是,如果它们有一个共同的父级,您可以在父级 pom 中使用 dependencyManagement 来设置不同依赖项的版本。在子 pom 中,您不需要列出版本。请参阅 Maven 示例中的此部分。这种方法的缺点是您必须一遍又一遍地重新列出相同的依赖项。
  2. 创建一个列出所有共享依赖项的父 pom。请参阅此处的示例。这里的缺点是,当所有想要利用此功能的项目由于某种原因可能需要使用另一个父项目时,您会限制它们使用父项目。
  3. 等待 Maven mixins,我最后听到的是 < a href="https://stackoverflow.com/questions/2456375/how-to-use-maven-3-mixins/2456528#2456528">仍然没有准备好了。
  4. 重新思考你的设计。项目依赖如此多不同的模块和接口有意义吗?为了减少耦合,您可能需要创建一个这些新项目可以使用的接口使用。几个开源多模块项目(例如 Apache Axis2)都遵循这种模式。一个模块包含 20 个依赖项,并公开新模块可以调用的接口。新模块可以仅将一个主模块列为依赖项,然后将所有 20 个依赖项作为传递依赖项拉入。

我认为选择#4可能是正确的,但我对你的情况不够熟悉。

You have a few choices here:

  1. No change. List all dependencies in each pom. However, if they have a common parent, you can use dependencyManagement in the parent pom to set the versions for the different dependencies. In the child poms, you do not need to list the version. See this section from Maven By Example. A downside of this approach is that you have to re-list the same dependencies over and over.
  2. Create a parent pom which lists all shared dependencies. See an example here. A downside here is you are restricting all projects that want to take advantage of this to use a parent project when they may need to use another parent project for some reason.
  3. Wait for Maven mixins, which last I heard were still not ready.
  4. Rethink your design. Does it make sense for projects to depend on so many different modules and interfaces? To reduce coupling, you may want to create one interface that these new projects can use. Several open source multi-module projects, such as Apache Axis2, follow this pattern. One module contains your 20 dependencies and exposes an interface which the new modules can call. The new modules can just list that one main module as a dependency and all of the 20 dependencies are pulled in as transitive dependencies.

I think choice #4 is probably right, but I am not familiar enough with your situation.

卸妝后依然美 2024-12-18 10:57:35

撇开设计考虑不谈,这很容易完成 - 只需在指向父 pom 的依赖项中包含 pom即可。例如:

<dependency>
    <groupId>my.group.id</groupId>
    <artifactId>a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>pom</type>
</dependency>

Design considerations aside, this is easily done - simply include <type>pom</type> in your dependency pointing at the parent pom. E.g:

<dependency>
    <groupId>my.group.id</groupId>
    <artifactId>a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>pom</type>
</dependency>
淡忘如思 2024-12-18 10:57:35

“自动”包含依赖项的唯一方法是通过传递依赖机制,并且它只能通过拉取依赖项的依赖项来工作,因此除非您有一个依赖于所有子模块的 pom,不,如果不列出,您将无法获得它们他们都出去了。需要这样做表明您的项目设计中存在缺陷。如果模块是“全部或无”,那么它们可能不是单独的模块。

The only way dependencies get "automatically" included is via the transitive dependency mechanism, and it only works by pulling in dependencies of dependencies, so unless you have a pom that depends on all the submodules, no, you won't get them without listing them all out. A need to do this points to a flaw in your project design. If the modules are "all or none", then they're probably not separate modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文