I'm not a cylon!

0 notes &

Maven order of execution

Ok, so I’ve been complaining to my friends about Maven a LOT recently.  It has taken me an embarrassingly long time to grok it even the small amount that I have.

So, basically you get phases in which you can invoke goals, which are where the work happens.  Unusual goals are generally provided by plugins, for example, the maven-exec plugin lets you execute arbitrary commands.

A common thing to want to do, is execute the equivalent of exec:exec right after the compile phase, but before the test phase, and have this happen automagically when you run mvn install.

So to do the execution bit, you declare an execution for the plugin within the build.  I like to think of this as an augmentation to some existing structure.  in your pom.xml it looks like this:

  <!—blah blah —>

Yeah I know, verbose right?  

I’m not a big xml fan either, but if we look at it there, you’re basically saying that our project has a build structure, in that build structure some plugins will be involved, this specific exec-maven-plugin is involved at that compile phase, where we want it to run the exec goal with the given configuration.

My first mental flip was having to recognise that the build doesn’t just have phases and goals grouped together, but spread out between plugins.

So another common thing, is to only want this exec thing to happen under certain conditions.  That’s achievable using a profile.  Profiles can be combined, so there are multiple of them, and can be switched on and off either with the -P flag when invoking mvn or using an <activation> block which lets you check for environment variables etc.

within the profile block, you can put more or less anything you put in the project block and it works kind of like an overrides/extending piece.

  <!— blah blah —>
        <!— insert the plugins block from above —>

So that’s the basics of it… to invoke the build with the above profile, because there’s no activation block, you’d have to invoke mvn install -P whateverYouLike

Getting into activations and lots of profiles, some turned on and some turned off is where things get a bit more complicated, but with the above starting blocks I’ve found the rest a lot more approachable.

It’s actually described in the maven docs at:
however for some reason I found that to be fairly difficult to grok… maybe it’s the higher level build construct with overrides that I needed to get to make it all fit.

Anyway, hopefully this helps someone else out there as confused as I was 

Filed under maven dev build