Maven - 跳过父项目构建

发布于 2024-08-06 11:41:34 字数 650 浏览 12 评论 0原文

我知道mauvais ton问两次一天之内,但这里还有另一个 Maven 难题:

我有一个父 POM,它定义了 5 个模块(5 个子项目)。由于每个模块都以完全相同的方式执行,因此我将 部分拉入父 POM 中以消除重复的代码。现在 - 如果我从每个模块单独执行构建,它可以工作,但是如果我想一次构建所有模块并移动到父目录,我会得到错误,因为 Maven 尝试执行的第一件事是父项目本身:

mvn package -P release
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO]   DWD Parent project
[INFO]   Projects

构建之后失败是因为 exec 插件尝试执行不存在的东西。查看输出,很明显反应器插件正在驱动构建,但如何配置反应器以跳过父级?

PS 为了防止混淆 - 我试图抑制父级上的配置文件执行,并在相同构建期间在子级上启用它

I know it's mauvais ton to ask twice in a single day but here's another Maven puzzler:

I have a parent POM which defines 5 modules (5 subprojects). Since each module is executed in exactly the same way I pull <profile><build> section into the parent POM to get rid of the duplicate code. Now - if I execute build individually from each module it works, however if I want to build all modules at once and move to the parent directory I got error since the very first thing Maven tries to execute is the parent project itself:

mvn package -P release
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO]   DWD Parent project
[INFO]   Projects

After that build fails because exec plugin tries to execute something that is not there. Looking at the output it is pretty obvious that reactor plugin is driving the build but how can I configure reactor to skip the parent?

P.S. To prevent confusion - I'm trying to suppress profile execution on parent and enable it on child during the same build

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

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

发布评论

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

