Compile project with groovy 3

Is there a way to specify that gradle compile my project with groovy 3 rather than the groovy version gradle is itself running with?

sourceCompatibility

only works to specify the Java version, not Groovy.

$ ./gradlew -v

------------------------------------------------------------
Gradle 6.4
------------------------------------------------------------

Build time:   2020-05-05 19:18:55 UTC
Revision:     42f7c3d0c3066b7b38bd0726760d4881e86fd19f

Kotlin:       1.3.71
Groovy:       2.5.10
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.7 (AdoptOpenJDK 11.0.7+10)
OS:           Mac OS X 10.15.5 x86_64

To be clear, I’m not asking how to add the correct groovy dependency to the artifacts, but how to ensure they are compiled with the new groovy 3 compiler.

Try ext groovy.version?

sourceCompatibility

only works to specify the Java version, not Groovy.

This flag only instructs the normal Java compiler to reject language syntax not present in the version you specify. This doesn’t stop you from compiling with newer versions of Java, or even use newer APIs (which will of cause make it fail at runtime if you execute it on earlier version). There are no similar options for the Groovy compiler to make it reject newer syntax in your Groovy source code. I would argue that it is not all that useful anyway.

Try ext groovy.version?

If you use the Spring Dependency Plugin and you use a BOM for enforcing common versions, this will set it to another version. Outside that, it doesn’t do anything.

To be clear, I’m not asking how to add the correct groovy dependency to the artifacts, but how to ensure they are compiled with the new groovy 3 compiler.

But this is exactly how you configure a normal Groovy project in Gradle to be compiled with whatever version you like. The version of Groovy you see in gradle --version is the one used internally by Gradle for the Groovy DSL, e.g. for interpreting a build.gradle file. You can’t change this without hacking the Gradle installation, and you also really shouldn’t as it can easily cause problems with the rest of the Gradle internals.

So to ensure that you are using (at least) Groovy 3, put in the following dependency:

implementation platform("org.codehaus.groovy:groovy-bom:3.0.4")

If you want to see for yourself if you are using the Groovy 3 compiler (with the new Parrot parser), try adding some source code using the new Groovy 3 syntax features like Java-style array initialization. Compilation will fail if using an older version.

1 Like

Thank you. That’s extremely helpful.