ProcessBuilder or Exec for calling external compiler from WorkAction?

In writing a plugin for a custom compiler (linux), inside a WorkAction (Gradle API 8.10), should I use a ProcessBuilder (Java SE 17 & JDK 17) or should I use an instance of Exec (Gradle API 8.10)?

I will need to consume stdout and stderr so the process does not block, and I need to get the exit status, as well as parse the contents of stdout and stderr. Multiple invocations should also work in parallel, and everything must halt itself if an error occurs (no point in keeping the other parallel invocation going if one fails). I looked at how Gradle supports Cpp but the code is too hard to follow for me to draw my own conclusion. Thanks in advance for any advice.

I don’t think there is really a good way to abort already scheduled work if one of the work-items failed.

An instance of Exec you cannot use, Exec is a task.
But if you meant injecting ExecOperations and using exec { ... } on it, then yes, I’d prefer that over a plain ProcessBuilder.
You get an ExecResult from which you can get the exit code if you need more than “fail if != 0”.
You can set the standard streams to anything you need to provide and consume the content as necessary, …

The parallel execution you already have by using the worker api, that is one of its major benefits.

1 Like