Multiple classpaths for JavaExec

Hello All,

I am trying to include multiple classpath for JavaExec task. I got it somehow working, but could understand why. So I am seeking help here to really understand what’s happening.

build.gradle:

configurations {
    scalaRepl
}

dependencies {
    scalaRepl "org.scala-lang:scala-compiler:2.12.12"
}
  1. My working JavaExec task is below:
task repl(type:JavaExec) {
    doFirst {
        main = "scala.tools.nsc.MainGenericRunner"
        classpath {
            [
                configurations.scalaRepl,
                sourceSets.main.runtimeClasspath,
            ]
        }
        standardInput System.in
        args '-usejavacp'
    }
}
  1. This also works:
        ...
        classpath = configurations.scalaRepl
        classpath {
            [
                sourceSets.main.runtimeClasspath,
            ]
        }
        ...
  1. This also works
classpath = configurations.scalaRepl + sourceSets.main.runtimeClasspath

But below classpath setting doesn’t work:

classpath = configurations.scalaRepl
classpath = sourceSets.main.runtimeClasspath

I’m novice to groovy/gradle so really dont know why things are working. Would be nice if someone explains what is happening and what should be the correct way to combine multiple entries into one for classpath

Firstly, you should never initialise task inputs in a doFirst{...} block. This closure is invoked in Gradle’s execution phase, task inputs should be configured in the configuration phase. See build phases

Secondly you should be able to use FileCollection.plus(FileCollection). Eg:

classpath = configurations.scalaRepl.plus(sourceSets.main.runtimeClasspath)
1 Like