Chapter 24. The Groovy Plugin

The Groovy plugin extends the Java plugin to add support for Groovy projects. It can deal with Groovy-only projects and with mixed Java/Groovy projects. It can even deal with Java-only projects. [9] The Groovy plugin supports joint compilation of Java and Groovy. This means that your project can contain Groovy classes which use Java classes, and vice versa.

24.1. Usage

To use the Groovy plugin, include in your build script:

Example 24.1. Using the Groovy plugin

build.gradle

apply plugin: 'groovy'

24.2. Tasks

The Groovy plugin adds the following tasks to the project.

Table 24.1. Groovy plugin - tasks

Task name Depends on Type Description
compileGroovy compileJava GroovyCompile Compiles production Groovy source files using groovyc.
compileTestGroovy compileTestJava GroovyCompile Compiles test Groovy source files using groovyc.
compileSourceSetGroovy compileSourceSetJava GroovyCompile Compiles the given source set's Groovy source files using groovyc.
groovydoc - Groovydoc Generates API documentation for the production Groovy source files using groovydoc.

The Groovy plugin adds the following dependencies to tasks added by the Java plugin.

Table 24.2. Groovy plugin - additional task dependencies

Task nameDepends on
classes compileGroovy
testClasses compileTestGroovy
sourceSetClasses compileSourceSetGroovy

Figure 24.1. Groovy plugin - tasks

Groovy plugin - tasks

24.3. Project layout

The Groovy plugin assumes the project layout shown in Table 24.3, “Groovy plugin - project layout”. All the Groovy source directories can contain Groovy and Java code. The Java source directories may only contain Java source code. [10] None of these directories need exist or have anything in them. The Groovy plugin will compile whatever it finds, and handles anything which is missing.

Table 24.3. Groovy plugin - project layout

Directory Meaning
src/main/java Production Java source
src/main/resources Production resources
src/main/groovy Production Groovy source. May also contain Java source for joint compilation.
src/test/java Test Java source
src/test/resources Test resources
src/test/groovy Test Groovy source. May also contain Java source for joint compilation.
src/sourceSet/java Java source for the given source set
src/sourceSet/resources Resources for the given source set
src/sourceSet/groovy Groovy source for the given source set. May also contain Java source for joint compilation.

24.3.1. Changing the project layout

TBD

Example 24.2. Custom Groovy source layout

build.gradle

sourceSets {
    main {
        groovy {
            srcDir 'src/groovy'
        }
    }
}

24.4. Dependency management

The Groovy plugin adds a dependency configuration called groovy.

Gradle is written in Groovy and allows you to write your build scripts in Groovy. But this is an internal aspect of Gradle which is strictly separated from building Groovy projects. You are free to choose the Groovy version your project should be build with. This Groovy version is not just used for compiling your code and running your tests. The groovyc compiler and the groovydoc tool are also taken from the Groovy version you provide. As usual, with freedom comes responsibility ;). You are not just free to choose a Groovy version, you have to provide one. Gradle expects that the groovy libraries are assigned to the groovy dependency configuration. Here is an example using the public Maven repository:

Example 24.3. Configuration of Groovy plugin

build.gradle

repositories {
    mavenCentral()
}

dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.10'
}

And here is an example using the Groovy JARs checked into the lib directory of the source tree:

Example 24.4. Configuration of Groovy plugin

build.gradle

repositories {
    flatDir { dirs 'lib' }
}

dependencies {
    groovy module(':groovy:1.6.0') {
        dependency('asm:asm-all:2.2.3')
        dependency('antlr:antlr:2.7.7')
        dependency('commons-cli:commons-cli:1.2')
        module(':ant:1.7.0') {
            dependencies(':ant-junit:1.7.0:jar', ':ant-launcher:1.7.0')
        }
    }
}

24.5. Convention properties

The Groovy plugin does not add any convention properties to the project.

24.6. Source set properties

The Groovy plugin adds the following convention properties to each source set in the project. You can use these properties in your build script as though they were properties of the source set object (see Section 21.3, “Conventions”).

Table 24.4. Groovy plugin - source set properties

Property name Type Default value Description
groovy SourceDirectorySet (read-only) Not null The Groovy source files of this source set. Contains all .groovy and .java files found in the Groovy source directories, and excludes all other types of files.
groovy.srcDirs Set<File>. Can set using anything described in Section 16.5, “Specifying a set of input files”. [projectDir/src/name/groovy] The source directories containing the Groovy source files of this source set. May also contain Java source files for joint compilation.
allGroovy FileTree (read-only) Not null All Groovy source files of this source set. Contains only the .groovy files found in the Groovy source directories.

These properties are provided by a convention object of type GroovySourceSet.

The Groovy plugin also modifies some source set properties:

Table 24.5. Groovy plugin - source set properties

Property name Change
allJava Adds all .java files found in the Groovy source directories.
allSource Adds all source files found in the Groovy source directories.

24.7. CompileGroovy

The Groovy plugin adds a GroovyCompile instance for each source set in the project. The task type extends the Compile task (see Section 23.11, “CompileJava”). The compile task delegates to the Ant Groovyc task to do the compile. Via the compile task you can set most of the properties of Ants Groovyc task.

Table 24.6. Groovy plugin - CompileGroovy properties

Task Property Type Default Value
classpath FileCollection sourceSet.compileClasspath
source FileTree. Can set using anything described in Section 16.5, “Specifying a set of input files”. sourceSet.groovy
destinationDir File. sourceSet.output.classesDir
groovyClasspath FileCollection groovy configuration


[9] We don't recommend this, as the Groovy plugin uses the Groovyc Ant task to compile the sources. For pure Java projects you might rather stick with javac. In particular as you would have to supply a groovy jar for doing this.

[10] We are using the same conventions as introduced by Russel Winders Gant tool (http://gant.codehaus.org).