Could not determine the dependencies of task ':compileJava'.
> Could not create task ':openapi-generate-model'.
> Could not create task of type 'OpenApiGenerateModel'.
> Class Build_gradle.OpenApiGenerateModel is a non-static inner class.
I have written this same script in groovy with no issue, however in kotlin, i always seem to get this error. I have read on stack overflow here that this issue may arise when the task uses a variable or state from outside the class definition. I am not really sure where to go from here, any pointers in the right direction would be very helpful.
The SO post is correct.
Which line causes is is hard to tell.
Your snippet is incomplete as it does not even compile and if I make it compile, the error does not resemble.
If you could provide an MCVE I could maybe tell where the problem is.
But generally it is like you found out.
If that class uses anything from the build script like a local variable or similar, then the class becomes non-static inner class as it needs an instance of the script class to get that variable.
If it does not use something from outside, it is a static inner class and it would work.
You can also simply try to comment everything and then comment in part by part to find the exact line where the problem is.
Besides that, registering a task at execution time from another task (or doing any configuration change is a very bad idea.
And trying to execute a task manually is an even worse idea, as you for example miss the necessary task dependencies and so on.
Besides that using register just to right away do get() also does not make much sense, you could then also right away use create instead.
As you already injected execOperations but did not use it, what you actually want to do is using execOperations.javaExec { ... } instead of registering a new JavaExec task and executing it.
@Vampire per your comment, Im a little confused on what best conventions are for creating and registering a custom task that uses execOperations to perform command line operations like OP. I found this post because I had some custom tasks in the format:
val someTask by tasks.register("someTask") {
doLast {
exec {
commandLine("git", "add", "*")
}
}
}
and was trying to get rid of the linter warnings telling me to use ExecOperations to perform those command line operations. When I define a task similar to OP’s and try to create an instance of it, I get the same error trying to inject an ExecOperations instance into my task
Custom Task
abstract class VersionGenerationTask : DefaultTask() {
@get:Inject
abstract val execOperations: ExecOperations
@TaskAction
fun run() {
dependsOn("writeNewProjectVersion")
// Tag current branch with version
execOperations.exec {
val currentVersion = baseVersion.toString().replace("-SNAPSHOT","")
commandLine("git" ,"tag", "-a", currentVersion, "-m", "version $currentVersion")
}
// Add , commit, push changes
execOperations.exec {
commandLine("git", "add", "*")
}
execOperations.exec {
commandLine("git", "commit", "-m", "Committing new version: $version")
}
execOperations.exec {
commandLine("git", "push")
}
}
}
Code I use to create an instance of that task
val myTask by tasks.register<VersionGenerationTask>("myTask")
Output I get when I attempt to sync my gradle config
Could not create task ':myTask'.
Could not create task of type 'VersionGenerationTask'.
Class Build_gradle.VersionGenerationTask is a non-static inner class.
You have a similar problem as OP, yes.
But it is totally unrelated to the injecting of execOperations, and I already explained what the problem is.
You are referencing something from outside the class inside the class, in your case baseVersion, which makes the class non-static as it references the script instance to get that variable.
Also it is not really a good idea to create such classes in your build scripts, better put them to buildSrc or - what I prefer - an included build, then such errors also cannot happen.