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
public interface JavaExecable extends Serializable, com.diffplug.common.base.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 callexec(Project, JavaExecable)
, - Write the `JavaExecable` to a temporary file usingSerializable
. - 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 Classes Modifier and Type Interface Description static class
JavaExecable.Mode
Encapsulates whether something is run internally or externally.static class
JavaExecable.PlugAction
static interface
JavaExecable.PlugParameters
-
Field Summary
Fields Modifier and Type Field Description static String
BUILDSCRIPT_CLASSPATH
-
Method Summary
Static Methods Modifier and Type Method Description static <T extends JavaExecable>
Texec(Project project, T input)
static <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 void
main(String[] args)
Main which works in conjunction withexec(Project, JavaExecable, Action)
.
-
-
-
Field Detail
-
BUILDSCRIPT_CLASSPATH
static final String BUILDSCRIPT_CLASSPATH
- See Also:
- Constant Field Values
-
-
Method Detail
-
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
static <T extends JavaExecable> T exec(Project project, T input) throws Throwable
- Throws:
Throwable
- See Also:
exec(Project, JavaExecable, Action)
-
execWithoutGradle
static <T extends JavaExecable> T execWithoutGradle(T input, Action<JavaExecSpec> settings) throws Throwable
- Throws:
Throwable
- See Also:
exec(Project, JavaExecable, Action)
-
execWithoutGradle
static <T extends JavaExecable> T execWithoutGradle(T input) throws Throwable
- Throws:
Throwable
- See Also:
exec(Project, JavaExecable, Action)
-
main
static void main(String[] args) throws IOException
Main which works in conjunction withexec(Project, JavaExecable, Action)
.- Throws:
IOException
-
-