Dec 7, 2009

How to Specify Character Encoding for Gradle

We, programmers living in the multi-bytes world, need knowledge of specifying character encoding.

For Gradle, it is the following:
usePlugin 'groovy'

repositories {
    mavenCentral()
}
dependencies {
    groovy group:'org.codehaus.groovy', name:'groovy', version:'1.7-beta-2'
}

def defaultEncoding = 'UTF-8'
compileJava {
    options.encoding = defaultEncoding  // affects *.java under src/main/java
                                        // alternative: "file.encoding" property
compileTestJava {
    options.encoding = defaultEncoding  // affects *.java under src/test/java
                                        // alternative: "file.encoding" property
}
compileGroovy {
    groovyOptions.encoding = defaultEncoding  // affects *.groovy under src/main/groovy
    options.encoding = defaultEncoding        // affects *.java under src/main/groovy
}
compileTestGroovy {
    groovyOptions.encoding = defaultEncoding  // affects *.groovy under src/test/groovy
    options.encoding = defaultEncoding        // affects *.java under src/test/groovy
}

5 comments:

  1. Fine that you have these options for compiling, because those are dificult to find!

    ReplyDelete
  2. I found a nice shortcut in the gradle-user mailing list (for a subpart of your example)
    ----
    //setting the groovy source file encoding:
    [compileGroovy, compileTestGroovy]*.options*.encoding = 'UTF-8'
    ----

    ReplyDelete
  3. Sorry, the example should read:
    //setting the groovy source file encoding:
    [compileGroovy, compileTestGroovy]*.groovyOptions*.encoding = 'Cp1252'

    ReplyDelete
  4. it seems pretty good. thanks for your feedback.

    ReplyDelete
  5. I use
    [compileJava, compileTestJava, compileGroovy, compileTestGroovy]*.options*.encoding = 'UTF-8'

    ReplyDelete