How to compile a java class that depends on a scala class in gradle

Hi,

I’m in the process of converting my SBT build over to gradle, as a trial. But, using the scala plugin, I cannot see how to get my project to compile when I have a java class that depends on a scala class in the same project.

I have my java files under src/main/java and my scala files under src/main/scala.

It seems that the scalaCompile task depends on javaCompile. This won’t work for me, as I need the Scala class compiled before the Java class.

/Raymond Barlow

Found a solution here: http://compulsiontocode.blogspot.com/2011/05/fixing-gradle-compilescala-to-not.html

I believe this solution isn’t quite correct. See my comment on the blog entry.

It seems that the scalaCompile task depends on javaCompile. This won’t work for me, as I need the Scala class compiled before the Java class.

This is intentional, to cover the common case that some Scala code depends on Java code (but not the other way around) and joint compilation is not wanted/needed.

To get joint compilation, you have to add some Java sources to the “scala” source directory set, and at the same time take them away from the “java” source directory set. For example, if you want everything to be joint-compiled, do this:

sourceSets.main.scala.srcDir "src/main/java"
sourceSets.main.java.srcDirs = []

Now both the Scala and Java code will be compiled by the compileScala task.

Please let us know in case this doesn’t work as promised.

1 Like

No, this worked beautifully. Thanks!

/Raymond

Perhaps this could be more clearly explained on the Scala plugin page? From reading the documentation, I was under the impression that joint compilation was the default.

Adam, seconded.