3

I want to make sure my app always builds, runs tests, and runs using the correct locale, regardless of build & run platform.

So I have a simple unit test that checks that new Locale("nb", "NO") == Locale.getDefault()

Running the build with locale settings in either one of _JAVA_OPTIONS, GRADLE_OPTS or JAVA_OPTS works:

GRADLE_OPTS="-Duser.language=nb -Duser.country=NO" ./gradlew build

But I'm trying to avoid setting environment variables for every developer and build agent, so I tried setting these in gradle.properties:

org.gradle.jvmargs=-Duser.language=nb -Duser.country=NO

That doesn't work, since Locale.getDefault() now returns en_US.

I tried setting them in tasks.WithType(JavaCompile), that doesn't work either:

tasks.withType(JavaCompile) {
    options.forkOptions.jvmArgs += ['-Duser.language=nb', '-Duser.country=NO']
}

tasks.withType(Test) works however. Yay!

tasks.withType(Test) {
    jvmArgs += ['-Duser.language=nb', '-Duser.country=NO']
}

But now to the problem: IntelliJ IDEA still doesn't see the -Duser.* VM options, and the unit test fails there.

As a tip from @Andrej I tried setting the environment in ProcessForkOptions as described in a JetBrains issue:

tasks.withType(ProcessForkOptions) {
    environment("LC_ALL", "nb_NO.UTF-8")
}

No success.

How do I make it honor the Gradle config? Do I have to manually set the VM options in the Run Configuration for the test?

neu242
  • 15,796
  • 20
  • 79
  • 114
  • 1
    Try adding it for the task that creates new JVM process, like with this example: https://youtrack.jetbrains.com/issue/IDEA-115426#comment=27-610182 – Andrey Oct 06 '17 at 12:37
  • @Andrey Good idea, but it didn't work, neither for command line building nor from IntelliJ – neu242 Oct 06 '17 at 12:46

1 Answers1

0

As you can see at https://github.com/gradle/gradle/issues/2214, some system properties are not set properly if you set them via org.gradle.jvmargs. But using systemProp.user.language=nb instead works.

But even that only works when using Gradle is used to run. So you either have to change the IJ run configuration to also set those system properties, or you have to use Gradle from IJ to run the tests.

Vampire
  • 35,631
  • 4
  • 76
  • 102