Ensure local repository cache is always used

This commit is contained in:
Dave Syer 2014-01-03 09:56:11 +00:00
parent 47a0df1e3d
commit df476bed1f
7 changed files with 81 additions and 30 deletions

View File

@ -16,7 +16,6 @@
package org.springframework.boot.cli.command;
import java.io.File;
import java.util.List;
import joptsimple.OptionSet;
@ -27,7 +26,6 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter;
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
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
@ -57,33 +55,10 @@ 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());
}
}
/**
* 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");
}
}

View File

@ -16,11 +16,13 @@
package org.springframework.boot.cli.compiler;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import org.springframework.util.StringUtils;
/**
* Factory used to create {@link RepositoryConfiguration}s.
@ -52,7 +54,31 @@ public final class RepositoryConfigurationFactory {
repositoryConfiguration.add(SPRING_MILESTONE);
}
addDefaultCacheAsRespository(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");
}
}

View File

@ -25,6 +25,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -95,7 +96,10 @@ public class AetherGrapeEngine implements GrapeEngine {
session.setProxySelector(this.proxySelector);
this.session = session;
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);
}
this.progressReporter = getProgressReporter(session);

View File

@ -52,6 +52,12 @@ public final class RepositoryConfiguration {
return this.name;
}
@Override
public String toString() {
return "RepositoryConfiguration [name=" + this.name + ", uri=" + this.uri
+ ", snapshotsEnabled=" + this.snapshotsEnabled + "]";
}
/**
* @return the uri of the repository
*/
@ -67,4 +73,30 @@ public final class RepositoryConfiguration {
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;
}
}

View File

@ -88,8 +88,14 @@ public class CliTester implements TestRule {
return Executors.newSingleThreadExecutor().submit(new Callable<T>() {
@Override
public T call() throws Exception {
command.run(sources);
return command;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
command.run(sources);
return command;
}
finally {
Thread.currentThread().setContextClassLoader(loader);
}
}
});
}

View File

@ -54,9 +54,8 @@ public class GrabCommandIntegrationTests {
// 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());
assertTrue(output.contains("Downloading: file:"));
}
}

View File

@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -60,8 +61,16 @@ public class SpringCliTests {
private Set<Call> calls = EnumSet.noneOf(Call.class);
private ClassLoader loader;
@After
public void close() {
Thread.currentThread().setContextClassLoader(this.loader);
}
@Before
public void setup() {
this.loader = Thread.currentThread().getContextClassLoader();
MockitoAnnotations.initMocks(this);
this.cli = new SpringCli() {