From b1c5b30de88eec844ca6912566569aedd19263be Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 22 Jul 2013 16:20:19 +0100 Subject: [PATCH] Add --classpath to CLI --- spring-cli/samples/ui.groovy | 22 +++++++++++++++++++ .../cli/command/RunCommand.java | 12 ++++++++++ .../cli/command/ScriptCommand.java | 5 +++++ .../cli/compiler/GroovyCompiler.java | 3 +++ .../compiler/GroovyCompilerConfiguration.java | 5 +++++ .../cli/SampleIntegrationTests.java | 6 +---- .../{ => static}/css/bootstrap.min.css | 0 7 files changed, 48 insertions(+), 5 deletions(-) rename spring-cli/src/test/resources/{ => static}/css/bootstrap.min.css (100%) diff --git a/spring-cli/samples/ui.groovy b/spring-cli/samples/ui.groovy index 044c4cd344..6a84cc0802 100644 --- a/spring-cli/samples/ui.groovy +++ b/spring-cli/samples/ui.groovy @@ -8,4 +8,26 @@ class Example { return "home"; } +} + +@Configuration +@Log +class MvcConfiguration extends WebMvcConfigurerAdapter { + + @Override + void addInterceptors(def registry) { + log.info("Registering temporary file interceptor") + registry.addInterceptor(temporaryFileInterceptor()) + } + + @Bean + HandlerInterceptor temporaryFileInterceptor() { + log.info("Creating temporary file interceptor") + new HandlerInterceptorAdapter() { + @Override + postHandle(def request, def response, def handler, ModelAndView mav) { + log.info("Model: " + model) + } + } + } } \ No newline at end of file diff --git a/spring-cli/src/main/java/org/springframework/cli/command/RunCommand.java b/spring-cli/src/main/java/org/springframework/cli/command/RunCommand.java index 4947a200b6..45799e8b77 100644 --- a/spring-cli/src/main/java/org/springframework/cli/command/RunCommand.java +++ b/spring-cli/src/main/java/org/springframework/cli/command/RunCommand.java @@ -71,6 +71,8 @@ public class RunCommand extends OptionParsingCommand { private OptionSpec localOption; + private OptionSpec classpathOption; + private SpringApplicationRunner runner; @Override @@ -86,6 +88,8 @@ public class RunCommand extends OptionParsingCommand { "Do not attempt to guess dependencies"); this.verboseOption = option(asList("verbose", "v"), "Verbose logging"); this.quietOption = option(asList("quiet", "q"), "Quiet logging"); + this.classpathOption = option(asList("classpath", "cp"), + "Additional classpath entries").withRequiredArg(); } @Override @@ -177,6 +181,14 @@ public class RunCommand extends OptionParsingCommand { return Level.INFO; } + @Override + public String getClasspath() { + if (this.options.has(RunOptionHandler.this.classpathOption)) { + return this.options.valueOf(RunOptionHandler.this.classpathOption); + } + return ""; + } + } } diff --git a/spring-cli/src/main/java/org/springframework/cli/command/ScriptCommand.java b/spring-cli/src/main/java/org/springframework/cli/command/ScriptCommand.java index f6394660f4..fe943132cf 100644 --- a/spring-cli/src/main/java/org/springframework/cli/command/ScriptCommand.java +++ b/spring-cli/src/main/java/org/springframework/cli/command/ScriptCommand.java @@ -240,6 +240,11 @@ public class ScriptCommand implements Command { return true; } + @Override + public String getClasspath() { + return ""; + } + } } diff --git a/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompiler.java b/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompiler.java index a200a0d2c7..034185fcf8 100644 --- a/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompiler.java +++ b/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompiler.java @@ -72,6 +72,9 @@ public class GroovyCompiler { CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); this.loader = new ExtendedGroovyClassLoader(getClass().getClassLoader(), compilerConfiguration); + if (configuration.getClasspath().length() > 0) { + this.loader.addClasspath(configuration.getClasspath()); + } // FIXME: allow the extra resolvers to be switched on (off by default) addExtraResolvers(); compilerConfiguration diff --git a/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompilerConfiguration.java b/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompilerConfiguration.java index 8a1376db0f..fe52fbf036 100644 --- a/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompilerConfiguration.java +++ b/spring-cli/src/main/java/org/springframework/cli/compiler/GroovyCompilerConfiguration.java @@ -33,4 +33,9 @@ public interface GroovyCompilerConfiguration { */ boolean isGuessDependencies(); + /** + * @return a path for local resources (colon separated) + */ + String getClasspath(); + } diff --git a/spring-cli/src/test/java/org/springframework/cli/SampleIntegrationTests.java b/spring-cli/src/test/java/org/springframework/cli/SampleIntegrationTests.java index 28b9473f94..79c9646437 100644 --- a/spring-cli/src/test/java/org/springframework/cli/SampleIntegrationTests.java +++ b/spring-cli/src/test/java/org/springframework/cli/SampleIntegrationTests.java @@ -28,7 +28,6 @@ import org.apache.ivy.util.FileUtil; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.springframework.cli.command.RunCommand; @@ -142,14 +141,11 @@ public class SampleIntegrationTests { } @Test - @Ignore public void uiSample() throws Exception { // FIXME Failing on OSX - // To run this one from the command line you need to add target/test-classes to - // CLASSPATH - start("samples/ui.groovy"); + start("samples/ui.groovy", "--classpath=.:src/test/resources"); String result = FileUtil.readEntirely(new URL("http://localhost:8080") .openStream()); assertTrue("Wrong output: " + result, result.contains("Hello World")); diff --git a/spring-cli/src/test/resources/css/bootstrap.min.css b/spring-cli/src/test/resources/static/css/bootstrap.min.css similarity index 100% rename from spring-cli/src/test/resources/css/bootstrap.min.css rename to spring-cli/src/test/resources/static/css/bootstrap.min.css