49

To avoid warnings regarding special characters when building my Java source code, I put this line in my gradle.build which worked fine before upgrading to Gradle 2.0:

tasks.withType(Compile) { options.encoding = "UTF-8" }

After upgrading, this fails with the following error:

Could not find property 'Compile' on root project

How can I fix that?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171

3 Answers3

101

Changing the line to

tasks.withType(JavaCompile) { options.encoding = "UTF-8" }

fixed the issue.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • 8
    `Compile` had long been deprecated in favor of `JavaCompile`, and was finally removed in 2.0. – Peter Niederwieser Jul 10 '14 at 05:36
  • 1
    @PeterNiederwieser: Good to know, thanks. I didn't notice that because I wasn't getting any deprecation warnings when compiling with Gradle pre 2.0. – Matthias Braun Jul 11 '14 at 02:45
  • 1
    @PeterNiederwieser how do I set encoding for all compile tasks (not just java)? It used to be ```Compile```, should I use ```tasks.withType(AbstractCompile)``` instead? – enlait Sep 26 '14 at 07:38
  • 6
    `Compile` has been renamed to `JavaCompile`. To set the encoding for Groovy sources, use `tasks.withType(GroovyCompile) { groovyOptions.encoding = "UTF-8" }`. For Scala sources, use `tasks.withType(ScalaCompile) { scalaCompileOptions.encoding = "UTF-8" }`. There never was an easier way. – Peter Niederwieser Sep 26 '14 at 08:11
2

For Groovy based projects. It'd be:

tasks.withType(GroovyCompile) {
    options.debug = true
}
maja
  • 17,250
  • 17
  • 82
  • 125
AKS
  • 16,482
  • 43
  • 166
  • 258
2

Use task.withType(JavaCompile).

My code:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.3'
    }

  tasks.withType(JavaCompile) {
      options.debug = true
      options.debugOptions.debugLevel = "source,lines,vars"
      options.encoding = "UTF-8"
  }
}
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
liu shuai
  • 53
  • 1
  • 1
  • 6
  • While this may solve the OP question, please add only essential parts of code, and provide explanation. – jb. Oct 26 '15 at 14:50