Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • How can I set the compileOptions for my Gradle Java plugin?
like image 559
JJD Avatar asked Oct 23 '15 07:10

JJD


1 Answers

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
like image 117
Stanislav Avatar answered Oct 19 '22 21:10

Stanislav