如何在Java测试之前让Gradle编译Groovy测试 [英] How to make Gradle compile Groovy tests before Java tests
问题描述
Gradle的Groovy插件声称它支持联合编译,它允许在两个方向上自由组合和匹配Groovy和Java代码,以及依赖项
但是,我认为这不适用于测试代码。
我有一个Java'sample'测试 src / test / java ...
它使用位于 src / test / groovy
中的类。
在尝试使用Gradle构建时,出现如下错误:
SwingJavaFXSampleAppTestInJava.java:23:error:can not find symbol
SwingJavaFXSampleAppTest swingJavaFx = new SwingJavaFXSampleAppTest();
请注意, SwingJavaFXSampleAppTest
是一个Groovy类,还没有被编译过(在Gradle输出中,我可以看到它在它尝试 compileTestJava
之前没有运行 compileTestGroovy
因为前者取决于后者)。
我可以使用groovy-eclipse插件与Maven一起构建相同的项目。
为什么在Gradle中声称支持以任何顺序编译时不起作用,我该如何让它工作? 解决方案
正如 Gradle用户指南所述,只有代码已通过到 GroovyCompile
任务是联合编译的。因此,无论是将Java还是Groovy代码放入 src / main / groovy
中,或者重新配置源集:
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs = [src / main / java,src / main / groovy]
对于测试,将所有出现的 main
替换为 test
。
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测试之前让Gradle编译Groovy测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!