How do I add generated java files to the compileJava task's source path correctly? The compiled classes are deleted by compileGroovy

In the antlr4 plugin I’ve written (https://github.com/fraoch/antlr4-plugin), I’m getting a problem where the generated (and compiled) source files are not being recognised by the compileGroovy phase (in fact it deletes the generated classes) if the package in the groovy file is different to the package of the grammar. If the packages are the same, it works.

The way I’m gen/compiling the grammars is making a dependency on the compileJava task to my own generate task and in this task adding any dirs of java files created to the compileJava task with:

compileJava.source(generatedSrcDir)

compileJava.include("**/*.java")

which I can see adding correctly if i print out the compileJava.getSource() values.

Running a compileJava task works as intended, generating the java from antlr grammar and compiling them to classes in the build dir.

However, if I run compileGroovy (to compile an example lexer) that’s in a different package, the generated classes above get deleted and the groovy fails to compile with missing classes exceptions.

What’s the correct way to add the generated java files so that they get compiled and the output classes aren’t deleted by the compileGroovy task?

The generate task where I think the issue is in, is at https://github.com/fraoch/antlr4-plugin/blob/master/src/main/groovy/net/markjfisher/antlr4/gradle/tasks/GenerateTask.groovy