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. [16] 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.
The Groovy plugin adds the following tasks to the project.
Table 19.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. |
compile
|
compile
|
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 19.2. Groovy plugin - additional task dependencies
Task name | Depends on |
classes | compileGroovy |
testClasses | compileTestGroovy |
sourceSet Classes |
compileSourceSet Groovy |
The Groovy plugin assumes the project layout shown in Table 19.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. [17] 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 19.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/
|
Java source for the given source set |
src/
|
Resources for the given source set |
src/
|
Groovy source for the given source set. May also contain Java source for joint compilation. |
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 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 19.2. Configuration of Groovy plugin
build.gradle
repositories { mavenCentral() } dependencies { groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.6.0' }
And here is an example using the Groovy JARs checked into the lib
directory of the source
tree:
Example 19.3. Configuration of Groovy plugin
build.gradle
repositories { flatDir(dirs: file('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') } } }
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 17.2, “Using the convention object”).
Table 19.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 14.4, “Specifying a set of files”.
|
[
|
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 19.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. |
The Groovy plugin adds a
GroovyCompile
instance for
each source set in the project. The task type extends the Compile
task (see Section 18.10, “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 19.6. Groovy plugin - CompileGroovy properties
Task Property | Type | Default Value |
classpath
|
FileCollection
|
|
source
|
FileTree
. Can set using anything described in Section 14.4, “Specifying a set of files”. |
|
destinationDir
|
File . |
|
groovyClasspath
|
FileCollection
|
groovy configuration |
[16] 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.
[17] We are using the same conventions as introduced by Russel Winders Gant tool (http://gant.codehaus.org).