Java 如何使用父模块进行子模块的 maven 构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27689425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:51:34  来源:igfitidea点击:

How to make maven build of child module with parent module?

javamavenpom.xml

提问by scott miles

I have multiple modules in my project and they are dependent on each other either directly or transitively. When I maven build ?Project A? some where ?Project D? gets build automatically.

我的项目中有多个模块,它们直接或传递地相互依赖。当我 maven build 时?Project A?一些在哪里?项目D?自动构建。

Project A > Project B  > Project C > Project D

where > means Project B depends on Project A

?Project D? pom snipeet is:

?项目D?pom snipeet 是:

<project xmlns="...">
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.myProduct</groupId>
    <artifactId>build-MyProjectD</artifactId>
    <name>MyProjectD</name>
    ........
</project>

As building ?Project A? automatically build ?Project B?, as per my understanding to make this happen somewhere build-MyProjectDshould be added as dependency in one of these projects Project A > Project B > Project Cbut I did not find any reference of string build-MyProjectDunder poms of these projects.

作为建筑?项目A?自动构建?项目 B?,根据我的理解,要在某处发生这种情况,build-MyProjectD应该将其添加为这些项目之一中的依赖项,Project A > Project B > Project C但我没有build-MyProjectD在这些项目的 poms 下找到任何字符串引用。

Any idea how is there any other way to make build of child module (in this case ?Project D?) without having child artifactIdpresence in upstream project?

知道如何artifactId在上游项目中没有子模块的情况下构建子模块(在这种情况下?项目 D?)?

采纳答案by Guillaume Polet

You need to create an aggregator project. See the link for more information on the aggregation concept.

您需要创建一个聚合器项目。有关聚合概念的更多信息,请参阅链接

Basically, you create a parent project containing several "modules". When building the parent, the modules automatically gets built as well.

基本上,您创建一个包含多个“模块”的父项目。在构建父模块时,模块也会自动构建。

If you declare dependencies between the modules, Maven will automatically build the different modules in a correct order so that if ?Project A? depends on ?Project B?, ?Project B? is built first and then ?Project A? is built so that its artifact is available for the building of the second artifact.

如果你声明了模块之间的依赖关系,Maven 会自动按照正确的顺序构建不同的模块,这样如果 ?Project A? 取决于?项目 B?,?项目 B?先建然后再建?项目A?被构建以便其工件可用于构建第二个工件。

See also this question from the Maven's FAQ.

另请参阅Maven 常见问题解答中的此问题

回答by xyz

For a parent project Maven will build all child modules while building parent project. Add the modules to your parent pom. Assuming Ais your parent project

对于父项目,Maven 将在构建父项目的同时构建所有子模块。将模块添加到您的父 pom.xml 文件中。假设A是你的父项目

  <modules>
    <module>projectB</module>
    <module>projectC</module>
    <module>projectD</module>
  </modules>

and in modules (B,C and D), add project A as parent (This is optional, Thanks @Guillaume Polet)

并在模块(B、C 和 D)中,将项目 A 添加为父项(这是可选的,谢谢@Guillaume Polet

 <parent>
   <groupId>foo.bar</groupId>
   <artifactId>ProjectA</artifactId>
   <version>1.0-SNAPSHOT</version>
 </parent>

So if you build projectA, it will build ProjectB, ProjectC and ProjectD. Also maven is smart enough to figure out correct build order for B,C and D.

因此,如果您构建 projectA,它将构建 ProjectB、ProjectC 和 ProjectD。maven 也足够聪明,可以为 B、C 和 D 找出正确的构建顺序。