TheTrav

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:

<project>
  <!—blah blah —>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.maven</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>whateverYouWant</id>
            <phase>compile</phase> 
            <configuration>
              <executable>dir</executable>
            </configuration>
            <goals>
              <goal>exec</goal> 
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

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.

<project>
  <!— blah blah —>
  <profiles>
    <profile>
      <id>whateverYouLike</id>
      <build>
        <!— insert the plugins block from above —>
      </build>
    </profile>
  </profiles>
</project> 

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:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
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