0

I have a gradle script with a compileJava task, and I want it to provide two different Jar tasks, jar and jarForce. The jarForce task should compile the sources with -Awarns option added so that the annotation processor errors are treated as warnings, and the jar task should run compile with the default arguments so that the compilation fails if there are any annotation processor errors. How can I achieve that?

saga
  • 1,933
  • 2
  • 17
  • 44

1 Answers1

0

Consider the following, which modifies the options to compileJava in the case where jarForce is in the task graph:

apply plugin: 'java'

gradle.taskGraph.whenReady { taskGraph ->
    if (taskGraph.hasTask(jarForce)) {
        println "TRACER jarForce detected"

        // we use verbose simply to illustrate that the options are changed
        compileJava.options.compilerArgs << "-verbose"
        compileJava.options.compilerArgs << "-Awarns"
    }
}

task jarForce(dependsOn: 'jar') {
    doFirst {
        println "TRACER jarForce"
    }
}

If you execute gradle clean jar versus gradle clean jarForce, you'll see the output is quite different, since we use -verbose as an illustration. The use of -Awarns is left to the reader.

Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • When is the "-Awarns" option added to compilerArgs? Between the configuration and execution phases? – saga Jun 09 '18 at 14:17
  • re: when? Yes, per this doc, the taskGraph is populated after the project is evaluated (and before a task is executed) - https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraph.html – Michael Easter Jun 09 '18 at 14:29