Maven 插件目标
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK,Maven API 没有为此提供任何工具,因为 Maven 人员不想推广导致插件之间强耦合的做法,这被认为是不好的。您可以在回复:在另一个插件中调用插件?中找到相关背景信息。
话虽这么说,这篇博客文章显示如何实例化 Mojo 并在调用执行之前使用反射设置其字段。
您可能还想检查 mojo-executor 库。
但请务必阅读提到的线程,我认为这很重要。
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.
那么为什么不为其他类提供一个公共类来实现目标验证呢?我认为这里最简单的事情就是让一个目标在代码中调用另一个目标。
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.
“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.