6

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

Community
  • 1
  • 1
JJD
  • 50,076
  • 60
  • 203
  • 339

1 Answers1

9

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
Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 1
    ... or just simply write "1.7" instead of using the `JavaVersion` enumeration. – thokuest Oct 24 '15 at 07:19
  • @Stanislav Your last suggestion `sourceCompatibility JavaVersion.VERSION_1_7 ..` does not work for me and produces the **same error** as before. The 1st and 2nd suggestions work although the former produces an **inspection warning** in IntelliJ 14.1.5. `... cannot be applied to (String)`. So, I might use the 2nd approach. – JJD Oct 26 '15 at 18:30
  • @JJD curiously, it is working for me, with gradle 2.6 and Intellij 14.1.3, if to place it out of any tasks closure – Stanislav Oct 26 '15 at 19:20
  • @Stanislav Please try to build [the project](https://github.com/johnjohndoe/HalfnarpClient) on the command line via `./gradlew clean assemble` - that is what fails in my case. – JJD Oct 26 '15 at 20:49
  • 1
    @JJD my mistake, since we set the property out of configuration closure, we have to provide `=`, so I updated my answer. I just downloaded your project and added to the build script root `sourceCompatibility = JavaVersion.VERSION_1_7` and `targetCompatibility = JavaVersion.VERSION_1_7` and it was successfully built with gradlew. – Stanislav Oct 27 '15 at 06:57
  • @Stanislav When I use the corrected assignment `sourceCompatibility = JavaVersion.VERSION_1_7` then IntelliJ states `Assignment is not used.` and renders a curly underline for `sourceCompatibility` to visually indicate the warning. – JJD Oct 27 '15 at 09:19
  • 1
    @JJD you can ignore it, seems, that Idea doesn't know, that they are used in some additional plugin. Just to check, whether is option accepted by gradle java plugin, modify it as follows `sourceCompatibility = "wrongVersion"` to make the value equals to `wrongVersion` string. Your build will fail, because gradle could not determine java version. If you want to prevent idea to underline this properties, just modify some task, to print this values as additional info to the user during configuration phase. – Stanislav Oct 27 '15 at 09:41
  • I think I will use `compileJava { sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 }` which combines the advantages of all the mentioned variants. – JJD Oct 27 '15 at 12:11