I have a gradle project that contains only Selenium/TestNG test classes. They are executed against a deployed war application. All works fine and now I’m adding a java utility that will query the test base and print list of tests that belong to a given TestNG group. The utility should be compiled and executed separate from the main project, as users may want to query the test base before test execution.
I added the following to build.gradle:
task listgroups(dependsOn:'buildUtil' ) <<{
ant.java(classname: 'util.TestGroupScanner', fork: true,
classpath: "src/test/java")
}
task buildUtil {
compile {
source = "src/test/java/util"
}
}
However, when calling listgroups task, I’m getting the following error:
C:\console-bg1>g listgroups
FAILURE: Build failed with an exception.
(...)
* What went wrong:
A problem occurred evaluating root project 'console-bg1'.
> Could not find method compile() for arguments [build_4emu7duna2isgubc1k8uts8k9
8$_run_closure6_closure11@d210ab] on root project 'console-bg1'.
I’m not sure how to resolve this issue and needless to say, haven’t found an answer online so far. Any pointers appreciated.
The problem is in the
buildUtiltask, as the error suggests. ThebuildUtildeclares acompileclosure, but such closure does not exist for the default task.Let me try to clarify what your setup is. The
util.TestGroupScannersource is in thesrc/test/java/utildirectory, which you want to compile separately from other source (presumablysrc/main/javaandsrc/test/java). ThebuildUtiltask is supposed to compile sources insrc/test/java/util, and thelistgroupstask executes the scanner utility on sourcessrc/test/javafolder.In this case, I’d suggest you declare a new source set for your utility sources, like this:
This will automatically create a compile task called
compileUtilJavafor you, that will compile those sources. I also think you’ll want to include utility classes in the classpath when executing your tool, which can be retrieved bysourceSets.util.output.classesDir. So now yourlistgroupstask will look like:One thing I have noticed about your setup, is that
src/test/java/utilsource folder is nested undersrc/test/java. Gradle will assumesrc/test/javato be the default folder for your project test, and will therefore automatically include it, and all of its children when running tests. Since you want to keep your utility folder separate from the default setup, I would recommend you put it insrc/testutil/java, to avoid any clashes. If you do, don’t forget to update thesourceSetssetup above with the correct source path.