Passing the path to a Gradle dependency to the Java compiler

点点圈 提交于 2019-12-13 04:31:51

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31950055/passing-the-path-to-a-gradle-dependency-to-the-java-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!