Jump to content

Running an External Groovy Script in a Maven Build

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

You need to execute one or more groovy scripts in a Maven build.

Configure the execute goal of the GMaven plugin, reference the Groovy script in the source configuration for the execution. The following POM configures two executions of the execute goal referencing two scripts stored in ${basedir}/src/main/groovy.

<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-ex</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>groovy-script-ex</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>create-deps-file</id>
            <phase>process-classes</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>${basedir}/src/main/groovy/CreateDeps.groovy</source>
            </configuration>
          </execution>
          <execution>
            <id>copy-the-source</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <source>${basedir}/src/main/groovy/CopySource.groovy</source>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>


The CreateDeps.groovy script creates a file named deps.txt in ${basedir}/target/classes which contains a list of direct project dependencies, and the CopySource.groovy script copies the source from ${basedir}/src/main/java to ${basedir}/target/classes.

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)
}


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