Running a Tool (written in Java) from gradle task leads to circular dependency

Hi,

I have a tool written in Java which generates some source code required for compilation of my Project module.
The Java Tool can be called using static main method. I have written a gradle task to invoke it:

task generateStubSource(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'org.test.mytool.Generate'
args = [arg1]
standardOutput = System.out
errorOutput = System.err

}

I have added a dependency like this:

compileJava.dependsOn(generateStubSource)

But this somehow ends up in circular dependency.

00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter]
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] Circular dependency between the following tasks:
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] :classes
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] ±-- :compileGroovy
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] | -– :compileJava
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] | -– :generateStubSource
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] | -– :classes ()
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] -– :compileJava (
)
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter]
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] (*) - details omitted (listed previously)
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter]
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter]
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] * Try:
00:57:15.782 [ERROR] [org.gradle.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.
00:57:15.782 [LIFECYCLE] [org.gradle.BuildResultLogger]
00:57:15.782 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED

P.S. I am very new to gradle/groovy (just started couple of days back). Apologies if I have overlooked something very obvious.

Well, your task has an implicit dependency on the “classes” task (I assume from your classpath setting), which depends on “compileJava”, which depends on your task. Thus, the circular dependency.

It might be possible to separate the source code that comprises your “org.test.mytool.Generate” class from the “other” source code in the project. Put these into two separate sourcesets (read about sourcesets in the user guide). You will then configure your build process to first compile the source for “Generate”, then run the class to generate some other source code, then you compile all your other source code. Your “generateStubSource” task will reference a different sourceset, obviously.

@David, Thanks for your inputs. I am able to make it work now. :grinning: