How to specify classpath for a ScalaCompile task


(kevin.stembridge.dev) #1

I’d like to execute a scala compile task from my build script but I can’t figure out how to configure it correctly. The task is in the snippet below. How do I specify the classpath attribute? I’ve tried a bunch of things but nothing works for me.

task compileModelDescriptor(type: ScalaCompile) {
    source 'src/main/modelgen'
    destinationDir project.file('build/classes/modeldescriptors')
    classpath 'what do I put here?'
}

(kevin.stembridge.dev) #2

I ended up solving this in a different way. By adding a source set, I don’t need to create my own scala compile task.

Thanks to Peter for his answer in a different thread: http://forums.gradle.org/gradle/topics/how_to_provide_a_sourceset_to_a_custom_scalacompile_task

This is my build script now:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath project(":mahana-modelgen-generator")
    }
}
    dependencies {
      compile project(":mahana-modelgen-generator")
    compile "org.scala-lang:scala-library:$scalaVersion"
  }
    sourceSets {
    main {
        scala {
            srcDir 'build/generated-src/scala/main'
        }
    }
    modelgen {
        compileClasspath = sourceSets.main.compileClasspath
    }
}
    task generateSampleModel(type: JavaExec, dependsOn: project.tasks[sourceSets.modelgen.getCompileTaskName('scala')]) {
      onlyIf {
        project.hasProperty('skipGenerateModel') == false
    }
      String modelDescriptorClass = 'com.mahanaroad.modelgen.sample.SampleModelDescriptor'
    String outputDirLocation = 'build/generated-src/scala/main'
      main = 'com.mahanaroad.modelgen.generator.ModelGenerator2'
    args = [modelDescriptorClass, outputDirLocation]
    classpath = project.files(configurations.runtime, sourceSets.modelgen.output)
}
    compileScala.dependsOn generateSampleModel

(René Groeschke) #3

Hi, classpath is a property of ScalaCompile and needs to be set by assigning a filecollection. here’s an example where I assign a configuration (which implements FileCollection) to the classpath

configurations{
   myConf
}
dependencies{
   myConf "somegroup:somelib:someVersion"
}
  task compileModelDescriptor(type: ScalaCompile) {
    source 'src/main/modelgen'
    destinationDir project.file('build/classes/modeldescriptors')
    classpath = configurations.myConf
}

hope that helps! cheers,

René