4

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

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • Will this solution help? http://stackoverflow.com/questions/1393691/how-to-bind-a-plugin-goal-to-another-plugin-goal – JoseK Jun 04 '10 at 11:51
  • @josek - no, my question is about how to invoke a goal programatically from with the source code of another plugin goal – Dónal Jun 04 '10 at 12:10

3 Answers3

1

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
  • 562,542
  • 136
  • 1,062
  • 1,124
0

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
  • 138,234
  • 66
  • 282
  • 345
  • But my problem is that I don't know how to invoke one goal from another – Dónal Jun 04 '10 at 12:39
  • What I am saying is that you should just call the code that handles goal1 from within the code that handles code5. – matt b Jun 04 '10 at 13:56
0

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.

Community
  • 1
  • 1
Curtis
  • 3,931
  • 1
  • 19
  • 26