Search code examples
javagradlechecker-framework

Passing the path to a Gradle dependency to the Java compiler


I'm playing with the Checker Framework and its annotation processor requires the path to the "annotated JDK" jar to be passed to it so that it can add type annotations to the JDK classes.

So, what I need Gradle to do is go get the path to the jar and pass it to the java compiler.

Pretty simple, normally, in Gradle:

// tried to keep this property in the project.extensions but wouldn't work
final AtomicReference jarRef = new AtomicReference()

task resolveAnnotatedJdk << {
    def jar = configurations.compile.resolve().find {
        it.name == "jdk8-${checkerVersion}.jar"
    }
    logger.log LogLevel.ERROR, "Found annoated JDK at ${jar.absolutePath}"
    jarRef.set jar.absolutePath
}

Running the resolveAnnotatedJdk task works!

The problem is trying to get it to work inside the compileJava config block:

compileJava {
    dependsOn << 'resolveAnnotatedJdk'
    sourceCompatibility = 1.8
    options.compilerArgs = ['-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
                            "-Xbootclasspath/p:${jarRef.get()}"]
}

This doesn't work because the compileJava block is configuration and runs before dependencies are resolved, I believe.

I've tried adding the options in a runFirst block and it seems to work:

compileJava {
    dependsOn << 'resolveAnnotatedJdk'
    sourceCompatibility = 1.8
}.doFirst {
    println "Will compile with ${jarRef}"
    options.compilerArgs = ['-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
                            "-Xbootclasspath/p:${jarRef.get()}"]
}

But I think I'm not doing it the appropriate way, seems like a hack, really.

Anyone knows how I can improve on this?


Solution

  • That's how I use it:

    configurations {
        checker
    }
    
    dependencies {
        checker "org.checkerframework:checker:1.9.6"
        checker 'org.checkerframework:jdk8:1.9.6'
        compile configurations.checker.dependencies
    }
    
    allprojects {
        tasks.withType(JavaCompile).all { JavaCompile compile ->
            compile.options.debug = true
            compile.options.compilerArgs = [
                    "-Xbootclasspath/p:${configurations.checker.asPath}",
                    '-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
                    '-implicit:class',
                    '-AprintErrorStack'
            ]
        }
    }
    

    Explanation: putting checker-framework into a separate configuration allows to obtain all jars in classpath format in a natural Gradle way, using ${configurations.checker.asPath}

    compile configurations.checker.dependencies allows to reuse checker jars in compile classpath without duplicate lines.