Chapter 8. Mixed Languages

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 Java Virtual Machine have emerged. In this chapter, we will take a look at Gradle's support for the Groovy and Scala languages. Both languages are supported by Java Virtual Machine.

We will see how we can 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 those languages. We will not cover this support in this book. We will focus on the JVM languages—Groovy and Scala.

Using the Groovy plugin

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 --all
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles all Jar, War, Zip, and Tar archives. [jar]
build - Assembles and tests this project. [assemble, check]
buildDependents - Assembles and tests this project and all projects that depend on it. [build]
buildNeeded - Assembles and tests this project and all projects it depends on. [build]
classes - Assembles the main classes.
    compileGroovy - Compiles the main Groovy source.
    compileJava - Compiles the main Java source.
    processResources - Processes the main resources.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes. [classes]
testClasses - Assembles the test classes. [classes]
    compileTestGroovy - Compiles the test Groovy source.
    compileTestJava - Compiles the test Java source.
    processTestResources - Processes the test resources.

Documentation tasks
-------------------
groovydoc - Generates Groovydoc API documentation for the main source code. [classes]
javadoc - Generates Javadoc API documentation for the main source code. [classes]

Help tasks
----------
dependencies - Displays the dependencies of root project 'groovy'.
help - Displays a help message
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' (some of the displayed tasks may belong to subprojects).

Verification tasks
------------------
check - Runs all checks. [test]
test - Runs the unit tests. [classes, testClasses]

Rules
-----
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
Pattern: clean<TaskName>: Cleans the output files of a task.

BUILD SUCCESSFUL

Total time: 5.175 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. So, to compile Groovy source files in our project, we must set a dependency on the groovy configuration.

To compile Groovy source files, we must add a dependency, with the Groovy library we want to use, to the groovy 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, because we can now use the Groovy version we really need. When we do want to use the Groovy libraries shipped with Gradle, we can use the special dependency localGroovy(). For a normal Groovy project this is not advised, but for plugin development it is useful.

First, we create a Groovy source file so 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 directory src/main/groovy/gradle/sample, with the name Sample.groovy. The following code shows the contents of this file:

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 Maven central repository and a Groovy dependency to the groovy configuration:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

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

When we run the build task, we get the following output:

$ gradle build
:compileJava UP-TO-DATE
:compileGroovy
Download http://repo1.maven.org/maven2/org/codehaus/groovy/groovy/1.8.6/groovy-1.8.6.pom
Download http://repo1.maven.org/maven2/org/codehaus/groovy/groovy/1.8.6/groovy-1.8.6.jar
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
:check
:build

BUILD SUCCESSFUL

Total time: 9.595 secs

When we don't have the specified Groovy library in our cache, it is downloaded from the Maven repository, by Gradle. The source code file is compiled, and if we look in the build/classes directory, we 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

groovy

org.gradle.api.file.SourceDirectorySet

The Groovy source files for this project. Contains both .java and .groovy source files if they are in the groovy directory.

groovy.srcDirs

java.util.Set<java.io.File>

Directories with the Groovy source files. Can also contain Java source files for joint compilation.

allGroovy

org.gradle.api.file.FileTree

Only the Groovy source files. All files with extension .groovy are part of this collection.

We extend our previous build file and add the task groovySourceSetsProperties. We print out the extra properties and their values with this task. The build now looks like this:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

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

task groovySourceSetProperties << {
    sourceSets {
        main {
            println "groovy.srcDirs = ${groovy.srcDirs}"
            println "groovy.files = ${groovy.files.name}"
            println "allGroovy.files = ${allGroovy.files.name}"
        }
    }
}

When we run the task groovySourceSetProperties on the command line, we see the following output:

$ gradle groovySourceSetProperties
:groovySourceSetProperties
groovy.srcDirs = [/gradle-book/samples/chapter8/groovy/src/main/groovy]
groovy.files = [Sample.groovy]
allGroovy.files = [Sample.groovy]

BUILD SUCCESSFUL

Total time: 2.997 secs

When our Java code uses Groovy classes, and vice versa, we can use the joint compilation feature. We must make sure both the Java and Groovy source files are in the src/main/groovy directory.

Creating documentation with the Groovy plugin

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 to the groovy configuration.

The task has several properties 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 configure the groovydoc task:

apply plugin: 'groovy'

version = 1.0

repositories {
    mavenCentral()
}

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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.158