共享多个 POM 文件之间的依赖关系

发布于 2024-12-29 00:49:51 字数 224 浏览 2 评论 0原文

跨 Maven 项目共享多个依赖项的最佳实践方法是什么?

我可以想到以下 2 种可能的方法 -

  1. 创建父 POM 文件并在使用依赖项集的每个项目中使用相同的 POM 文件。

  2. 使用包含依赖项的 POM 文件创建新的 Maven 项目。然后在需要公共依赖项的所有项目中引用此项目。

还有其他方法吗?我应该使用哪一种?

What is the best practice method of sharing multiple dependencies across Maven projects.

I can think of the following 2 possible approaches -

  1. Creating a parent POM file and using that same POM file in each project that is using the set of dependencies.

  2. Creating a new maven project with a POM file that contains the dependencies. Then referencing this project within all projects that require the common dependencies.

Are there any other approaches, which one should I use ?

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

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

发布评论

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

评论(5

最单纯的乌龟 2025-01-05 00:49:51

IMO 这在某种程度上取决于您的具体用例。

选项 1 当然是一个好方法,但如果您已经有一个父项目,特别是如果您有多个共享依赖项集,那么管理起来可能会变得更加困难/复杂(您可能会发现自己迷失在多个父项目的复杂层次结构中) )。
选项 1 的另一个缺点是(据我所知)不可能排除继承的依赖项,因此如果您的项目扩展了父 pom,您别无选择,只能继承所有依赖项。

在这种情况下,选项 2 更易于管理、更清晰且更灵活。创建 POM 类型的 Maven 项目,并在需要时添加对此的依赖项。通过此解决方案,可以从 POM 项目中排除某些依赖项,这再次使该解决方案更加灵活。

IMO it depends a bit on your exact use case.

Option 1 is certainly a good way to go but it can become more difficult/complex to manage if you already have a parent project and especially if you have several of those shared dependency sets (you might find yourself lost in a complex hierarchy of multiple parents).
Another disadvantage of option 1 is that (AFAIK) it is not possible to exclude inherited dependencies so if your project extends the parent pom you have no choice but to inherit all the dependencies.

In that case option 2 is easier to manage, clearer and more flexible. Create a maven project of type POM and add a dependency to this where needed. With this solution it is possible to exclude certain dependencies from the POM project which again makes this solution more flexible.

我是男神闪亮亮 2025-01-05 00:49:51

选项 1 效果很好 - 我建议这样做。

Option 1 works well - I'd suggest that.

后eg是否自 2025-01-05 00:49:51

我认为选项 2 更好,因为它是包含继承 问题。

如果您必须在项目之间共享 2 个依赖特征怎么办?使用选项 1,您将陷入单一继承级别,而选项 2 则为您提供了更大的灵活性。

I think option 2 is better, because it's containment vs inheritance issue.

What if you have to share 2 dependency traits among your projects. With option 1 you are stuck at the single inheritance level, while option 2 gives you added flexibility.

酒浓于脸红 2025-01-05 00:49:51

选项 2 - 您需要在 dependencyManagement 部分中通过 import 指定依赖项:

<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>com.test</groupId>
    <artifactId>bom</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>

有关更多信息:http://maven.apache。 org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

OPTION 2 - You'll need to specify the dependency with import on your dependencyManagement section:

<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>com.test</groupId>
    <artifactId>bom</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>

For further info: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

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