Search code examples
javagradlejavacompiler

Could not find method sourceCompatibility() for arguments on root project


I would like to define source and target compatibility for a Java library which is built with Gradle. Therefore, I add the following block as documented for the Java plugin.

apply plugin: 'java'

// ...

compileJava {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

When I assemble the project the following error occurs:

Could not find method sourceCompatibility() for arguments [1.7] on root project


Related


Solution

  • You are trying to pass the values of the wrong type. sourceCompatibility and targetCompatibility should be the Strings, but JavaVersion.VERSION_1_7 is not a String, but just enumiration with overrided toString() method. That is why you've got for arguments [1.7] in the exception text. Just try to do it like:

    compileJava {
        sourceCompatibility JavaVersion.VERSION_1_7.toString()
        targetCompatibility JavaVersion.VERSION_1_7.toString()
    }
    

    or

    compileJava {
        sourceCompatibility "$JavaVersion.VERSION_1_7"
        targetCompatibility "$JavaVersion.VERSION_1_7"
    }
    

    Or just move it out out of compileJava closure to the script body, as it usually used, like:

    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7