I followed chapter 24 (groovy plugin) to compile groovy sources. I’ve a simple project, just 2 classes. 1 groovy class and 1 java class. My groovy class is located in src/main/groovy and my java class in src/main/java. The problem is that my java class uses the groovy class. During the compiling process I get the following error:
package XX does not exist (package from java class)
I don’t have that problem using maven. When using gradle I’ve to move the java class to src/main/groovy. This can not be the way to do it, because every time a java class uses a groovy class I’ve to move that class and every depending class in src/main/groovy.
My understanding that the “src/…/java” sources are handled by the java plugin, using the “javac” compiler, which doesn’t know about your Groovy classes.
If you move your files to “src/…/groovy” they will be handled by the groovy plugin, using “groovyc” which can do joint compilation of the classes by first producing stub classes for Groovy (only signatures, empty methods), then delegating to javac for the Java code, and finally compiling second time the Groovy classes against the generated Java code. It is a roundabout way, but it works.
I am not sure what Maven does, but I guess if that scenario works it must probably use Groovyc to compile both source trees in one pass.
But this would result in using only the src/main/groovy directory for both, groovy and java classes. The point is if I have a Util class, written in groovy, none of my pure java classes should use it, even in the future. Because if a java class uses this uitl class I’ve to move that java class and all depending classes into the src/main/groovy directory.
For me, it doesn’t make sense to put a java class into the src/main/java directory, because maybe in the future this java class will use (or what ever) a groovy class and then I’ve to move this class anyway.
The default settings provide full control over which Java sources (not) to joint-compile. If you don’t need/want that, either place all Java sources in ‘src/main/groovy’ and get rid of ‘src/main/java’, or reconfigure the source sets: