Joint compilation
In the preceding examples in this chapter, we declared an interface in Java and implemented it in Groovy and Scala respectively. It was possible because the classes compiled by the java
plugin are available to Groovy and Scala classes.
If we want a Java class to have access to Groovy or Scala classes for its compilation, then we must compile the Java source file using the joint compilation supported by the respective plugin. Both the groovy
and scala
plugins support joint compilation and can compile Java sources.
For referencing Groovy classes in a Java class, the easiest way is to move the corresponding Java source file into src/main/groovy
(or in any of the Groovy srcDirs
configured for sourceSets
), and the Groovy compiler makes Groovy classes available to the Java class while compilation. The same goes for Scala joint compilation. We can put the Java files, which need Scala classes for their compilation, in any of the Scala srcDirs
(src/main/scala
by default).