/JavaAndGroovy

Example project using both Groovy and Java interchangeably [maven and gradle]

Primary LanguageGroovy

JavaAndGroovy

Summary

This is an example of how a project can be setup to use Groovy and Java interchangeably. By this I mean that you should be able to use a mixture of Java and Groovy classes and do things like extend Groovy classes from Java classes (and the other way round). I provide an example project configuration for both Gradle and Maven.

Using Gradle

Achieving this in Gradle is very simple. Firstly you need to add the Groovy plugin by including this line in the build.gradle file:

apply plugin: 'groovy'

This plugin extends the Java plugin so by adding this plugin you will be adding tasks for compiling Java and Groovy source files. Below is the default expected source layout [which can be reconfigured]. FYI - The Groovy source folders can contain either Java or Groovy sources but the Java source folders must contain only Java source files.

	src/main/java
	src/main/groovy
	src/main/resources
	src/test/java
	src/test/groovy

Finally you need to add a compile time Groovy dependency to the project, e.g.

compile 'org.codehaus.groovy:groovy-all:2.0.5'

That is pretty much it - here is a full build.gradle example which is a slightly cut down version of the Gradle build config used for this git project:

	apply plugin: 'groovy'

	version = '1.0'

	jar {
		manifest {
			attributes 'Title': 'JavaAndGroovy', 'Version': version
		}
	}

	repositories {
		mavenLocal()
		mavenCentral()
	}

	dependencies {
		compile 'org.codehaus.groovy:groovy-all:2.0.5'
	}

See the Gradle Groovy plugin documentation for further details http://www.gradle.org/docs/current/userguide/groovy_plugin.html

Using Maven

It is slightly trickier to setup the Maven build config. Firstly we need to add a Groovy plugin to the pom file. Note that although the compiler has eclipse in its name there is no requirement to use eclipse [It is simply the same compiler that is used by the eclipse Groovy plugin to compile Groovy source files].

	<plugin>
		<groupId>org.codehaus.groovy</groupId>
		<artifactId>groovy-eclipse-compiler</artifactId>
		<version>2.6.0-01</version>
		<extensions>true</extensions>
	</plugin>

By default the project source folder layout is the same for both Maven and Gradle. Next we need to configure the Maven compiler to use the Groovy compiler plugin as a dependency:

	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.4</version>
		<configuration>
			<compilerId>groovy-eclipse-compiler</compilerId>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
		<dependencies>
			<dependency>
				<groupId>org.codehaus.groovy</groupId>
				<artifactId>groovy-eclipse-compiler</artifactId>
				<version>2.6.0-01</version>
			</dependency>
		</dependencies>
	</plugin>

Finally we need to add the compile time project dependency to the pom [This is what determines which Groovy version gets used]:

	<dependency>
		<groupId>org.codehaus.groovy</groupId>
		<artifactId>groovy-all</artifactId>
		<version>2.0.5</version>
	</dependency>

Here is a full example based on a cut down version of this git project's pom:

	<?xml version="1.0" encoding="UTF-8"?>
	<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</groupId>
		<artifactId>nick</artifactId>
		<version>1.0-SNAPSHOT</version>
		<packaging>jar</packaging>
		<build>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>2.4</version>
					<configuration>
						<compilerId>groovy-eclipse-compiler</compilerId>
						<source>1.6</source>
						<target>1.6</target>
					</configuration>
					<dependencies>
						<dependency>
							<groupId>org.codehaus.groovy</groupId>
							<artifactId>groovy-eclipse-compiler</artifactId>
							<version>2.6.0-01</version>
						</dependency>
					</dependencies>
				</plugin>
				<plugin>
					<groupId>org.codehaus.groovy</groupId>
					<artifactId>groovy-eclipse-compiler</artifactId>
					<version>2.6.0-01</version>
					<extensions>true</extensions>
				</plugin>
			</plugins>
		</build>
		<dependencies>
			<dependency>
				<groupId>org.codehaus.groovy</groupId>
				<artifactId>groovy-all</artifactId>
				<version>2.0.5</version>
			</dependency>
		</dependencies>
	</project>

Not quite as clean as Gradle but it does the same job.

See the project source for a real albeit simplistic example of a project with Java and Groovy classes.