Project mixing scala and groovy does not compile with gradle 4.0


(Felix) #1

Hi,

I have a scala project that uses a tiny bit of groovy source code (a single class to be precisely). I am applying the groovy plugin; and the sources are locates in src/main/scala and src/main/groovy. This used to work fine, however when I gave 4.0 a shot I got this error:

> Task :XXX:YYY:compileScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
/path/to/some/ScalaSourceFile.scala:162: not found: type SomeClassWrittenInGroovy

where SomeClassWrittenInGroovy is located under src/main/groovy

–debug reveals that the -classpath passed to the scala compiler doesn’t include build/classes/groovy/main:

16:13:41.369 [DEBUG] [org.gradle.api.internal.tasks.scala.ZincScalaCompiler] Calling Scala compiler with arguments  (CompilerInterface):
	-deprecation
	-unchecked
	-bootclasspath
	/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/classes:/home/felix/.gradle/caches/modules-2/files-2.1/org.scala-lang/scala-library/2.12.2/fe9780170207ebdabd4ac5bb335c82bd0aead480/scala-library-2.12.2.jar
	-classpath
	/MY_PROJECT_HOME/XX/YY/build/classes/scala/main:<LONG_LIST_OF_DEPENDENCIES>:/MY_PROJECT_HOME/XX/YY/build/classes/java/main:/MY_PROJECT_HOME/XX/YY/build/classes/scala/main

Now how can I make the scala compiler to include /MY_PROJECT_HOME/XX/YY/build/classes/groovy/main into -classpath ?


(Felix) #2

So I followed the suggestion in the release notes and added this to my build file:

sourceSets.main.output.classesDir = new File(buildDir, "classes/main")

which helps. It turns out that the message Pruning sources from previous analysis, due to incompatible CompileSetup. was not related to the issue.

Anyway, this is just a workaround, so I would still like to understand how I can control the class path of the scala compiler task or how to enable joint compilation of scala and groovy sources. Anyone?


(Sterling Greene) #3

Did you have any other configuration in your build before?
e.g., something like

compileScala.dependsOn compileGroovy


(Felix) #4

No, just the apply plugin: statements.
So I gave it a shot but the result is the same. The groovy classes dir is still not passed with -classpath.


(Sterling Greene) #5

Sorry, I didn’t mean to imply that would fix it.

This used to work because all compilation tasks shared the same output directory, so depending on the order that the compilation tasks would run, you could depend on classes from earlier compilation tasks.

Since there was no relationship between compileGroovy and compileScala, you were getting lucky that Groovy compilation would happen before Scala compilation.

I’ve opened an issue for this: https://github.com/gradle/gradle/issues/2312

As a different workaround, you could do this:

compileScala {
    classpath = classpath.plus(files(compileGroovy.destinationDir))
    dependsOn compileGroovy
}

This explicitly adds the Groovy compilation output directory to the classpath for Scala.