Reputation: 187379
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
Upvotes: 4
Views: 521
Reputation: 4031
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.
Upvotes: 0
Reputation: 570585
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.
Upvotes: 1
Reputation: 140041
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.
Upvotes: 0