Package com.diffplug.gradle
Interface JavaExecable
- All Superinterfaces:
Serializable,com.diffplug.common.base.Throwing.Runnable,com.diffplug.common.base.Throwing.Specific.Runnable<Throwable>
- All Known Implementing Classes:
JavaExecableTestIncrementer
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),
- Write the `JavaExecable` to a temporary file using Serializable.
- Launch a new JVM with the same classpath as the project's buildscript, and pass the location of that temp file.
- The new JVM loads the `JavaExecable` from the temp file, calls run(), writes it back to the temp file, then exits.
- Back in gradle, we deserialize the tempfile and return the result.
If the `JavaExecable` happens to throw an exception, it will be transparently
rethrown within the calling thread.
Example usage:
```java
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);
}
```-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumEncapsulates whether something is run internally or externally.static classstatic interface -
Field Summary
Fields -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <T extends JavaExecable>
Tstatic <T extends JavaExecable>
Texec(Project project, T input, Action<JavaExecSpec> settings) static <T extends JavaExecable>
Texec(org.gradle.workers.WorkQueue queue, T input) static <T extends JavaExecable>
TexecWithoutGradle(T input) static <T extends JavaExecable>
TexecWithoutGradle(T input, Action<JavaExecSpec> settings) static voidMain which works in conjunction withexec(Project, JavaExecable, Action).Methods inherited from interface com.diffplug.common.base.Throwing.Specific.Runnable
run
-
Field Details
-
BUILDSCRIPT_CLASSPATH
- See Also:
-
-
Method Details
-
exec
static <T extends JavaExecable> T exec(org.gradle.workers.WorkQueue queue, T input) throws Throwable - Throws:
Throwable
-
exec
static <T extends JavaExecable> T exec(Project project, T input, Action<JavaExecSpec> settings) throws Throwable - Parameters:
project- the project on which we'll callProject.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)- Returns:
- the JavaExecable after it has had run() called.
- Throws:
Throwable
-
exec
- Throws:
Throwable- See Also:
-
execWithoutGradle
static <T extends JavaExecable> T execWithoutGradle(T input, Action<JavaExecSpec> settings) throws Throwable - Throws:
Throwable- See Also:
-
execWithoutGradle
- Throws:
Throwable- See Also:
-
main
Main which works in conjunction withexec(Project, JavaExecable, Action).- Throws:
IOException
-