How to compile Java class with groovyc compiler?

Hello, I’ve faced an issue with compiling Java source files with groovyc compiler. I use some Groovy AST annotations on my Java beans and need to compile this classes with groovyc compiler to apply this transfromations. Here is my src folder strutcture:


This Java class has next AST anntotations:


But when I run compileGroovy task it GJavaBean.class doesn’t have any AST transformation results.

Here is my build.gradle file:

apply plugin: 'groovy'
  repositories {
  tasks.withType(GroovyCompile) {
    dependsOn = []
  dependencies {

Can you please help me with this issue?

Are you saying that you want to use AST transformations such as ‘@Canonical’ on Java (rather than Groovy) classes? That isn’t possible, at least not without having the Groovy compiler compile them as Groovy classes.

Yes, you are correct. When I tried to do this from bash with command:


It works fine. Is it possible to say gradle to compile Java sources as Groovy one?

It will work as long as the code is also valid Groovy code. Of course the generated bytecode will be different, and will contain references to the Groovy runtime libraries. (You could try ‘@CompileStatic’ to get output that’s closer to Java.)

As for how to achieve the same with Gradle: As Gradle always has Java/Groovy joint compilation activated (i.e. ‘GroovyCompile’ tasks can compile mixed Java/Groovy code and will use the Java compiler for Java code), you’ll have to rename the source file to ‘.groovy’. This is a good move anyway, as it’s now effectively Groovy code.

So placing .java files into src/main/groovy directory doesn’t ensure that these files will be compiled by Groovy compiler. Am I right?

In a sense. Such files will be joint-compiled as Java code, and transform annotations will have no effect.