简体   繁体   English

从父pom继承时管理Maven多模块构建

[英]Managing Maven multimodule build while inheriting from parent pom

In a nutshell, what I am trying to do is build a bunch of libraries and applications, all Maven projects, all at once. 简而言之,我想做的是一次构建所有库和应用程序以及所有Maven项目。 From what I understand a way to accomplish this just in one command line run of mvn package would be to create a multimodule project that will list each module that I would like to build, throw them in the Maven reactor, and build. 据我了解,仅通过mvn package一个命令行运行即可完成此任务的方法是创建一个多模块项目,该项目将列出我要构建的每个模块,并将它们放入Maven反应器中并进行构建。

Following examples in the Maven book it seems that normally a multimodule pom sits in a directory above the individual modules. 遵循Maven书中的示例,似乎通常在单个模块上方的目录中有一个多模块pom However it is also normally the case that a parent pom sits in a directory above the modules, which raises the question, is it normally the case that a multimodule build should also be a parent? 但是,通常情况下,父pom位于模块上方的目录中也会引发问题,通常情况下,多模块构建也应该是父级吗? I think not; 我想不是; however I wonder why I am running into this funny design quirk. 但是我不知道为什么我会遇到这个有趣的设计怪癖。

So, I'm wondering the right way to set this up. 因此,我想知道正确的方法进行设置。 I see the following conventions / requirements: 我看到以下约定/要求:

  • The multimodule pom must have knowledge of where the other modules live on disc. 多模块pom必须了解其他模块在磁盘上的位置。 Since it is actually doing the build from source it can't simply rely on already installed versions (since it's installing them!) 由于它实际上是从源代码进行构建,因此它不能仅仅依赖已安装的版本(因为它正在安装它们!)
  • The parent doesn't actually have to be a physical directory up although that would be preferable. 父级实际上不必一定是物理目录,尽管这样做会更好。 I see this as the convention best to break. 我认为这是打破常规的惯例。
  • Really the individual libraries/application shouldn't even need to know they are being built as part of a multimodule build. 实际上,各个库/应用程序甚至都不需要知道它们是作为多模块构建的一部分而构建的。

How is this usually set up in a multimodule build? 通常如何在多模块构建中进行设置? Is there a simpler way to manage building multiple Maven projects all at once? 有没有一种更简单的方法可以一次管理多个Maven项目?

I put all the individual modules within the root module. 我将所有单个模块放在根模块中。 Some software has trouble with multiple layers of hierarchy. 某些软件在多层层次结构方面存在问题。

To make a child module refer to it's parent on the same level: 要使子模块在同一级别上引用其父模块:

<parent>
    <groupId>com.domain</groupId>
    <artifactId>xyz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../xyz/pom.xml</relativePath>
</parent>

I suggest you do not put anything in the multimodule (eg properties) that individual modules need to inherit. 我建议您不要在单个模块需要继承的多模块中放置任何内容(例如属性)。 If you do, you won't be able to build the other modules independently. 如果这样做,您将无法独立构建其他模块。

I would go so far as to say that this is the fuzzy part of the "conventions". 我什至可以说这是“公约”的模糊部分。 The documentation, and common sense, both suggest that project aggregation ( aka multi-module builds ) and inheritance are two different mechanisms provided to handle different use cases. 该文档和常识均表明,项目聚合(也称为多模块构建)和继承是为处理不同用例而提供的两种不同机制。

At the same time, it seems that there is a de facto convention ( yeah, I know ) of combining both the project aggregation and inheritance parent roles into a single pom. 同时,似乎存在一种事实上的约定(是的,我知道),将项目聚合和继承父角色组合到一个pom中。 In fact, both the element of the parent declaration and the module element of the project aggegration mechanism seem to steer the use toward this combination. 实际上,父声明的元素和项目聚集机制的module元素似乎都在引导这种组合的使用。

Personally, I find it very usefull to separate the parent pom out on a regular basis. 就个人而言,我发现定期分离父pom非常有用。 And I also find it useful ot locate parent pom in a totally separation location in my source control, and thus my folder structure. 而且我还发现将父pom定位在源代码管理中的完全分隔位置以及文件夹结构中很有用。 However, it rarely seems useful to locate builds that are a part of the same multi-module build structure in source control / folder structure. 但是,在源代码管理/文件夹结构中定位属于同一多模块生成结构的一部分的生成似乎很少有用。 Perhaps this is even a good measure of whether something should be included in the same aggregate build; 也许这甚至可以很好地衡量是否应在同一聚合版本中包含某些内容; if it seems to deserve collocation in the source folder structure, then perhaps its a strong candidate for aggregation. 如果它似乎值得在源文件夹结构中并置,那么它可能是进行聚合的强大候选者。

The only thin I am sure of is that these things are worth sorting out a head of time. 我唯一确定的薄弱之处是,这些事情值得花点时间。 And it's probably better to error on the side of not creating monolithic build structures . 在不创建整体式构建结构方面可能更容易出错。 . . it's very hard to deal with a huge lump of aggregated, parent child build modules that isn't really necessary. 真正不需要的大量汇总父级构建模块很难处理。 On the other hand, aggregating individual builds to run together is a functionality provide at higher levels, such as the CI build server. 另一方面,聚合单个构建以一起运行是更高级别提供的功能,例如CI构建服务器。 So, I guess I might suggest erroring on the side of more independence. 因此,我想我可能建议更多独立性方面的错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM