Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Gradle compile Groovy tests before Java tests

The Groovy plugin for Gradle claims that it "supports joint compilation, which allows to freely mix and match Groovy and Java code, with dependencies in both directions".

However, I don't think this applies to test code.

I have a Java 'sample' test in src/test/java... which uses a class which is located in src/test/groovy.

When trying to build with Gradle, I get an error like this:

SwingJavaFXSampleAppTestInJava.java:23: error: cannot find symbol
SwingJavaFXSampleAppTest swingJavaFx = new SwingJavaFXSampleAppTest();

Notice that SwingJavaFXSampleAppTest is a Groovy class that has not been compiled yet (in the Gradle output I can see that it did not run the compileTestGroovy before it tried compileTestJava because the former depends on the latter).

I am able to build this same project with Maven using the groovy-eclipse plugin.

Why does it not work in Gradle when it claims to support compilation in any order, and how can I make it work?

like image 632
Renato Avatar asked Dec 25 '22 18:12

Renato


2 Answers

As explained in the Gradle User Guide, only code passed to GroovyCompile tasks is joint-compiled. So either you put both Java and Groovy code into src/main/groovy, or you reconfigure the source sets:

sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs = ["src/main/java", "src/main/groovy"]

For tests, replace all occurrences of main with test.

like image 190
Peter Niederwieser Avatar answered Dec 28 '22 10:12

Peter Niederwieser


You should be able to move your java tests into src/test/groovy.

like image 41
tim_yates Avatar answered Dec 28 '22 11:12

tim_yates