diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CompilerOptionHandler.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CompilerOptionHandler.java index dce0d79c8b9..159d43baacd 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CompilerOptionHandler.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CompilerOptionHandler.java @@ -25,6 +25,7 @@ import static java.util.Arrays.asList; * Groovy scripts * * @author Andy Wilkinson + * @author Dave Syer */ public class CompilerOptionHandler extends OptionHandler { @@ -32,6 +33,8 @@ public class CompilerOptionHandler extends OptionHandler { private OptionSpec noGuessDependenciesOption; + private OptionSpec autoconfigureOption; + private OptionSpec classpathOption; @Override @@ -40,6 +43,9 @@ public class CompilerOptionHandler extends OptionHandler { "Do not attempt to guess imports"); this.noGuessDependenciesOption = option("no-guess-dependencies", "Do not attempt to guess dependencies"); + this.autoconfigureOption = option("autoconfigure", + "Add autoconfigure compiler transformations").withOptionalArg() + .ofType(Boolean.class).defaultsTo(true); this.classpathOption = option(asList("classpath", "cp"), "Additional classpath entries").withRequiredArg(); doOptions(); @@ -60,4 +66,8 @@ public class CompilerOptionHandler extends OptionHandler { return this.classpathOption; } + public OptionSpec getAutoconfigureOption() { + return this.autoconfigureOption; + } + } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/GrabCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/GrabCommand.java index 28cde0dfd79..f1a3b8f0bdb 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/GrabCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/GrabCommand.java @@ -49,8 +49,6 @@ public class GrabCommand extends OptionParsingCommand { List repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); - repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File( - getM2HomeDirectory(), "repository").toURI(), true)); GroovyCompilerConfiguration configuration = new GroovyCompilerConfigurationAdapter( options, this, repositoryConfiguration); @@ -59,16 +57,33 @@ public class GrabCommand extends OptionParsingCommand { System.setProperty("grape.root", "."); } + if (!new File(System.getProperty("grape.root")).equals(getM2HomeDirectory())) { + GrabCommand.addDefaultCacheAsRespository(repositoryConfiguration); + } GroovyCompiler groovyCompiler = new GroovyCompiler(configuration); groovyCompiler.compile(fileOptions.getFilesArray()); } - private File getM2HomeDirectory() { - String mavenRoot = System.getProperty("maven.home"); - if (StringUtils.hasLength(mavenRoot)) { - return new File(mavenRoot); - } - return new File(System.getProperty("user.home"), ".m2"); - } } + + /** + * Add the default local M2 cache directory as a remote repository. Only do this if + * the local cache location has been changed from the default. + * + * @param repositoryConfiguration + */ + public static void addDefaultCacheAsRespository( + List repositoryConfiguration) { + repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File( + getM2HomeDirectory(), "repository").toURI(), true)); + } + + private static File getM2HomeDirectory() { + String mavenRoot = System.getProperty("maven.home"); + if (StringUtils.hasLength(mavenRoot)) { + return new File(mavenRoot); + } + return new File(System.getProperty("user.home"), ".m2"); + } + } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/RunCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/RunCommand.java index 78667de4e52..587a3b5595d 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/RunCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/RunCommand.java @@ -17,7 +17,6 @@ package org.springframework.boot.cli.command; import java.awt.Desktop; -import java.io.File; import java.util.List; import java.util.logging.Level; @@ -89,8 +88,6 @@ public class RunCommand extends OptionParsingCommand { List repositoryConfiguration = RepositoryConfigurationFactory .createDefaultRepositoryConfiguration(); - repositoryConfiguration.add(0, new RepositoryConfiguration("local", new File( - "repository").toURI(), true)); SpringApplicationRunnerConfiguration configuration = new SpringApplicationRunnerConfigurationAdapter( options, this, repositoryConfiguration); diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ScriptCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ScriptCommand.java index a615134d0b3..4fe7e8ca83b 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ScriptCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ScriptCommand.java @@ -264,6 +264,11 @@ public class ScriptCommand implements Command { return true; } + @Override + public boolean isAutoconfigure() { + return true; + } + @Override public boolean isGuessDependencies() { return true; diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java index d575a56e0f4..6564775af6a 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.ServiceLoader; @@ -77,7 +78,7 @@ public class GroovyCompiler { private final ExtendedGroovyClassLoader loader; - private final ServiceLoader compilerAutoConfigurations; + private final Iterable compilerAutoConfigurations; private final List transformations; @@ -96,8 +97,14 @@ public class GroovyCompiler { this.loader.getConfiguration().addCompilationCustomizers( new CompilerAutoConfigureCustomizer()); - this.compilerAutoConfigurations = ServiceLoader.load( - CompilerAutoConfiguration.class, GroovyCompiler.class.getClassLoader()); + if (configuration.isAutoconfigure()) { + this.compilerAutoConfigurations = ServiceLoader.load( + CompilerAutoConfiguration.class, + GroovyCompiler.class.getClassLoader()); + } + else { + this.compilerAutoConfigurations = Collections.emptySet(); + } this.transformations = new ArrayList(); this.transformations.add(new GrabResolversAutoConfigurationTransformation()); diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java index 233869c12ed..2a8873f1820 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java @@ -48,6 +48,11 @@ public interface GroovyCompilerConfiguration { */ boolean isGuessDependencies(); + /** + * Returns true if autoconfiguration transformations should be applied. + */ + boolean isAutoconfigure(); + /** * @return a path for local resources */ diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfigurationAdapter.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfigurationAdapter.java index b2688e5c4d0..9f8a5636a88 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfigurationAdapter.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompilerConfigurationAdapter.java @@ -71,6 +71,11 @@ public class GroovyCompilerConfigurationAdapter implements GroovyCompilerConfigu return !this.options.has(this.optionHandler.getNoGuessDependenciesOption()); } + @Override + public boolean isAutoconfigure() { + return this.optionHandler.getAutoconfigureOption().value(this.options); + } + @Override public String[] getClasspath() { OptionSpec classpathOption = this.optionHandler.getClasspathOption(); diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java index df560c3b10c..8e8f04d0731 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java @@ -24,6 +24,7 @@ import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration; /** * @author Andy Wilkinson + * @author Dave Syer */ public final class RepositoryConfigurationFactory { @@ -51,4 +52,5 @@ public final class RepositoryConfigurationFactory { return repositoryConfiguration; } + } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index 2f1121b2884..7dac98e0f62 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -98,7 +98,12 @@ public class CliTester implements TestRule { final String[] sources = new String[args.length]; for (int i = 0; i < args.length; i++) { String arg = args[i]; - sources[i] = this.prefix + arg; + if (arg.startsWith("--")) { + sources[i] = arg; + } + else { + sources[i] = this.prefix + arg; + } } return sources; } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/GrabCommandIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/GrabCommandIntegrationTests.java index 32879abc37b..b16658aefd8 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/GrabCommandIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/GrabCommandIntegrationTests.java @@ -31,6 +31,7 @@ import static org.junit.Assert.assertTrue; * Integration tests for {@link GrabCommand} * * @author Andy Wilkinson + * @author Dave Syer */ public class GrabCommandIntegrationTests { @@ -41,22 +42,21 @@ public class GrabCommandIntegrationTests { @After public void deleteLocalRepository() { FileSystemUtils.deleteRecursively(new File("target/repository")); + System.clearProperty("grape.root"); + System.clearProperty("groovy.grape.report.downloads"); } @Test public void grab() throws Exception { + System.setProperty("grape.root", "target"); System.setProperty("groovy.grape.report.downloads", "true"); - try { - String output = this.cli.grab("grab.groovy"); - assertTrue(output.contains("Downloading: file:")); - assertTrue(new File("target/repository/org/springframework/spring-jdbc") - .isDirectory()); - } - finally { - System.clearProperty("grape.root"); - System.clearProperty("groovy.grape.report.downloads"); - } + // Use --autoconfigure=false to limit the amount of downloaded dependencies + String output = this.cli.grab("grab.groovy", "--autoconfigure=false"); + assertTrue(output.contains("Downloading: file:")); + assertTrue(new File("target/repository/net/sf/jopt-simple/jopt-simple") + .isDirectory()); + } } diff --git a/spring-boot-cli/src/test/resources/grab-samples/grab.groovy b/spring-boot-cli/src/test/resources/grab-samples/grab.groovy index 05a3b9d5fd8..7e592389e3c 100644 --- a/spring-boot-cli/src/test/resources/grab-samples/grab.groovy +++ b/spring-boot-cli/src/test/resources/grab-samples/grab.groovy @@ -1,4 +1,4 @@ -@Grab('spring-jdbc') +@Grab('jopt-simple') class GrabTest { } \ No newline at end of file