public interface JavaExecable extends Serializable, Throwing.Runnable
Easy way to execute code from a Gradle plugin in a separate JVM.
Create an class which implements JavaExecable
. It should have some fields which are input, some fields which are output, and a run()
method.
Here’s what happens when you call exec(Project, JavaExecable)
,
JavaExecable
to a temporary file using Serializable
.JavaExecable
from the temp file, calls run(), writes it back to the temp file, then exits.If the JavaExecable
happens to throw an exception, it will be transparently rethrown within the calling thread.
Example usage:
static class Incrementer implements JavaExecable {
private static final long serialVersionUID = -5728572785844814830L;
int input;
int output;
Incrementer(int input) {
this.input = input;
}
public int getOutput() {
return output;
}
public void run() throws Throwable {
output = input + 1;
}
}
// obvious
public void testInternal() {
Incrementer example = new Incrementer(5);
example.run();
Assert.assertEquals(6, example.output);
}
// magic!
public void testExternal() throws Throwable {
Incrementer example = new Incrementer(5);
Incrementer result = JavaExecable.execWithoutGradle(example);
Assert.assertEquals(6, result.output);
}
Modifier and Type | Interface and Description |
---|---|
static class |
JavaExecable.Mode
Encapsulates whether something is run internally or externally.
|
Modifier and Type | Field and Description |
---|---|
static String |
BUILDSCRIPT_CLASSPATH |
Modifier and Type | Method and Description |
---|---|
static <T extends JavaExecable> |
exec(Project project,
T input) |
static <T extends JavaExecable> |
exec(Project project,
T input,
Action<JavaExecSpec> settings) |
static <T extends JavaExecable> |
execWithoutGradle(T input) |
static <T extends JavaExecable> |
execWithoutGradle(T input,
Action<JavaExecSpec> settings) |
static void |
main(String[] args)
Main which works in conjunction with
exec(Project, JavaExecable, Action) . |
run
static final String BUILDSCRIPT_CLASSPATH
static <T extends JavaExecable> T exec(Project project, T input, Action<JavaExecSpec> settings) throws Throwable
project
- the project on which we’ll call Project.javaexec(Action)
.input
- the JavaExecable which we’ll take as input and call run() on.settings
- any extra settings you’d like to set on the JavaExec (e.g. heap)Throwable
static <T extends JavaExecable> T exec(Project project, T input) throws Throwable
Throwable
exec(Project, JavaExecable, Action)
static <T extends JavaExecable> T execWithoutGradle(T input, Action<JavaExecSpec> settings) throws Throwable
Throwable
exec(Project, JavaExecable, Action)
static <T extends JavaExecable> T execWithoutGradle(T input) throws Throwable
Throwable
exec(Project, JavaExecable, Action)
static void main(String[] args) throws IOException
Main which works in conjunction with exec(Project, JavaExecable, Action)
.
IOException