Commit df476bed authored by Dave Syer's avatar Dave Syer

Ensure local repository cache is always used

parent 47a0df1e
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.boot.cli.command; package org.springframework.boot.cli.command;
import java.io.File;
import java.util.List; import java.util.List;
import joptsimple.OptionSet; import joptsimple.OptionSet;
...@@ -27,7 +26,6 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration; ...@@ -27,7 +26,6 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter; import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter;
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory; import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration; import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import org.springframework.util.StringUtils;
/** /**
* {@link Command} to grab the dependencies of one or more Groovy scripts * {@link Command} to grab the dependencies of one or more Groovy scripts
...@@ -57,33 +55,10 @@ public class GrabCommand extends OptionParsingCommand { ...@@ -57,33 +55,10 @@ public class GrabCommand extends OptionParsingCommand {
System.setProperty("grape.root", "."); System.setProperty("grape.root", ".");
} }
if (!new File(System.getProperty("grape.root")).equals(getM2HomeDirectory())) {
GrabCommand.addDefaultCacheAsRespository(repositoryConfiguration);
}
GroovyCompiler groovyCompiler = new GroovyCompiler(configuration); GroovyCompiler groovyCompiler = new GroovyCompiler(configuration);
groovyCompiler.compile(fileOptions.getFilesArray()); groovyCompiler.compile(fileOptions.getFilesArray());
} }
} }
/**
* 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) {
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");
}
} }
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
package org.springframework.boot.cli.compiler; package org.springframework.boot.cli.compiler;
import java.io.File;
import java.net.URI; import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration; import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import org.springframework.util.StringUtils;
/** /**
* Factory used to create {@link RepositoryConfiguration}s. * Factory used to create {@link RepositoryConfiguration}s.
...@@ -52,7 +54,31 @@ public final class RepositoryConfigurationFactory { ...@@ -52,7 +54,31 @@ public final class RepositoryConfigurationFactory {
repositoryConfiguration.add(SPRING_MILESTONE); repositoryConfiguration.add(SPRING_MILESTONE);
} }
addDefaultCacheAsRespository(repositoryConfiguration);
return repositoryConfiguration; return repositoryConfiguration;
} }
/**
* 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) {
RepositoryConfiguration repository = new RepositoryConfiguration("local",
new File(getM2HomeDirectory(), "repository").toURI(), true);
if (!repositoryConfiguration.contains(repository)) {
repositoryConfiguration.add(0, repository);
}
}
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");
}
} }
...@@ -25,6 +25,7 @@ import java.net.URI; ...@@ -25,6 +25,7 @@ import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -95,7 +96,10 @@ public class AetherGrapeEngine implements GrapeEngine { ...@@ -95,7 +96,10 @@ public class AetherGrapeEngine implements GrapeEngine {
session.setProxySelector(this.proxySelector); session.setProxySelector(this.proxySelector);
this.session = session; this.session = session;
this.repositories = new ArrayList<RemoteRepository>(); this.repositories = new ArrayList<RemoteRepository>();
for (RemoteRepository repository : remoteRepositories) { List<RemoteRepository> remotes = new ArrayList<RemoteRepository>(
remoteRepositories);
Collections.reverse(remotes); // priority is reversed in addRepository
for (RemoteRepository repository : remotes) {
addRepository(repository); addRepository(repository);
} }
this.progressReporter = getProgressReporter(session); this.progressReporter = getProgressReporter(session);
......
...@@ -52,6 +52,12 @@ public final class RepositoryConfiguration { ...@@ -52,6 +52,12 @@ public final class RepositoryConfiguration {
return this.name; return this.name;
} }
@Override
public String toString() {
return "RepositoryConfiguration [name=" + this.name + ", uri=" + this.uri
+ ", snapshotsEnabled=" + this.snapshotsEnabled + "]";
}
/** /**
* @return the uri of the repository * @return the uri of the repository
*/ */
...@@ -67,4 +73,30 @@ public final class RepositoryConfiguration { ...@@ -67,4 +73,30 @@ public final class RepositoryConfiguration {
return this.snapshotsEnabled; return this.snapshotsEnabled;
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RepositoryConfiguration other = (RepositoryConfiguration) obj;
if (this.name == null) {
if (other.name != null)
return false;
}
else if (!this.name.equals(other.name))
return false;
return true;
}
} }
\ No newline at end of file
...@@ -88,8 +88,14 @@ public class CliTester implements TestRule { ...@@ -88,8 +88,14 @@ public class CliTester implements TestRule {
return Executors.newSingleThreadExecutor().submit(new Callable<T>() { return Executors.newSingleThreadExecutor().submit(new Callable<T>() {
@Override @Override
public T call() throws Exception { public T call() throws Exception {
command.run(sources); ClassLoader loader = Thread.currentThread().getContextClassLoader();
return command; try {
command.run(sources);
return command;
}
finally {
Thread.currentThread().setContextClassLoader(loader);
}
} }
}); });
} }
......
...@@ -54,9 +54,8 @@ public class GrabCommandIntegrationTests { ...@@ -54,9 +54,8 @@ public class GrabCommandIntegrationTests {
// Use --autoconfigure=false to limit the amount of downloaded dependencies // Use --autoconfigure=false to limit the amount of downloaded dependencies
String output = this.cli.grab("grab.groovy", "--autoconfigure=false"); 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") assertTrue(new File("target/repository/net/sf/jopt-simple/jopt-simple")
.isDirectory()); .isDirectory());
assertTrue(output.contains("Downloading: file:"));
} }
} }
...@@ -20,6 +20,7 @@ import java.util.Arrays; ...@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Set; import java.util.Set;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -60,8 +61,16 @@ public class SpringCliTests { ...@@ -60,8 +61,16 @@ public class SpringCliTests {
private Set<Call> calls = EnumSet.noneOf(Call.class); private Set<Call> calls = EnumSet.noneOf(Call.class);
private ClassLoader loader;
@After
public void close() {
Thread.currentThread().setContextClassLoader(this.loader);
}
@Before @Before
public void setup() { public void setup() {
this.loader = Thread.currentThread().getContextClassLoader();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
this.cli = new SpringCli() { this.cli = new SpringCli() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment