Groovy with UTF-8 encoding output

There is no problem with UTF-8 output in build.gradle like:

println "測試" //in build.gradle

But if I do the same thing in Groovy classes. The result output to console is in wrong encoding even add ‘-Dfile.encoding=utf8’ to JAVA_OPTS. Following code is the task definition.

task (runSimple, dependsOn: 'classes', type: JavaExec) {
    main = 'com.example.SimpleSMS'
    classpath = sourceSets.main.runtimeClasspath

Is there a gradle way to specify correct encoding?

Groovy compilation forks a new process by default, and system properties aren’t carried over. But you can do this:

tasks.withType(GroovyCompile) {
  groovyOptions.encoding = 'utf-8'

The best way to discover such answers yourself is to look up the task in question in the DSL reference.