Note that this blog post has been archived. Information may be out of date or incorrect.

Using Groovy classes from within Java when building with Gradle

I’ve been experimenting a bit on Terasology, an open-source game inspired by Minecraft. They’re using Groovy and Java in their project, and their build tool is gradle.

Unfortunately it wasn’t as straight forward as I had hoped to use a Groovy class from within Java, because by default, Gradle seems to first invoke the Java compiler and afterwards the Groovy compiler, which leaves the former with missing symbols (the classes from Groovy that haven’t been compiled yet).

Apparently, to enable “joint compilation”, you have to force the Groovy compiler to compile both Java and Groovy sources (as stated here), which can be done by adding the following snippet to your “build.gradle”:

sourceSets {
    main {
        java { srcDirs = [] }    // no source dirs for the java compiler
        groovy { srcDir 'src' }  // compile everything in src/ with groovy