评论(5

心意如水 2024-08-13 11:41:34

您无法跳过父构建,但您可以通过一些小技巧将配置文件配置为不被激活。 此答案展示了如何通过以下方式控制配置文件的激活:基于文件的 元素是否存在。通过这种方式,您可以在父级中定义配置文件,但由于标记文件存在于父级中,因此在该项目中将其停用。子项目的源中不会有标记文件,因此将为这些项目激活配置文件。

更新以澄清:在我的测试项目中,此配置意味着配置文件在父项目(在 src/main/resources 中有该文件)中被停用,但在其资源目录中没有该文件的所有子项目中被激活。

<profile>
  <id>test</id>
  <activation>
    <file>
      <missing>src/main/resources/test.marker</missing>
    </file>
  </activation>
  ...
</profile>

You can't skip the parent build, but you can configure the profile to not be activated with a little hack. This answer shows how to control the activation of a profile by the presence or absence of a file-based <activation> element. This way you can define the profile in the parent, but have it deactivated in that project because of the marker file being present in the parent. Child projects would not have the marker file in their source, so the profile would be activated for those projects.

Update to clarify: On my test project this configuration means the profile is deactivated in the parent project (which has the file in src/main/resources), but activated in all child projects which do not have the file in their resources directories.

<profile>
  <id>test</id>
  <activation>
    <file>
      <missing>src/main/resources/test.marker</missing>
    </file>
  </activation>
  ...
</profile>
岁月静好 2024-08-13 11:41:34

构建父级有什么问题?

事实上,在 Maven 中,有两个不同的概念(通常同时使用):

  • 父 POM
  • 模块聚合

第一个是所有子项共有的所有内容的定义。您将父项目定义为 pom 打包的项目,然后可以将其安装在存储库中。

当您构建此父项的子项时,Maven2 将检索此父项以将父项 pom 与子项 pom 合并。您可以通过运行命令mvn help: effective-pom来查看整个pom.xml。

在这种情况下,不会构建父 pom,只会从存储库中检索它。

第二种情况是一个包含子项目的 modules 列表的项目。原则是您在此项目上运行的每个命令也将在所有子模块上运行。模块的顺序将由 Reactor 定义,它将查看模块间的依赖关系,以找出必须在其他模块之前构建哪个模块。如果没有依赖项,那么它将采用父 pom.xml 中定义的模块列表。

在这种情况下,如果您在根项目上运行命令,Maven2 将首先构建根项目,然后构建子模块。您不能跳过根项目的创建。


编辑,感谢RichSeller评论

关于多模块(聚合项目)和继承(父项目)之间差异的完整解释可以在Maven书中找到,此处

What is wrong by building the parent?

In fact, in Maven, there are two different concepts (which are generally used at the same time):

  • The parent POM
  • The modules aggregation

The first one is a definition of everything that is common with all the children. You define a parent as a pom-packaged project, and you can install it in your repository.

When you build a child of this parent, Maven2 will retrieve this parent to merge the parent pom with the child pom. You can have a look to the entire pom.xml by running the command mvn help:effective-pom.

In this case, the parent pom will not be built, it will just be retrieved from the repository.

The second case is a project that contains a modules list of sub-projects. The principle is that every command that you run on this project will also be run on all the sub-modules. The order of the modules will be defined by the Reactor, which will look at inter-modules dependencies to find which module must be built before the others. If there are no dependencies, then it will take the list of the modules as they are defined in the parent pom.xml.

In this case, if you run a command on the root project, Maven2 will first built the root project, and then the sub-modules. You cannot skip the creation of the root project.


Edit, thanks to RichSeller comment

A complete explanation of the differences between multi-modules (aggregation project) and inheritance (parent project) can be found in the Maven book, here.

缱倦旧时光 2024-08-13 11:41:34

我想记录下我的情况有部分妥协(感谢 Maven 用户邮件列表上的建议)。基本上你需要将轮廓切成两块。插件的可重用配置部分转到父POM,而执行保留在子POM中。然后子级中的配置文件插件被标记为继承,瞧 - 在运行时父级配置文件不会被执行,因为它缺少执行部分。这远非理想,但确实有效。请参阅此链接< /a> 例如

I want to document that there's partial compromise to my situation (thanks to guys on maven users mailing list for suggestion). Basically you need to chop profile in two pieces. Reusable configuration section of plugin goes to the parent POM, and executions stays in the child POM. Then the profile plugin in the child is marked as inherited and voila - at run time parent profile is not executed since it's missing executions section. This is far from ideal but it does work. Refer to this link for example

古镇旧梦 2024-08-13 11:41:34

我无法实现上面 Rick Seller 提供的“丢失”文件解决方案。似乎一旦设置了配置文件的活动/非活动状态,即使模块中缺少标记文件,也不会更改。然而,这是我的问题的确切解决方案。警告:这仅从 Maven 2.1+ 开始可用

如果我有一个定义了 2 个模块的父 POM: foo 和 boo 那么在常规情况下执行顺序将是:

  1. parent
  2. foo
  3. boo

要跳过父构建我需要做的就是添加这个命令行开关

mvn install –rf foo

或者您可以使用 --resume-from 它会跳过父级并从 foo 模块继续向下。现在 - 我正在研究是否可以通过配置 Reactor 插件来实现这一点(PS - 不,它不能),但即使使用开关,上述场景对我来说也能正常工作

I wasn't able to implement "missing" file solution as provided by Rick Seller above. It seems that once set active/non-active state of profile will not be changed even the marker file is missing from the module(s). However here's the exact solution to my problem. Warning: this is only available starting with Maven 2.1+

If I have a parent POM with 2 modules defined: foo and boo then in regular circumstances order of execution will be:

  1. parent
  2. foo
  3. boo

All I need to do to skip parent build is to add this command line switch

mvn install –rf foo

Alternatively you can use --resume-from What it will do is to skip parent and continue from foo module down. Now - I'm investigating if this can be achieved by configuring Reactor plugin (P.S. - no it can't) but even with the switch, the above scenario works fine for me

岁月静好 2024-08-13 11:41:34

@romaintaz 对同一问题的回答暗示了这一点,Maven - 跳过父项目构建

但为了明确这一点,在父 pom 中,必须将打包元素指定为 pom(而不是 jar 或 war)。
例子:

<packaging>pom</packaging>

This is implied by @romaintaz's answer to the same question, Maven - skip parent project build

but just to make this explicit, in the parent pom it's imperative that you specify the packaging element as pom (and not jar or war).
Example:

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