Executing 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. Groovy source can be configured inline from POM configuration, or from an external file or URL source.

1. Hello world Example for Groovy script with Maven


Sample pom file for Groovy Script
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javavillage.groovy</groupId>
	<artifactId>GroovyExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>GroovyExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
<!--		<dependency>-->
<!--			<groupId>org.codehaus.groovy</groupId>-->
<!--			<artifactId>groovy-all</artifactId>-->
<!--			<version>2.3.1</version>-->
<!--		</dependency>-->

	</dependencies>
	<build>
		<plugins>
		<plugin>
			<groupId>org.codehaus.groovy.maven</groupId>
			<artifactId>gmaven-plugin</artifactId>
			<executions>
				<execution>
					<id>call-groovy</id>
					 <phase>prepare-package</phase>
					<goals>
						<goal>execute</goal>
					</goals>
					<configuration>
						<source>src/main/resources/test.groovy</source>
					</configuration>
				</execution>
			</executions>
		</plugin>
		</plugins>
	</build>
</project>


Groovy script file, placed in src/main/resources(test.groovy)
 println "Hello, world Maven with Groovy***********************************************!"

Output

Maven with Groovy execution in Maven install

We did just hello world program using groovy something like display some message.

2. Skip test cases from Groovy script from Maven



Now will try to do some activity on maven build.
Something like control on maven build.
Will pass test skip flag as true from groovy script. For that need to change the phase because flag can be accepted before test case execution. As of now we used phase as prepare-package, this will be before package preparation that means after test case execution.
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javavillage.groovy</groupId>
	<artifactId>GroovyExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>GroovyExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>	
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!--		<dependency>-->
		<!--			<groupId>org.codehaus.groovy</groupId>-->
		<!--			<artifactId>groovy-all</artifactId>-->
		<!--			<version>2.3.1</version>-->
		<!--		</dependency>-->

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18</version>
				<configuration>
					<skipTests>${skip.tests}</skipTests>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.groovy.maven</groupId>
				<artifactId>gmaven-plugin</artifactId>
				<executions>
					<execution>
						<id>call-groovy</id>
						<phase>compile</phase>
						<goals>
							<goal>execute</goal>
						</goals>
						<configuration>
							<source>src/main/resources/test.groovy</source>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Groovy script file, placed in src/main/resources(test.groovy)
println "Entry****"
project.properties.setProperty('skip.tests','true');
println "Exit****"

Output

Result Skip test cases from Groovy script

Note: DOn't specify property value inside properties tag like below. Because variabale value can be override by properties tag, which is initialized by Groovy script.
<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>	
		<skip.tests>false</skip.tests>	
</properties>