Maven 插件目标

发布于 2024-09-04 01:39:45 字数 286 浏览 3 评论 0原文

我正在开发一个 Maven 插件,它将提供 5 个目标。您可以单独执行目标 1-4,也可以执行 goal5,这将按顺序执行目标 1-4。我一直在寻找一种从另一个 Maven 目标中重用(即调用)一个 Maven 目标的方法,但尚未找到。

当然,我可以将 goalX 委托给 ClassX 来实现其大部分功能,然后当调用 goal5 时,它委托给 Class1...Class4,但这在指定、读取和验证方面仍然涉及一定量的代码重复每个目标的配置。

有没有一种方法可以在另一个目标中重用一个目标?

谢谢, 大学教师

I'm developing a Maven plugin that will have provide 5 goals. You can either execute goals 1-4 individually, or execute goal5, which will execute goals 1-4 in sequence. I've been looking for a way to reuse (i.e. invoke) one Maven goal from within another, but haven't found it yet.

Of course, I could just have goalX delegate to ClassX for most of it's functionality, then when goal5 is invoked, it delegates to Class1...Class4, but this still involves a certain amount of code duplication in terms of specifying, reading and validating each goal's configuration.

Is there a way to reuse one goal within another?

Thanks,
Don

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

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

发布评论

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

评论(3

放我走吧 2024-09-11 01:39:45

有没有办法在另一个目标中重用一个目标?

AFAIK,Maven API 没有为此提供任何工具,因为 Maven 人员不想推广导致插件之间强耦合的做法,这被认为是不好的。您可以在回复:在另一个插件中调用插件?中找到相关背景信息。

话虽这么说,这篇博客文章显示如何实例化 Mojo 并在调用执行之前使用反射设置其字段。

您可能还想检查 mojo-executor 库。

但请务必阅读提到的线程,我认为这很重要。

Is there a way to reuse one goal within another?

AFAIK, the Maven API doesn't offer any facility for this because the Maven folks don't want to promote a practice leading to strong coupling between plugins which is considered as bad. You'll find background on that in Re: calling plugin in another plugin?.

That being said, this blog post shows how you could instantiate a Mojo and use reflection to set its field before to call execute.

You might also want to check the mojo-executor library.

But be sure to read the mentioned thread, I think it's important.

梦冥 2024-09-11 01:39:45

当然,我可以将 goalX 委托给 ClassX 来实现其大部分功能,然后当调用 goal5 时,它委托给 Class1...Class4,但这在指定方面仍然涉及一定量的代码重复,读取并验证每个目标的配置。

那么为什么不为其他类提供一个公共类来实现目标验证呢?我认为这里最简单的事情就是让一个目标在代码中调用另一个目标。

Of course, I could just have goalX delegate to ClassX for most of it's functionality, then when goal5 is invoked, it delegates to Class1...Class4, but this still involves a certain amount of code duplication in terms of specifying, reading and validating each goal's configuration.

So then why not provide a common class for your other classes for the purpose of goal validation? I think the easiest thing to do here is to have one goal invoke the other in your code.

梦回梦里 2024-09-11 01:39:45

“Maven 思维方式”似乎是配置是 pom.xml 作者的责任,而不是 Mojo 实现者的责任。如果您将所有配置等移动到一个公共基类中,您最终会绕过此机制。

听起来你想要的是子项目:你的每个目标 1-4 都存在于自己的项目中,或者你可以运行目标 5,它运行所有目标。也许这可能有帮助?: http://i-proving.com/space/Technologies/Maven/Maven+Recipes/Split+Your+Project+Into+Sub-Projects

如果您的源代码树没有沿着项目线很好地分割,您可能会能够使用配置文件做一些事情(尽管我还没有尝试过)。在此处查看已接受的答案:如何绑定插件目标到另一个插件目标

The "Maven mindset" appears to be that configuration is the responsibility of the pom.xml author, not the Mojo implementor. If you move all your configuration and such into a common base class, you end up bypassing this mechanism.

It kind of sounds like what you want are sub-projects: Each of your goals 1-4 live in their own project, or you can run goal 5, which runs them all. Perhaps this might help?: http://i-proving.com/space/Technologies/Maven/Maven+Recipes/Split+Your+Project+Into+Sub-Projects

If your source trees don't split nicely along project lines, you might be able to do something with profiles (though I haven't tried this). Check out the accepted answer here: How to bind a plugin goal to another plugin goal.

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