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 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); } ```