Gradle cannot infer scala class path despite the scala library being declared as a compile dependency


(Jim Newsham) #1

I’m new to gradle, and trying to get a basic script working with scala. As the title says, I have configured the scala library as a compile-time dependency, however “gradle compileScala” fails saying that it cannot infer the scala class path because “no Scala library Jar was found”. I’m using ivy, and I see that I’m able to compile Java code which depends on other lib dependencies (i.e., using “gradle compileJava”). In fact, I can modify my java code to refer to scala classes (such as Option), and this builds fine in Java. So something appears broken in compiler inference.

Any ideas? Also, is there any workaround which allows me to explicitly configure the scala compiler?


(Jim Newsham) #2

As a workaround, I found that I can point scalaClasspath at the scala jars and get it to compile. However, this is not in a state where I would be comfortable committing to our project because I’m currently pointing to the jars on the filesystem, and I would really not like to commit the scala jars to source control. Is there a way I can retrieve the scala jars from the artifact repository (ivy in this case) and add them to scalaClasspath?


(Jim Newsham) #3

I was able to figure out how to configure scalaClasspath with the scala jars from ivy. Here’s what I used, in case this helps anyone:

ext.scalaFullVersion = ‘2.10.3’

dependencies {

scalaTools “org.scala-lang:scala-library:${scalaFullVersion}”,

“org.scala-lang:scala-compiler:${scalaFullVersion}”,

“org.scala-lang:scala-reflect:${scalaFullVersion}”

}

def scalaToolJars = configurations.scalaTools.resolvedConfiguration.resolvedArtifacts.collect({ artifact ->

def jars = new ArrayList()

if (artifact.type == ‘jar’) {

jars.add(artifact.file)

}

return jars

}).flatten()

tasks.withType(ScalaCompile) {

scalaClasspath = files(scalaToolJars)

}


(Peter Niederwieser) #4

If ‘scalaClasspath’ can’t be inferred, this means that there is no Jar named ‘scala-library-some.version.jar’ on the compile class path. Did you declare such a compile dependency?


(Jim Newsham) #5

Yes I did declare such a dependency.


(Peter Niederwieser) #6

What does the following print?

task debug << { println configurations.compile.findAll { it.name.startsWith("scala") } }

(Jim Newsham) #7

I’ll have to check on that monday morning. Thanks.


(Jim Newsham) #8
$ gradle debug
:MyProject:debug
[C:\dev\ivyrepo\org.scala-lang\scala-library.10.3\jars\scala-library.jar]

(Peter Niederwieser) #9

Looks like you have a local Ivy repo whose artifact names don’t include the version. If you can’t change that, the Scala version cannot be inferred, and you’ll have to configure ‘scalaClasspath’ explicitly:

configurations {
    scalaCompiler
}
  dependencies {
      scalaCompiler "org.scala-lang:scala-compiler:2.10.3"
    // if you don't have a correct ivy.xml for scala-compiler,
     // you'll also have to declare the two other deps
}
  tasks.withType(ScalaCompile) {
    scalaClasspath = configurations.scalaCompiler
}

This solution doesn’t rely on deprecated elements such as ‘scalaTools’, and hence also works for 2.x.


Could this get added to the documentation for the scala plugin?
(Jim Newsham) #10

The version doesn’t appear in the filename, but it appears elsewhere in the path. Is this not sufficient? Ivy is configured with the appropriate pattern to parse the version out of the path.

I think there was a problem with pasting the output into this forum. The version 2.10.3 should appear in the path. Here’s another attempt:

$ gradle debug :MyProject:debug [C:\dev\ivyrepo\org.scala-lang\scala-library\2.10.3\jars\scala-library.jar]


(Peter Niederwieser) #11

No, it isn’t sufficient.