Invalid flag message with gradle - clarification needed


(Sabari) #1

Hi,

I’m a newbie in gradle. I noticed some behaviour with gradle which i wanted to clarify.

  1. In the this code, the javac options i add appear with double quotes in the front and back of the option in the compiler-options text file. Hence javac fails with a invalid flag message.

project(’:com.test’) {
def dJar = project(’:com.best’).jar.outputs.files.getFiles()
def addReads = "–add-reads com.test=ALL-UNNAMED"
def modulePath = "–module-path " + dJar.getAt(0)

    tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.executable = '/home/sabari/java_builds/java9/bin/javac'

    options.compilerArgs << addReads
    options.compilerArgs << modulePath

    }

    dependencies {
    compile project(':com.best')
    }

}

Output of java-compiler-args.txt: (truncated)
-classpath
/home/sabari/sample-codes/java9/com.best/build/libs/com.best.jar
"–add-reads com.test=ALL-UNNAMED"
"–module-path /home/sabari/sample-codes/java9/com.best/build/libs/com.best.jar"
/home/sabari/sample-codes/java9/com.test/src/main/java/com.test/com/test/Hi.java
/home/sabari/sample-codes/java9/com.test/src/main/java/com.test/module-info.java

Output of build:
javac: invalid flag: --add-reads com.test=ALL-UNNAMED
Usage: javac

whereas when i add like this (below code), it works. Is my earlier way of using incorrect?

project(’:com.test’) {
def dJar = project(’:com.best’).jar.outputs.files.getFiles()
def addReads = "com.test=ALL-UNNAMED"
def modulePath = dJar.getAt(0)

    tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.executable = '/home/sabari/java_builds/java9/bin/javac'

options.compilerArgs << '--add-reads'
    options.compilerArgs << addReads
options.compilerArgs << '--module-path'
    options.compilerArgs << modulePath

    }

    dependencies {
    compile project(':com.best')
    }

}

  1. While debugging the problem #1, i was trying to add “-help” javac option to check if it’s working. It worked only when i fork a process for javac.

This code worked:

project(’:com.test’) {
def dJar = project(’:com.best’).jar.outputs.files.getFiles()
def addReads = "com.test=ALL-UNNAMED"
def modulePath = dJar.getAt(0)

    tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.executable = '/home/sabari/java_builds/java9/bin/javac'

options.compilerArgs << '--add-reads'
    options.compilerArgs << addReads
options.compilerArgs << '--module-path'
    options.compilerArgs << modulePath
options.compilerArgs << '-help'

    }

    dependencies {
    compile project(':com.best')
    }

}

This code failed to work: // Here, as i understand, it uses Java Compiler API from javax.tools package to compile the code.

project(’:com.test’) {
def dJar = project(’:com.best’).jar.outputs.files.getFiles()
def addReads = "com.test=ALL-UNNAMED"
def modulePath = dJar.getAt(0)

    tasks.withType(JavaCompile) {
   // options.fork = true
   // options.forkOptions.executable = '/home/sabari/java_builds/java9/bin/javac'

options.compilerArgs << '--add-reads'
    options.compilerArgs << addReads
options.compilerArgs << '--module-path'
    options.compilerArgs << modulePath
options.compilerArgs << '-help'

    }

    dependencies {
    compile project(':com.best')
    }

}

Output: invalid flag: -help

Again, is there anything wrong in the usage?


(James Justinic) #2

The difference between your usages is whether you provide one or two arguments for --add-reads and --module-path. As far as the API is concerned, options.compilerArgs is a collection of all compiler args, which requires that you provide --add-reads and com.test=ALL-UNNAMED as two separate arguments (they’re separate but related).

You can still use your declared variables if you update them to store separate items.

def addReads = [ "--add-reads", "com.test=ALL-UNNAMED" ]
def modulePath = [ "--module-path", dJar.getAt(0) ]