# Chapter 17. The Groovy Plugin

The Groovy Plugin extends the Java Plugin. It can deal with pure Java projects, [17] with mixed Java and Groovy projects, and with pure Groovy projects.

## 17.1. Tasks

The Groovy plugin does not add any tasks. It modifies some of the tasks of the Java Plugin.

## 17.2. Project layout

The Groovy plugin assumes the project layout shown in Table 17.1, “Groovy plugin - project layout”. All the Groovy s ource directories can contain Groovy and Java code. The Java source directories may only contain Java source code (and can of course be empty). [18]

Table 17.1. Groovy plugin - project layout

Directory | Meaning
`src/main/groovy` | Application/Library Groovy/Java source
`src/test/groovy` | Test Groovy/Java source

## 17.3. 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 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 17.1. Configuration of Groovy plugin

`build.gradle`

    repositories {
        mavenCentral()
    }

    dependencies {
        groovy group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.6.0'
    }

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

Example 17.2. Configuration of Groovy plugin

`build.gradle`

    repositories {
        flatDir(dirs: file('lib'))
    }

    dependencies {
        groovy module(':groovy-all:1.6.0') {
            dependency(':commons-cli:1.0')
            module(':ant:1.7.0') {
                dependencies(':ant-junit:1.7.0:jar', ':ant-launcher:1.7.0')
            }
        }
    }

## 17.4. Convention properties

The Groovy plugin adds the convention properties shown in Table 17.2, “Groovy plugin - directory properties” and Table 17.3, “Groovy Plugin - floating directory properties”.

Table 17.2. Groovy plugin - directory properties

Dir Name | Dir File | Default Value Name | Default Value File
groovySrcDirNames | groovySrcDirs | [`main/groovy`] | [``srcRoot`/main/groovy`]
groovyTestSrcDirNames | groovyTestSrcDirs | `test/groovy` | [``srcRoot`/test/groovy`]
groovydocDirName | groovydocDir | `groovydoc` | ``docsDir`/groovydoc`

Table 17.3. Groovy Plugin - floating directory properties

Property | Type | Default Value
floatingGroovySrcDirs | List | empty
floatingGroovyTestSrcDirs | List | empty

## 17.5. Compile

The GroovyCompile task has two instances, `compile` and `compileTests`. The task type extends the `Compile` task (see Section 16.8, “Compile”)

Table 17.4. Groovy Convention Object - source directory properties

Task Instance | Task Property | Convention Property
compile | groovySourceDirs | groovySrcDirs
compileTests | groovySourceDirs | groovyTestSrcDirs

Have a look at `GroovyCompile` to learn about the details. 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.

## 17.6. Test

In contrast to the Java plugin the fork mode is set to once by default, because of the significant startup time of Groovy. The Java plugin uses per test as fork mode (see Section 16.9, “Test”).

[17] 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 pure `javac`. In particular as you would have to supply a groovy jar for doing this.

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