简体   繁体   English

如何在Maven中处理子项目依赖项

[英]How to handle sub projects dependencies in Maven

My project is made of 5 sub projects. 我的项目由5个子项目组成。 One is a War, and the other 4 are jars. 一个是战争,另外四个是罐子。 Basically the war project needs all 4 jar projects, and their dependencies. 基本上,war项目需要所有4个jar项目及其依赖项。

I can strip down the dependencies to have something like war->A->B->C->D. 我可以删除依赖项以获得类似war-> A-> B-> C-> D的内容。 Every sub project add their share of external dependencies (spring, struts, hibernate) so that in the end the war gets everything needed to run. 每个子项目都会添加它们的外部依赖项(spring,struts,hibernate),这样战争最终会得到运行所需的一切。

This looks pretty well organised and square, but then I ask myself if this is very practical to make changes. 这看起来非常有条理和方正,但后来我问自己,这是否非常实用,可以进行更改。

Imagine I have to change one line of code in project D, without changing anything to its Maven dependencies. 想象一下,我必须在项目D中更改一行代码,而不更改其Maven依赖项。 I would have to re-release project D obviously, but then I have to re-release projects C, B, A, and the war just to reflect this change in their pom files. 我显然必须重新发布项目D,但后来我必须重新发布项目C,B,A和战争只是为了反映他们的pom文件中的这一变化。 This can be long and annoying, especially if you have to quickly release a new version to fix something in production. 这可能很长而且很烦人,特别是如果您必须快速发布新版本以修复生产中的某些内容。

I could make the war depend on all 4 projects, so then I just have to change project D version number in the war pom file. 我可以让战争依赖于所有4个项目,所以我只需要在war pom文件中更改项目D版本号。 But then I have project A depending indirectly on project D 1.0 and the war specifying project D 1.1. 但后来我有项目A间接取决于项目D 1.0和战争指定项目D 1.1。 I think that the war direct dependency would win in that case wouldn't it ? 我认为战争直接依赖会赢得那种情况不会吗?

This would make the new war release quicker, but it would also mess my sub projects dependencies, as they would be outdated. 这将使新的战争版本更快,但它也会弄乱我的子项目依赖项,因为它们将过时。

What would be an acceptable way to handle this situation ? 处理这种情况的可行方法是什么?

There is no simple answer to your problem. 你的问题没有简单的答案。

If you indeed do have a chain of transitive dependencies (A->B->C->D), then releasing each modules up the chain independently is not a bad option. 如果你确实有一系列传递依赖(A-> B-> C-> D),那么独立释放链中的每个模块并不是一个糟糕的选择。 Although it is tedious, there is a good chance your nested dependencies are simple lib jars and will not see changes too often. 虽然它很繁琐,但您的嵌套依赖项很可能是简单的lib jar,并且不会经常看到更改。 Hopefully you will not be forced to go through that process frequently. 希望你不会被迫经常经历这个过程。 Pretend it would be the same situation as if log4j was updated and all of your modules needed to be updated as well. 假设它与更新log4j并且所有模块也需要更新的情况相同。

Another thing to consider is your WAR's dependencies. 另一件需要考虑的事情是你的WAR依赖。 Yes, Maven will pull dependencies in automatically for you but it is often a good practice to declare your known dependencies explicitly so you can specify a version number yourself for each module. 是的,Maven将自动为您提取依赖关系,但明确声明已知的依赖关系通常是一个好习惯,因此您可以为每个模块自己指定版本号。 This would mean A depends on D and the others directly. 这意味着A直接取决于D和其他人。 Unfortunately, if you have conflicting version numbers, as you've described, then you are looking for trouble on your classpath. 不幸的是,如果您的版本号有冲突,正如您所描述的那样,那么您正在寻找类路径上的问题。 If you really need to do this though, maven does allow you exclude transitive dependencies explicitly: 如果你真的需要这样做,maven确实允许你明确地排除传递依赖:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>my.project</groupId>
      <artifactId>module-B</artifactId>
      <version>1.0</version>
      <exclusions>
        <exclusion>
          <groupId>my.project</groupId>
          <artifactId>module-C</artifactId>
        </exclusion>
        <exclusion>
          <groupId>my.project</groupId>
          <artifactId>module-D</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>my.project</groupId>
      <artifactId>module-C</artifactId>
      <version>1.0</version>
      <exclusions>
        <exclusion>
          <groupId>my.project</groupId>
          <artifactId>module-D</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>my.project</groupId>
      <artifactId>module-D</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  ...
</project>

Here is the documentation describing these optional dependencies and exclusions. 以下是描述这些可选依赖项和排除项的文档

Do you actually need to release B, C, and D independently? 你真的需要独立发布B,C和D吗? If not, consider using an Aggregator pom.xml file at the root of your modules. 如果没有,请考虑在模块的根目录中使用Aggregator pom.xml文件。 This will allow you to use SNAPSHOT versions throughout your modules and then release the bunch at once. 这将允许您在整个模块中使用SNAPSHOT版本,然后立即释放这些版本。 This is the way our team manages our multi-module project. 这是我们团队管理多模块项目的方式。 Using SNAPSHOT dependencies ensures you use the version that was JUST built when those artifacts are needed. 使用SNAPSHOT依赖项可确保您使用在需要这些工件时刚刚构建的版本。

The best answer these days is now use gradle which is best of ant and maven. 这些天最好的答案现在使用gradle,这是最好的蚂蚁和maven。 I never really liked maven but gradle took alot of the common concepts but made it more like ant in that it is flexible so that there is no an easy answer to your question in gradle ;). 我从来没有真正喜欢过maven,但是gradle采用了很多常见的概念,但更像是蚂蚁,因为它很灵活,所以在gradle中没有一个简单的问题答案;)。

Do you actually release any of projects A to D independently, without the WAR? 在没有WAR的情况下,您是否实际独立地发布任何项目A到D? If not, I don't see any problems with your current setup. 如果没有,我认为您当前的设置没有任何问题。 You should absolutely use the same version of any module throughout the project. 您应该在整个项目中绝对使用相同版本的任何模块。 Otherwise you open the door to classloader hell - believe me, you don't want to get there :-( 否则你打开了装载机地狱的大门 - 相信我,你不想到达那里:-(

To make releases easier, the maven-release-plugin may help you. 为了使发布更容易, maven-release-plugin可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven:如何处理子项目上的本地文件以进行测试 - Maven: How to handle local files on sub projects for tests Maven:如何处理冲突的依赖项 - Maven: how to handle conflicting dependencies Maven 依赖项以及它们在 Java 项目中的实际工作方式 - Maven dependencies and how they actually work in Java projects IDEA如何处理Maven项目中的依赖关系? - How does IDEA handle dependencies in maven project? Eclipse 运行配置:如何添加已添加到 Classpath 的项目的 Maven 依赖项 - Eclipse Run Configurations: how to add Maven dependencies of projects added to Classpath 如何在Maven中的多个项目之间共享项目依赖项? - How to share project dependencies among multiple projects in Maven? 所有项目的Maven依赖关系直方图(使用频率) - Histrogram of maven dependencies over all projects (What is used how often) 如何在Eclipse中的Maven项目之间设置“构建”依赖关系? - How to set “build” dependencies between Maven projects in Eclipse? 如何在全球范围内管理不同Maven项目之间的依赖关系? - How do to manage Dependencies between different maven projects, globally? 如何在两个项目中的测试文件夹之间设置Maven依赖项? - How to setup Maven dependencies between test folders in two projects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM