We have seen how to use Gradle for projects with Java code. Gradle has support for other languages as well. In the last couple of years, other languages for JVM have emerged. In this chapter, we will take a look at Gradle's support for Groovy and Scala languages. Both languages are supported by JVM.
We will see how to apply the correct plugin and configuration to our Gradle build files to work with the different languages.
Gradle also supports C. The C plugin adds support to compile source files. JavaScript and Closure plugins are available as third-party plugins, which add support for these languages. We will not cover this support in this book. We will focus on the JVM languages-Groovy and Scala.
To use Groovy sources in our project, we can apply the Groovy plugin. The Groovy plugin makes it possible to compile Groovy source files to class files. The project can contain both Java and Groovy source files. The compiler that Gradle uses is a joint compiler that can compile Java and Groovy source files.
The plugin also adds new tasks to our build. To compile the Groovy source files, we can invoke the compileGroovy
task. Test sources written in Groovy can be compiled with the compileTestGroovy
task. Also, a compile<SourceSet>Groovy
task is added for each extra source set in our build definition. So, if we create a new source set with the name api
, there will be a compileApiGroovy
task.
In the following example build file, we apply the Groovy plugin:
apply plugin: 'groovy'
If we invoke the tasks
task to see what is available, we get the following output:
$ gradle tasks :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. classes - Assembles main classes. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles test classes. Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Documentation tasks ------------------- groovydoc - Generates Groovydoc API documentation for the main source code. javadoc - Generates Javadoc API documentation for the main source code. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'groovy'. components - Displays the components produced by root project 'groovy'. [incubating] dependencies - Displays all dependencies declared in root project 'groovy'. dependencyInsight - Displays the insight into a specific dependency in root project 'groovy'. help - Displays a help message. model - Displays the configuration model of root project 'groovy'. [incubating] projects - Displays the sub-projects of root project 'groovy'. properties - Displays the properties of root project 'groovy'. tasks - Displays the tasks runnable from root project 'groovy'. Verification tasks ------------------ check - Runs all checks. test - Runs the unit tests. Rules ----- Pattern: clean<TaskName>: Cleans the output files of a task. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration. To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task> BUILD SUCCESSFUL Total time: 1.378 secs
Note that we also got all the tasks from the Java plugin. This is because the Groovy plugin automatically includes the Java plugin. So, even though we only defined the Groovy plugin in our build file, the Java plugin is applied as well.
The extra compileGroovy
and compileTestGroovy
tasks are visible in the command output. The new tasks are dependency tasks for the classes
and testClasses
tasks. If we invoke the classes
task, the compileGroovy
task is also executed.
The plugin adds the groovy configuration. The Groovy compiler uses this configuration. Therefore, to compile Groovy source files in our project, we must set a dependency on the compile
configuration.
To compile Groovy source files, we must add a dependency with the Groovy library that we want to use to the compile
configuration. We might expect that Gradle will use the Groovy version that is used by Gradle, but the compilation task is independent of the Groovy version used by Gradle. We have to define the Groovy library ourselves.
It is good to be independent of the Groovy libraries shipped with Gradle as we can then use the Groovy version we really need. When we want to use the Groovy libraries shipped with Gradle, we can use the localGroovy()
special dependency. For a normal Groovy project, this is not advised; but for plugin development, it is useful.
First, we create a Groovy source file so that we can compile it with Gradle. The default source directory for Groovy source files is src/main/groovy
. Let's create a new file in the src/main/groovy/gradle/sample
directory, with the name Sample.groovy
. The following code shows the contents of this file:
// File: src/main/groovy/gradle/sample/Sample.groovy package gradle.sample import groovy.transform.ToString @ToString class Sample { String name }
Next, we create a Gradle build file and apply the Groovy plugin. We add the Bintray JCenter repository and a Groovy dependency to the compile
configuration, as follows:
apply plugin: 'groovy' repositories { jcenter() } dependencies { // Define dependency for Groovy libraries. compile group: 'org.codehaus.groovy', name: 'groovy', version: '2.4.5' }
When we run the build
task, we get the following output:
$ gradle build :compileJava UP-TO-DATE :compileGroovy :processResources UP-TO-DATE :classes :jar :assemble :compileTestJava UP-TO-DATE :compileTestGroovy UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build BUILD SUCCESSFUL Total time: 6.081 secs
When we don't have the specified Groovy library in our cache, it is downloaded by Gradle from the Bintray JCenter repository. The source code file is compiled, and if we look in the build/classes
directory, we can see the compiled class file.
The Groovy plugin also adds new source set properties. The following table shows the extra properties:
Property name |
Type |
Description |
|
|
These are the Groovy source files for this project. This contains both |
|
|
These are the directories with the Groovy source files. They can also contain Java source files for joint compilation. |
|
|
These consist of only the Groovy source files. All files with extension |
We extend our previous build file and add the groovySourceSetsProperties
task. We print the extra properties and their values with this task. The build now looks similar to the following code:
apply plugin: 'groovy' repositories { jcenter() } dependencies { // Use String notation to define dependency. compile 'org.codehaus.groovy:groovy:2.4.5' } // New task to print out source set properties // added by the Groovy plugin. task groovySourceSetProperties << { sourceSets.main.with { sourceSet -> println "groovy.srcDirs = ${sourceSet.groovy.srcDirs}" println "groovy.files = ${sourceSet.groovy.files.name}" println "allGroovy.files = ${sourceSet.allGroovy.files.name}" } }
When we run the groovySourceSetProperties
task on the command line, we see the following output:
$ gradle groovySourceSetProperties :groovySourceSetProperties groovy.srcDirs = [/gradle-book/samples/Chapter8/Code_Files/groovy/src/main/groovy] groovy.files = [Sample.groovy] allGroovy.files = [Sample.groovy] BUILD SUCCESSFUL Total time: 0.729 secs
When our Java code uses Groovy classes, and vice versa, we can use the joint compilation feature. We must make sure that both Java and Groovy source files are in the src/main/groovy
directory.
The Groovy plugin also adds the groovydoc
task. The groovydoc
task is like the javadoc
task from the Java plugin. Gradle uses the GroovyDoc
tool, which is available from the Groovy version that we have defined as a dependency of the compile
configuration.
The task has several properties that we can change. For example, we can set the header and footer to be used in the generated documentation.
In the following build file, we will configure the groovydoc
task:
apply plugin: 'groovy' // Set version for project, we use // it in the configuration for groovydoc. version = 1.0 repositories { jcenter() } dependencies { compile 'org.codehaus.groovy:groovy:2.4.5' } // Configure groovydoc task. groovydoc { header = 'GroovyDoc for sample project' footer = "Generated documentation - $version" docTitle = 'GroovyDoc Title' windowTitle = docTitle use = true // Create class and package usage pages // Exclude files, use include to include files exclude '**/*Doc.groovy' }
When we run the groovydoc
task, we can see the generated documentation in the build/docs/groovydoc
directory. We must open the index.html
file in our web browser to see the result.
3.147.83.176