Update AetherGrapeEngine to honour --local

When running an application, --local can be used to collect the
application's dependencies in a local directory. Prior to
AetherGrapeEngine being introduced, using --local would result in the
dependencies being written to ./grapes. When AetherGrapeEngine was
introduced --local no longer had any effect.

This commit updates AetherGrapeEngine so that it honours --local,
writing its dependencies to ./repository. When --local is not specified
dependencies are written to ~/.m2/repository (the standard location
for the local Maven cache). As part of this change TestCommand has
been refactored so that it lazily initialises its GroovyCompiler. This
ensures that RunCommand has a chance to set the system property that
backs --local before AetherGrapeEngine is initialised and accesses the
property.

Fixes #99
This commit is contained in:
Andy Wilkinson 2013-11-04 11:01:52 +00:00
parent e741557a38
commit b631c113ba
3 changed files with 19 additions and 23 deletions

View File

@ -141,8 +141,18 @@ public class AetherGrapeEngine implements GrapeEngine {
}
});
LocalRepository localRepo = new LocalRepository(new File(
System.getProperty("user.home"), ".m2/repository"));
String grapeRootProperty = System.getProperty("grape.root");
File root;
if (grapeRootProperty != null && grapeRootProperty.trim().length() > 0) {
root = new File(grapeRootProperty);
}
else {
root = new File(System.getProperty("user.home"), ".m2");
}
LocalRepository localRepo = new LocalRepository(new File(root, "repository"));
repositorySystemSession.setLocalRepositoryManager(this.repositorySystem
.newLocalRepositoryManager(repositorySystemSession, localRepo));

View File

@ -76,7 +76,7 @@ public class RunCommand extends OptionParsingCommand {
protected void options() {
this.watchOption = option("watch", "Watch the specified file for changes");
this.localOption = option("local",
"Accumulate the dependencies in a local folder (./grapes)");
"Accumulate the dependencies in a local folder (./repository)");
this.editOption = option(asList("edit", "e"),
"Open the file with the default system editor");
this.noGuessImportsOption = option("no-guess-imports",

View File

@ -27,7 +27,6 @@ import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import joptsimple.OptionSet;
@ -46,11 +45,8 @@ import org.springframework.boot.cli.util.FileUtils;
*/
public class TestCommand extends OptionParsingCommand {
private TestOptionHandler testOptionHandler;
public TestCommand() {
super("test", "Test a groovy script", new TestOptionHandler());
this.testOptionHandler = (TestOptionHandler) this.getHandler();
}
@Override
@ -59,7 +55,7 @@ public class TestCommand extends OptionParsingCommand {
}
public TestResults getResults() {
return this.testOptionHandler.results;
return ((TestOptionHandler) this.getHandler()).results;
}
private static class TestGroovyCompilerConfiguration implements
@ -79,27 +75,17 @@ public class TestCommand extends OptionParsingCommand {
public String getClasspath() {
return "";
}
public Level getLogLevel() {
return Level.INFO;
}
}
private static class TestOptionHandler extends OptionHandler {
private final GroovyCompiler compiler;
private TestResults results;
public TestOptionHandler() {
TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration();
this.compiler = new GroovyCompiler(configuration);
if (configuration.getLogLevel().intValue() <= Level.FINE.intValue()) {
System.setProperty("groovy.grape.report.downloads", "true");
}
}
@Override
protected void run(OptionSet options) throws Exception {
TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration();
GroovyCompiler compiler = new GroovyCompiler(configuration);
FileOptions fileOptions = new FileOptions(options, getClass()
.getClassLoader());
@ -114,13 +100,13 @@ public class TestCommand extends OptionParsingCommand {
// Compile - Pass 1 - compile source code to see what test libraries were
// pulled in
Object[] sources = this.compiler.sources(fileOptions.getFilesArray());
Object[] sources = compiler.sources(fileOptions.getFilesArray());
List<File> testerFiles = compileAndCollectTesterFiles(sources);
// Compile - Pass 2 - add appropriate testers
List<File> files = new ArrayList<File>(fileOptions.getFiles());
files.addAll(testerFiles);
sources = this.compiler.sources(files.toArray(new File[files.size()]));
sources = compiler.sources(files.toArray(new File[files.size()]));
if (sources.length == 0) {
throw new RuntimeException("No classes found in '" + files + "'");
}