System property (file.encoding) in gradle.properties not passed to forked process/groovyc

Setting the parameter externally works: export GRADLE_OPTS="-Dfile.encoding=UTF-8" But we’re trying to contain this on a per-project basis so we tried adding this to gradle.properties systemProp.file.encoding=UTF-8 But our tests fail as it does not seem to get passed to the groovyc compiler to compile the source with UTF-8. I can see the property is being utilised just not passed to these processes. So debug with neither setting

12:01:29.924 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:01:29.934 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'MacRoman'
12:01:29.998 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:01:30.011 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'MacRoman'
12:01:30.708 [QUIET] [system.out] *************************** File Encoding=MacRoman
12:01:43.251 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:01:43.253 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'MacRoman'

Debug with gradle.properties set

12:02:48.476 [DEBUG] [org.gradle.initialization.ProjectPropertySettingBuildLoader] Adding project properties (if not overwritten by user properties): [systemProp.file.encoding]
12:02:48.721 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:02:48.731 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'
12:02:48.792 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:02:48.806 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'
12:02:49.561 [QUIET] [system.out] *************************** File Encoding=UTF-8
12:03:02.446 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
12:03:02.447 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'

Debug with exported env var and no gradle.properties

11:59:15.209 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
11:59:15.218 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'
11:59:15.274 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
11:59:15.284 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'
11:59:15.966 [QUIET] [system.out] *************************** File Encoding=UTF-8
11:59:29.256 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
11:59:29.257 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'
-Dfile.encoding=UTF-8
'-Dfile.encoding=UTF-8'
-Dfile.encoding=UTF-8
'-Dfile.encoding=UTF-8'
11:59:40.414 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding.pkg' to 'sun.io'
11:59:40.415 [DEBUG] [org.gradle.api.internal.artifacts.ivyservice.IvyLoggingAdaper] setting 'file.encoding' to 'UTF-8'

You can see from the last one the -D parameters being applied to groovyc

I thought these 2 issues might resolve http://issues.gradle.org/browse/GRADLE-1618 - file.encoding System property (and GRADLE_OPTS) not passed to Daemon http://issues.gradle.org/browse/GRADLE-2099 - Impossible to set the default character encoding when using the daemon But obviously not and a slightly different issue? I tried this on m6, m9 and rc-1

Is this a bug, or is it meant to be this way and our only option in this case is to set the property externally. We want to avoid having to pass it on the command line and want to contain these per project as we run multiple projects in our workspaces.

Thanks Warren

The correct way to set the source file encoding for the Groovy compiler is:

tasks.withType(GroovyCompile) {
  groovyOptions.encoding = "MacRoman" // defaults to UTF-8
}

Awesome thank you! and the equivalent for Java was

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

and it all works now!

@Peter: is there are reason why we don’t default both with the same strategy?

I’d say we should default both to ‘-Dfile.encoding’ and be encouraging people to manage that properly as it has so many other benefits.

It’s going to be difficult to change behaviour here in a safe (i.e. backwards compatible) way though.

I think we should have a sourceEncoding property that’s obeyed by all compilers. I’d rather not model encodings with ‘file.encoding’. It’s not part of our object model, and it’s too coarse grained. Maybe it would make sense to default sourceEncoding to file.encoding, but I think I’d rather have it default to UTF-8.

All Java 7 VMs use ‘UTF-8’ as the default ‘file.encoding’, and Java 6 default is ‘UTF-8’ for all except Mac OS it seems.

To me, it’s cleaner to default all encodings to ‘file.encoding’ and strongly encourage people (through warnings) if necessary to make sure their ‘file.encoding’ is UTF-8. Mixing and matching encodings is a great way to tie yourself in knots.

All Java 7 VMs use UTF-8 as the default file.encoding, and Java 6 default is UTF-8 for all except Mac OS it seems.

Where did you get this from?

As far as I know, relying on file.encoding is discouraged in Java land. What I would do is to make all relevant Gradle APIs encoding aware, and provide ways to configure encodings on coarse grained and fine grained levels. If I’d provide an overall default, it would probably be UTF-8. Setting file.encoding should only be necessary when working with legacy libraries, and could probably stay a separate thing.

1 Like

I have this in my build file, which is a pure Java project:

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

but when I execute this

$ gradle --no-color -i -u clean build oneJar 2>&1 |tee build.log

I see MacRoman encoding arguments to the daemon:

Starting Gradle daemon
Starting Gradle daemon
Starting daemon process: workingDir = /Users/petrovic/.gradle/daemon/1.4, daemonArgs: [/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java, -XX:MaxPermSize=4g, -XX:+HeapDumpOnOutOfMemoryError, -Xmx4g, -Dfile.encoding=MacRoman, -cp, /Users/petrovic/tools/gradle-1.4/lib/gradle-launcher-1.4.jar, org.gradle.launcher.daemon.bootstrap.GradleDaemon, 1.4, /Users/petrovic/.gradle/daemon, 10800000, c7fd14a7-ae44-43df-907a-380ac0e22bcc, -XX:MaxPermSize=4g, -XX:+HeapDumpOnOutOfMemoryError, -Xmx4g, -Dfile.encoding=MacRoman]
Starting process 'Gradle build daemon'. Working directory: /Users/petrovic/.gradle/daemon/1.4 Command: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError -Xmx4g -Dfile.encoding=MacRoman -cp /Users/petrovic/tools/gradle-1.4/lib/gradle-launcher-1.4.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 1.4 /Users/petrovic/.gradle/daemon 10800000 c7fd14a7-ae44-43df-907a-380ac0e22bcc -XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError -Xmx4g -Dfile.encoding=MacRoman

I don’t know if this is of concern, but it was unexpected. Where is the -Dfile.encoding=MacRoman coming from?

Thanks.

It’s not a concern. Encoding set for the (daemon) JVM and encoding set for the Groovy compiler are separate things. Just means that your client JVM is using MacRoman, which might be the default on your platform.