// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[groovy_plugin]]
= The Groovy Plugin

The Groovy plugin extends the Java plugin to add support for Groovy projects. It can deal with Groovy code, mixed Groovy and Java code, and even pure Java code (although we don't necessarily recommend to use it for the latter). The plugin supports _joint compilation_, which allows you to freely mix and match Groovy and Java code, with dependencies in both directions. For example, a Groovy class can extend a Java class that in turn extends a Groovy class. This makes it possible to use the best language for the job, and to rewrite any class in the other language if needed.


[[sec:groovy_usage]]
== Usage

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

.Using the Groovy plugin
====
include::sample[dir="groovy/quickstart/groovy",files="build.gradle[tags=use-plugin]"]
include::sample[dir="groovy/quickstart/kotlin",files="build.gradle.kts[tags=use-plugin]"]
====

[[sec:groovy_tasks]]
== Tasks

The Groovy plugin adds the following tasks to the project.

`compileGroovy` — link:{groovyDslPath}/org.gradle.api.tasks.compile.GroovyCompile.html[GroovyCompile]::
_Depends on_: `compileJava`
+
Compiles production Groovy source files.

`compileTestGroovy` — link:{groovyDslPath}/org.gradle.api.tasks.compile.GroovyCompile.html[GroovyCompile]::
_Depends on_: `compileTestJava`
+
Compiles test Groovy source files.

`compile__SourceSet__Groovy` — link:{groovyDslPath}/org.gradle.api.tasks.compile.GroovyCompile.html[GroovyCompile]::
_Depends on_: `compile__SourceSet__Java`
+
Compiles the given source set's Groovy source files.

`groovydoc` — link:{groovyDslPath}/org.gradle.api.tasks.javadoc.Groovydoc.html[Groovydoc]::
+
Generates API documentation for the production Groovy source files.

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

.Groovy plugin - additional task dependencies
[%header%autowidth,compact]
|===
| Task name | Depends on
| `classes` | `compileGroovy`
| `testClasses` | `compileTestGroovy`
| `__sourceSet__Classes` | `compile__SourceSet__Groovy`
|===

.Groovy plugin - tasks
image::groovyPluginTasks.png[]


[[sec:groovy_project_layout]]
== Project layout

The Groovy plugin assumes the project layout shown in <<#groovylayout,Groovy Layout>>. All the Groovy source directories can contain Groovy _and_ Java code. The Java source directories may only contain Java source code.footnote:[Gradle uses the same conventions as introduced by Russel Winder's link:https://gant.github.io/[Gant tool].] None of these directories need to exist or have anything in them; the Groovy plugin will simply compile whatever it finds.

[[groovylayout]]
include::javaProjectMainLayout.adoc[]

`src/main/groovy`::
Production Groovy source. May also contain Java source files for joint compilation.

include::javaProjectTestLayout.adoc[]

`src/test/groovy`::
Test Groovy source. May also contain Java source files for joint compilation.

include::javaProjectGenericLayout.adoc[]

`src/__sourceSet__/groovy`::
Groovy source files for the given source set. May also contain Java source files for joint compilation.


[[sec:changing_groovy_project_layout]]
=== Changing the project layout

Just like the Java plugin, the Groovy plugin allows you to configure custom locations for Groovy production and test source files.

.Custom Groovy source layout
====
include::sample[dir="groovy/customizedLayout/groovy",files="build.gradle[tags=custom-source-locations]"]
include::sample[dir="groovy/customizedLayout/kotlin",files="build.gradle.kts[tags=custom-source-locations]"]
====

[[sec:groovy_dependency_management]]
== Dependency management

Because Gradle's build language is based on Groovy, and parts of Gradle are implemented in Groovy, Gradle already ships with a Groovy library. Nevertheless, Groovy projects need to explicitly declare a Groovy dependency. This dependency will then be used on compile and runtime class paths. It will also be used to get hold of the Groovy compiler and Groovydoc tool, respectively.

If Groovy is used for production code, the Groovy dependency should be added to the `implementation` configuration:

.Configuration of Groovy dependency
====
include::sample[dir="groovy/quickstart/groovy",files="build.gradle[tags=groovy-dependency]"]
include::sample[dir="groovy/quickstart/kotlin",files="build.gradle.kts[tags=groovy-dependency]"]
====

If Groovy is only used for test code, the Groovy dependency should be added to the `testImplementation` configuration:

.Configuration of Groovy test dependency
====
include::sample[dir="userguide/groovy/groovyDependency/groovy",files="build.gradle[tags=groovy-test-dependency]"]
include::sample[dir="userguide/groovy/groovyDependency/kotlin",files="build.gradle.kts[tags=groovy-test-dependency]"]
====

To use the Groovy library that ships with Gradle, declare a `localGroovy()` dependency. Note that different Gradle versions ship with different Groovy versions; as such, using `localGroovy()` is less safe then declaring a regular Groovy dependency.

.Configuration of bundled Groovy dependency
====
include::sample[dir="userguide/groovy/groovyDependency/groovy",files="build.gradle[tags=bundled-groovy-dependency]"]
include::sample[dir="userguide/groovy/groovyDependency/kotlin",files="build.gradle.kts[tags=bundled-groovy-dependency]"]
====

The Groovy library doesn't necessarily have to come from a remote repository. It could also come from a local `lib` directory, perhaps checked in to source control:

.Configuration of Groovy file dependency
====
include::sample[dir="userguide/tutorial/groovyWithFlatDir/groovy",files="build.gradle[tags=groovy-dependency]"]
include::sample[dir="userguide/tutorial/groovyWithFlatDir/kotlin",files="build.gradle.kts[tags=groovy-dependency]"]
====

[[sec:automatic_configuration_of_groovyclasspath]]
== Automatic configuration of groovyClasspath

The `GroovyCompile` and `Groovydoc` tasks consume Groovy code in two ways: on their `classpath`, and on their `groovyClasspath`. The former is used to locate classes referenced by the source code, and will typically contain the Groovy library along with other libraries. The latter is used to load and execute the Groovy compiler and Groovydoc tool, respectively, and should only contain the Groovy library and its dependencies.

Unless a task's `groovyClasspath` is configured explicitly, the Groovy (base) plugin will try to infer it from the task's `classpath`. This is done as follows:

* If a `groovy-all(-indy)` Jar is found on `classpath`, that jar will be added to `groovyClasspath`.
* If a `groovy(-indy)` jar is found on `classpath`, and the project has at least one repository declared, a corresponding `groovy(-indy)` repository dependency will be added to `groovyClasspath`.
* Otherwise, execution of the task will fail with a message saying that `groovyClasspath` could not be inferred.

Note that the “`-indy`” variation of each jar refers to the version with `invokedynamic` support.

[[sec:groovy_convention_properties]]
== Convention properties

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

[[sec:groovy_source_set_properties]]
== 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.

=== Groovy Plugin — source set properties

`groovy` — link:{groovyDslPath}/org.gradle.api.file.SourceDirectorySet.html[SourceDirectorySet] (read-only)::
_Default value_: 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&lt;File&gt;`::
_Default value_: `[__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. Can set using anything described in <<working_with_files.adoc#sec:specifying_multiple_files,Specifying Multiple Files>>.

`allGroovy` — link:{javadocPath}/org/gradle/api/file/FileTree.html[FileTree] (read-only)::
_Default value_: 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 link:{groovyDslPath}/org.gradle.api.tasks.GroovySourceSet.html[GroovySourceSet].

The Groovy plugin also modifies some source set properties:

=== Groovy Plugin - modified source set properties

[%header%autowidth,compact]
|===
| 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.
|===


[[sec:groovyCompile]]
== GroovyCompile

The Groovy plugin adds a link:{groovyDslPath}/org.gradle.api.tasks.compile.GroovyCompile.html[GroovyCompile] task for each source set in the project. The task type extends the `JavaCompile` task (see <<building_java_projects.adoc#sec:compile, the relevant Java Plugin section>>). The `GroovyCompile` task supports most configuration options of the official Groovy compiler.

.Groovy plugin - GroovyCompile properties
[%header%autowidth,compact]
|===
| Task Property | Type | Default Value

|  `classpath`
| link:{javadocPath}/org/gradle/api/file/FileCollection.html[FileCollection]
| `__sourceSet__.compileClasspath`

|  `source`
| link:{javadocPath}/org/gradle/api/file/FileTree.html[FileTree]. Can set using anything described in <<working_with_files.adoc#sec:specifying_multiple_files,Specifying Multiple Files>>.
| `__sourceSet__.groovy`

|  `destinationDir`
| `File`.
| `__sourceSet__.groovy.outputDir`

|  `groovyClasspath`
| link:{javadocPath}/org/gradle/api/file/FileCollection.html[FileCollection]
| `groovy` configuration if non-empty; Groovy library found on `classpath` otherwise
|===


[[sec:groovy_cross_compilation]]
== Compiling and testing for Java 6 or Java 7

The Groovy compiler will always be executed with the same version of Java that was used to start Gradle. You should set `sourceCompatibility` and `targetCompatibility` to `1.6` or `1.7`. If you also have Java source files, you can follow the same steps as for the <<building_java_projects.adoc#sec:java_cross_compilation,Java plugin>> to ensure the correct Java compiler is used.

=== Example: Configure Java 6 build for Groovy

[source]
.gradle.properties
----
include::{samplesPath}/groovy/crossCompilation/groovy/gradle.properties[]
----

====
include::sample[dir="groovy/crossCompilation/groovy",files="build.gradle[tags=groovy-cross-compilation]"]
include::sample[dir="groovy/crossCompilation/kotlin",files="build.gradle.kts[tags=groovy-cross-compilation]"]
====

