diff --git a/spring-bootstrap-cli/samples/runner.groovy b/spring-bootstrap-cli/samples/runner.groovy new file mode 100644 index 0000000000..a5598ac968 --- /dev/null +++ b/spring-bootstrap-cli/samples/runner.groovy @@ -0,0 +1,10 @@ +package org.test + +class Runner implements CommandLineRunner { + + void run(String... args) { + print "Hello World!" + } +} + + diff --git a/spring-bootstrap-cli/samples/runner.xml b/spring-bootstrap-cli/samples/runner.xml new file mode 100644 index 0000000000..2a8f5aad1a --- /dev/null +++ b/spring-bootstrap-cli/samples/runner.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java index 2c5d30893f..fefdbcaf94 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java @@ -22,6 +22,7 @@ import groovy.lang.GroovyClassLoader.ClassCollector; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.codehaus.groovy.ast.ClassNode; @@ -81,6 +82,21 @@ public class GroovyCompiler { .addCompilationCustomizers(new CompilerAutoConfigureCustomizer()); } + public Object[] sources(File[] files) throws CompilationFailedException, IOException { + List compilables = new ArrayList(); + List others = new ArrayList(); + for (File file : files) { + if (file.getName().endsWith(".groovy") || file.getName().endsWith(".java")) { + compilables.add(file); + } else { + others.add(file); + } + } + Class[] compiled = compile(compilables.toArray(new File[compilables.size()])); + others.addAll(0, Arrays.asList(compiled)); + return others.toArray(new Object[others.size()]); + } + /** * Compile the specified Groovy source files, applying any * {@link CompilerAutoConfiguration}s. All classes defined in the files will be diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/runner/BootstrapRunner.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/runner/BootstrapRunner.java index b5b86a1011..b18a48032e 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/runner/BootstrapRunner.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/runner/BootstrapRunner.java @@ -75,13 +75,13 @@ public class BootstrapRunner { stop(); // Compile - Class[] classes = this.compiler.compile(this.files); - if (classes.length == 0) { + Object[] sources = this.compiler.sources(this.files); + if (sources.length == 0) { throw new RuntimeException("No classes found in '" + this.files + "'"); } // Run in new thread to ensure that the context classloader is setup - this.runThread = new RunThread(classes); + this.runThread = new RunThread(sources); this.runThread.start(); this.runThread.join(); @@ -106,18 +106,18 @@ public class BootstrapRunner { */ private class RunThread extends Thread { - private final Class[] classes; + private final Object[] sources; private Object applicationContext; /** * Create a new {@link RunThread} instance. - * @param classes the classes to launch + * @param sources the sources to launch */ - public RunThread(Class... classes) { - this.classes = classes; - if (classes.length != 0) { - setContextClassLoader(classes[0].getClassLoader()); + public RunThread(Object... sources) { + this.sources = sources; + if (sources.length != 0 && sources[0] instanceof Class) { + setContextClassLoader(((Class) sources[0]).getClassLoader()); } setDaemon(true); } @@ -130,7 +130,7 @@ public class BootstrapRunner { "org.springframework.bootstrap.SpringApplication"); Method method = application.getMethod("run", Object[].class, String[].class); - this.applicationContext = method.invoke(null, this.classes, + this.applicationContext = method.invoke(null, this.sources, BootstrapRunner.this.args); } catch (Exception ex) { ex.printStackTrace(); diff --git a/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SampleIntegrationTests.java b/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SampleIntegrationTests.java index edc15b4335..006b6a2e06 100644 --- a/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SampleIntegrationTests.java +++ b/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SampleIntegrationTests.java @@ -134,4 +134,11 @@ public class SampleIntegrationTests { assertTrue("Wrong output: " + output, output.contains("Hello, World")); } + @Test + public void xmlSample() throws Exception { + start("samples/app.xml", "samples/runner.groovy"); + String output = getOutput(); + assertTrue("Wrong output: " + output, output.contains("Hello World")); + } + }