views:

77

answers:

3
+4  Q: 

maven plugin goals

Hi,

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

A: 

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.

matt b
But my problem is that I don't know how to invoke one goal from another
Don
What I am saying is that you should just call the code that handles goal1 from within the code that handles code5.
matt b
+1  A: 

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.

Pascal Thivent
A: 

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.ca/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: http://stackoverflow.com/questions/1393691/how-to-bind-a-plugin-goal-to-another-plugin-goal.

Curtis