Incremental Java compilation often fails if there are ANTLR grammar files

Our project uses two plugins:

apply plugin: 'java'
apply plugin: 'antlr'

Grammar parsers/lexers generated by ANTLR are then compiled with the main source code. Generally this works fine. However, if the compilation is set to be incremental, then quite often it fails with errors like these:

.../src/main/java/some/LexerUser.java:8: error: package some.antlr does not exist
import some.antlr.SomeLexer;

Contents of package some.antlr is supposed to be autogenerated by the previous task, which actually does nothing, because everything is UP-TO-DATE. But the compilation task apparently fails to add the generated sources or compiled classes to the compilation classpath, so they cannot be referred to by other code.

I cannot quite figure out when it fails, but:

  • running gradle clean classes always succeeds;
  • non-incremental compilation always succeeds, including after the failure described above;
  • incremental compilation often (but not always) fails with errors as above.

Need some more context here. How are you defining the task that creates the generated source? Also how are you declaring that the generated sources should be on the compile classpath?

Basically just using tasks provided by the plugins. There is some configuration, but it’s probably not important:

// Don't build base grammar by themselves, as they are incomplete (like abstract classes).
generateGrammarSource {
    exclude ('**/*Base.g')
}

compileJava {
    sourceCompatibility  = '1.8'
    targetCompatibility  = '1.8'
    options.compilerArgs = ['-Xdiags:verbose', '-Xlint:all', '-Xlint:-deprecation', '-Xlint:-serial', '-Xlint:-classfile']
    options.incremental  = true

    doLast { ... }
}

How are the ANTLR grammer files being declared as inputs to the compileJava task?

They are not. They are input to standard generateGrammarSource task. This generates Java files, which are fed to compilation task. I never declare this explicitly, but I’m pretty sure the plugin already does that for me. If only because it always works in non-incremental compilation, even though the generated Java files are not in src/..., but in build/generated-src/antlr/....

I misspoke, I didn’t realize you were using the ‘antlr’ plugin for this. I’ll look into this, it may be a bug.

Hi Mark!

I am seeing a (probably) similar thing. My code is at https://github.com/flowlo/alpha (just set up the project so it’s quite empty, which might make pinning the error easier).

I am using the ANTLR Plugin with ANTLR v4. Locally, everything works fine, however on Travis CI (see https://travis-ci.org/flowlo/alpha/builds/139243577#L145 as an example) the task generateGrammarSource is skipped as “up to date”. I very much doubt that, as Travis CI does a fresh clone on every build.