Jump to content

Running an In-line Groovy Script in a Maven Build

+ 1
  tmo9d's Photo
Posted Sep 30 2009 12:32 PM

You need to run some Groovy script as a part of your build process.

Put some Groovy in your project's POM like this:

<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>org.sonatype.mhandbook</groupId>
  <artifactId>groovy-script</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>groovy-script</name>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-model</artifactId>
      <version>2.2.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.groovy.maven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <executions>
          <execution>
            <id>groovy-magic</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>
                def depFile = new File(project.build.outputDirectory, 'deps.txt')
                project.dependencies.each() {
                  depFile.write("${it.groupId}:${it.artifactId}:${it.version}")
                }
                ant.copy(todir: project.build.outputDirectory ) {
                  fileset(dir: project.build.sourceDirectory)
                }
              </source>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>


This example configures the GMaven Plugin's execute goal to execute during the prepare-package phase. The source configuration supplies a Groovy script which is run during the execution of the execute goal. Also note that the execution has an id element with a value of groovy-magic. This id element isn't required if you are only configuring one execution for a plugin, but it is necessary once you define more than one execution for a given plugin.

The Groovy script included, creates a file named "deps.txt" in ${basedir}/target/classes, it then iterates through the project's declared dependencies, and then it copies all of the source in ${basedir}/src/main/java into the ${basedir}/target/classes directory. This script demonstrates the use of Groovy's closure syntax and ease with which you can manipulate the filesystem. Groovy is an ideal scripting language for manipulating text and working with files. To test this script example, run mvn package as shown below.

~/examples/groovy/groovy-script $ mvn package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building groovy-script
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resource
s, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ~/examples/groovy/groovy-script/src/
main/resources
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resource
s, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ~/examples/groovy/groovy-script/src/
test/resources
[INFO] [compiler:testCompile]
[INFO] Not compiling test sources
[INFO] [surefire:test]
[INFO] Tests are skipped.
[INFO] [groovy:execute {execution: default}]
[INFO] [jar:jar]
[INFO] Building jar: ~/examples/groovy/groovy-script/target/groovy-script-1.0-S
NAPSHOT.jar


After running this example, list the contents of ${basedir}/target/classes/deps.txt, and you should see the following contents:

~/examples/groovy/groovy-script $ more deps.txt 
org.apache.maven:maven-model:2.2.0


This simple Groovy script contained a number of references to implicit objects such as project and ant, it also demonstrated the simple syntax of Groovy and its closure-friendly nature of Groovy.

Cover of Maven: The Definitive Guide
Learn more about this topic from Maven: The Definitive Guide. 

Written by Maven creator Jason Van Zyl and his team at Sonatype, Maven: The Definitive Guide clearly explains how this popular tool can bring order to your software development projects. The first part of the book demonstrates Maven's capabilities through the development of several sample applications from ideation to deployment, and the second part offers a complete reference guide. Concise and to the point, this is the only guide you need to manage your project.

Learn More Read Now on Safari


Tags:
1 Subscribe


0 Replies