如何在所有模块上运行 Maven 插件?

发布于 2024-11-07 09:34:28 字数 418 浏览 4 评论 0原文

我有一个自定义插件,作为发布:执行目标的一部分在我们所有的项目上运行。

我们刚刚开始使用多模块构建,我注意到我的插件仅在顶部模块运行。我必须对我的插件做什么才能使其在版本结束时单独在所有模块上运行?我是否必须在插件代码本身中迭代它们?如果是这样,是否有这样做的示例,因为从我看来,MavenProject.getModules() 仅返回这些模块的字符串名称列表,并且我看不到如何获取这些模块的信息(我的插件需要每个模块的 groupId:artifactId:version,在这种情况下,模块并不总是具有相同的版本)。

我尝试过使用和不使用@aggregator,但这并没有改变我的问题。

我假设这与直接从 CLI 运行插件(不绑定到某个阶段)的情况相同,它也只在项目的顶层运行,并报告所有子模块的 SKIPPED。

我正在使用 Maven 3.0.3。

I have a custom plugin that I run on all our projects as part of the release:perform goal.

We are just starting to use multi-module builds, and I notice that my plugin only runs at the top module. What do I have to do to my plugin to make it run on all the modules individually at the end of the release? Do I have to iterate through them in the plugin code itself? If so, is there an example of doing that, because from what I see, MavenProject.getModules() just returns a list of String names of those modules, and I can't see how to get info for those modules (my plugin needs the groupId:artifactId:version of each, and in this case, the modules do not always have the same version).

I've tried with and without @aggregator, but that doesn't change anything with respect to my problem.

I'm assuming this is the same case as running the plugin directly (not tied to a phase) from CLI, which also only runs on the top-level of the project, and reports SKIPPED for all the sub-modules.

I am using Maven 3.0.3.

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

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

发布评论

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

评论(3

ゃ懵逼小萝莉 2024-11-14 09:34:28

我的 Maven 插件也有同样的问题。它只被调用一次(通常是针对反应器中的第一个模块),我最终追踪到我的插件具有 Maven 注释“@requiresProject false”。 Maven Reactor 似乎只调用此类插件一次。一旦我切换到“@requiresProject true”,我的魔力就会为每个模块调用一次。我并没有沉迷于release:perform,所以YMMV。

至于访问所有模块,您可以像这样注入它们的 MavenProject 对象,而不是使用给出它们名称的 MavenProject.getModules() :

   /**
    * The projects in the reactor.
    *
    * @parameter expression="${reactorProjects}"
    * @readonly
    */
   private List<MavenProject> reactorProjects;

绑定到生命周期阶段时注意 @aggregator。 “当绑定到生命周期时,聚合器 mojo 可能会产生一些令人讨厌的副作用。它可以强制提前执行...生命周期阶段,并可能导致最终执行...阶段的构建两次。” 参考

你怎么样强制 Maven MOJO 在构建结束时仅执行一次? 有一些 @aggregator 的替代方案。

I had the same problem with my Maven plugin. It was only being invoked once (generally for the first module in the reactor), and I eventually traced it to the fact that my plugin had the Maven annotation "@requiresProject false". It seems that the Maven Reactor only invokes such plugins once. Once I switched to "@requiresProject true", my mojo was invoked once for each module. I wasn't hooking into release:perform, so YMMV.

As for accessing all the modules, instead of using MavenProject.getModules() which gives their names, you could inject their MavenProject objects like this:

   /**
    * The projects in the reactor.
    *
    * @parameter expression="${reactorProjects}"
    * @readonly
    */
   private List<MavenProject> reactorProjects;

Watch out for @aggregator when binding to lifecycle phases. "When bound to a lifecycle, an aggregator mojo can have some nasty side-effects. It can force the execution of the ... lifecycle phase to execute ahead of time, and can result in builds which end up executing the ... phase twice." ref

How do you force a maven MOJO to be executed only once at the end of a build? has some alternatives to @aggregator.

姜生凉生 2024-11-14 09:34:28

使用新的 maven-plugin-annotations 您还可以注入:

@Component
private MavenSession session;

并调用 session.getProjects() (Maven 3) 或 session.getSortedProjects() (2).

Using the new maven-plugin-annotations you can also inject:

@Component
private MavenSession session;

and call session.getProjects() (Maven 3) or session.getSortedProjects() (2).

公布 2024-11-14 09:34:28

由于某种原因,@seanf的答案对我不起作用(也许是因为我使用了更新版本的Maven?!),所以我修改了代码以接受@Parameter注释并且它起作用了!

@Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
private List<MavenProject> reactorProjects;

For some reason @seanf's answer didn't work for me (maybe because I use a much newer version of Maven?!) so I modified the code to accept the @Parameter annotation and it worked !

@Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
private List<MavenProject> reactorProjects;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文