简体   繁体   中英

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?

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 .

您应该能够将java测试移动到src/test/groovy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM