How can I configure which JDK is used to run the scala compiler?

How can I configure which JDK is used to run the scala compiler? I would like to configure this within the script, as opposed to changing the version of Java used to run gradle.

I don’t think it’s currently possible.

Thanks for the response.

For the purpose of repeatable builds, we’d like to guarantee that we compile against the same libraries as part of the build process, and this should extend to include the core platform libraries (i.e., core Java and Scala APIs).

When compiling Java code, we can point to a specific version of Java using forkOptions.executable. When compiling Scala, we have the ability to point to the specific scala library we want to build against, however I don’t see how I can point to a specific Java platform (since scala code can reference core Java apis).

Any ideas? How do other people accomplish this?

I’ve explained the current options in my answer to your Stack Overflow question.

I did not post a question on SO, but I’m assuming you’re referring to this one:

Thanks for the feedback!

Yes, that’s the one.

Peter, I tried passing jvmArgs to the scala compiler as suggested in your stackoverflow answer, but this parameter seems to be ignored. I inspected the output of “gradle compileScala --debug” and the passed jvmArgs did not appear in the exec call.

This is what I’m using:

tasks.withType(ScalaCompile) {
  configure(scalaCompileOptions.forkOptions) {
    jvmArgs = [ 'asfdlksafkljsaflsjdf' ]
  scalaCompileOptions.fork = true
  scalaClasspath = configurations.scalaCompiler

Apparently, JVM args aren’t passed when using the old Ant-based compiler integration. However, they are passed when using the new Zinc compiler integration (‘scalaCompileOptions.useAnt = false’).

It is possible to pass javabootclasspath and javaextdirs to scalac so it would use alternative JDK

allprojects {
    tasks.withType(ScalaCompile) {
        if (sourceCompatibility == '1.8') {
            scalaCompileOptions.with {
                useAnt = true
                fork = false
                def jdk8rt = new File("$System.env.JAVA8_HOME", "jre/lib/rt.jar").canonicalPath
                def jdk8ext = new File("$System.env.JAVA8_HOME", "jre/lib/ext").canonicalPath
                additionalParameters = ["-javabootclasspath $jdk8rt".toString(), "-javaextdirs $jdk8ext".toString()]