Search

Dark theme | Light theme

August 23, 2009

Grassroots Groovy: the Maven Route (part 2)

We can introduce Groovy into our projects at grassroots level. In a a previous blog we learned how we could add Groovy code to a Maven based Java project. In this blog post we see how we can use the GMaven plugin to add Groovy code to the Maven build process itself. Sometimes we want a bit more functionality than Maven supports by default. We could write our own Maven plugin to get this extra functionality, but sometimes we can get away with a little scripting. And for this scripting we use Groovy of course.

To execute Groovy code from Maven we only have to define the GMaven plugin in our POM. And then we write Groovy code in the POM itself to be executed, or from a local file, of from an URL. The Groovy code can access a couple of variables:

  • project: points to the Maven project and we can access everything.
  • pom: just another name for the project variable.
  • session: the current Maven session.
  • settings: the user's global settings.
  • log: the SLF4J logger instance to log messages to the console.
  • ant: a reference to an AntBuilder instance to execute Ant tasks.
  • fail(): a helper method to throw a MojoExecutionException.

Let's add the Maven plugin to our POM with a reference to a local Groovy script file:

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mrhaki.sample</groupId>
    <artifactId>sample-app</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>sample-app</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <!-- GMaven plugin so we can run Groovy scripts. -->
            <plugin>
                <groupId>org.codehaus.groovy.maven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <!-- Script will be executed in 
                                the generate-resources phase. -->
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>${pom.basedir}/src/main/scripts/MavenHelper.groovy</source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <!-- Extra profile to be used in Groovy script as an example. -->
        <profile>
            <id>dev</id>
        </profile>
    </profiles>
</project>

Next we create the Groovy script file in src/main/scripts. In this script we look for active profiles, and if we have a match for a profile with id dev we log some extra info and use the Antbuilder to copy a properties file:

// Get list of active profiles.
def profiles = project.getActiveProfiles()

profiles.each{ profile ->
    // Show off Groovy's switch for matching on strings.
    switch(profile.id) {
        case 'dev':
            // Show some extra logging info.
            log.info "*" * 70
            log.info "* You are running in DEVELOPMENT mode."
            log.info "* Version: ${pom.version}, Finalname: ${pom.build.finalName}"
            log.info "*" * 70
            
            // Using Antbuilder to copy properties file.
            ant.copy(file: 'src/main/resources/dev.properties', tofile: project.build.outputDirectory + '/app.properties')
            break;
        default:
            break;
    }
}

To execute Maven with the dev profile we run the following command: $ mvn install -Pdev. In the logging generated by Maven we get the following output:

[INFO] [groovy:execute {execution: default}]
[INFO]  **********************************************************************
[INFO]  * You are running in DEVELOPMENT mode.
[INFO]  * Version: 1.0-SNAPSHOT, Finalname: sample-app-1.0-SNAPSHOT
[INFO]  **********************************************************************
Copying 1 file to P:\samples\sample-app\target\classes

So we learned how to add Groovy scripts to our Maven build. The Groovy website contains more information about all the configuration we can do to use the GMaven plugin like this.