How to compile groovy sources mixed with java sources?


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.

Am I missing something? I appreciate your help.

Best regards Daniel

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.

Thanks for your reply.

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: = []
sourceSets.main.groovy.srcDirs += ["src/main/java"]

Thanks Peter. If that’s the recommend way, I’ll remove the src/main/java directory (because it’s useless).


how to set the javac compiler options when using the Groovy joint compilation.

I would need to suppress some warnings like "Unsafe is internal proprietary API … "

I’ve tried to add the following like:

compileGroovy.options.compilerArgs << ‘-XDignore.symbol.file’

But it is not working. What’s wrong ?

Please create a new topic.