Using Project.javaexec from java?

I’m attempting to write a plugin for a specific source generation tool (think grammar/lexer generator) , and I would like to call the tool using Project.javaexec(Closure) from within a subclass of SourceTask.

However, I can’t quite figure out how to create a closure to customize a JavaExecSpec from java. Is this even possible?

You can use an Action instead

It’s basically the same thing as a Closure, but let’s you keep your code in pure Java.

I was afraid of that :slight_smile:

I’m tweaking an existing plugin that’s using an older version of gradle, and I’m trying to avoid bumping up the required gradle version. The javaexec override that accepts an Action seems to have been added more recently than what the plugin is using.

So it sounds like I’ll either have to switch to groovy for the SourceTask subclass so I can pass in a Closure, or bump up the gradle version so I can use an Action.

You can also use a MethodClosure, it’s just a little more cumbersome.

Perfect, thanks! I was able to get it working using a MethodClosure.

I ended up using a slightly different approach:

getProject().javaexec(new Closure(this) {
            public void doCall(JavaExecSpec javaExecSpec) {