Polish
This commit is contained in:
@@ -20,6 +20,9 @@ import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Resolve artifact identifiers (typically in the form {@literal group:artifact:version})
|
||||
* to {@link File}s.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@@ -28,7 +31,6 @@ interface DependencyResolver {
|
||||
/**
|
||||
* Resolves the given {@code artifactIdentifiers}, typically in the form
|
||||
* "group:artifact:version", and their dependencies.
|
||||
*
|
||||
* @param artifactIdentifiers The artifacts to resolve
|
||||
* @return The {@code File}s for the resolved artifacts
|
||||
* @throws Exception if dependency resolution fails
|
||||
|
||||
@@ -55,11 +55,9 @@ class GroovyGrabDependencyResolver implements DependencyResolver {
|
||||
groovyCompiler.compile(createSources(artifactIdentifiers));
|
||||
List<URL> artifactUrls = getClassPathUrls(groovyCompiler);
|
||||
artifactUrls.removeAll(initialUrls);
|
||||
|
||||
for (URL artifactUrl : artifactUrls) {
|
||||
artifactFiles.add(toFile(artifactUrl));
|
||||
}
|
||||
|
||||
}
|
||||
return artifactFiles;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.boot.cli.command.OptionParsingCommand;
|
||||
import org.springframework.boot.cli.command.options.CompilerOptionHandler;
|
||||
import org.springframework.boot.cli.command.status.ExitStatus;
|
||||
import org.springframework.boot.cli.util.Log;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link Command} to install additional dependencies into the CLI.
|
||||
@@ -47,24 +48,18 @@ public class InstallCommand extends OptionParsingCommand {
|
||||
private static final class InstallOptionHandler extends CompilerOptionHandler {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ExitStatus run(OptionSet options) throws Exception {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> args = (List<String>) options.nonOptionArguments();
|
||||
|
||||
if (args.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Please specify at least one dependency, in the form group:artifact:version, to install");
|
||||
}
|
||||
|
||||
Assert.notEmpty(args, "Please specify at least one "
|
||||
+ "dependency, in the form group:artifact:version, to install");
|
||||
try {
|
||||
new Installer(options, this).install(args);
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
Log.error(message != null ? message : e.getClass().toString());
|
||||
catch (Exception ex) {
|
||||
String message = ex.getMessage();
|
||||
Log.error(message != null ? message : ex.getClass().toString());
|
||||
}
|
||||
|
||||
return ExitStatus.OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.SystemPropertyUtils;
|
||||
|
||||
/**
|
||||
* Shared logic for the {@link InstallCommand} and {@link UninstallCommand}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@@ -44,13 +46,13 @@ class Installer {
|
||||
|
||||
private final Properties installCounts;
|
||||
|
||||
Installer(OptionSet options, CompilerOptionHandler compilerOptionHandler)
|
||||
public Installer(OptionSet options, CompilerOptionHandler compilerOptionHandler)
|
||||
throws IOException {
|
||||
this(new GroovyGrabDependencyResolver(createCompilerConfiguration(options,
|
||||
compilerOptionHandler)));
|
||||
}
|
||||
|
||||
Installer(DependencyResolver resolver) throws IOException {
|
||||
public Installer(DependencyResolver resolver) throws IOException {
|
||||
this.dependencyResolver = resolver;
|
||||
this.installCounts = loadInstallCounts();
|
||||
}
|
||||
@@ -59,7 +61,6 @@ class Installer {
|
||||
OptionSet options, CompilerOptionHandler compilerOptionHandler) {
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
|
||||
return new OptionSetGroovyCompilerConfiguration(options, compilerOptionHandler,
|
||||
repositoryConfiguration) {
|
||||
@Override
|
||||
@@ -69,17 +70,14 @@ class Installer {
|
||||
};
|
||||
}
|
||||
|
||||
private static Properties loadInstallCounts() throws IOException {
|
||||
|
||||
private Properties loadInstallCounts() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
|
||||
File installed = getInstalled();
|
||||
if (installed.exists()) {
|
||||
FileReader reader = new FileReader(installed);
|
||||
properties.load(reader);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
@@ -96,9 +94,7 @@ class Installer {
|
||||
public void install(List<String> artifactIdentifiers) throws Exception {
|
||||
File libDirectory = getDefaultLibDirectory();
|
||||
libDirectory.mkdirs();
|
||||
|
||||
Log.info("Installing into: " + libDirectory);
|
||||
|
||||
List<File> artifactFiles = this.dependencyResolver.resolve(artifactIdentifiers);
|
||||
for (File artifactFile : artifactFiles) {
|
||||
int installCount = getInstallCount(artifactFile);
|
||||
@@ -108,7 +104,6 @@ class Installer {
|
||||
}
|
||||
setInstallCount(artifactFile, installCount + 1);
|
||||
}
|
||||
|
||||
saveInstallCounts();
|
||||
}
|
||||
|
||||
@@ -131,9 +126,7 @@ class Installer {
|
||||
|
||||
public void uninstall(List<String> artifactIdentifiers) throws Exception {
|
||||
File libDirectory = getDefaultLibDirectory();
|
||||
|
||||
Log.info("Uninstalling from: " + libDirectory);
|
||||
|
||||
List<File> artifactFiles = this.dependencyResolver.resolve(artifactIdentifiers);
|
||||
for (File artifactFile : artifactFiles) {
|
||||
int installCount = getInstallCount(artifactFile);
|
||||
@@ -142,31 +135,27 @@ class Installer {
|
||||
}
|
||||
setInstallCount(artifactFile, installCount - 1);
|
||||
}
|
||||
|
||||
saveInstallCounts();
|
||||
}
|
||||
|
||||
public void uninstallAll() throws Exception {
|
||||
File libDirectory = getDefaultLibDirectory();
|
||||
|
||||
Log.info("Uninstalling from: " + libDirectory);
|
||||
|
||||
for (String name : this.installCounts.stringPropertyNames()) {
|
||||
new File(libDirectory, name).delete();
|
||||
}
|
||||
|
||||
this.installCounts.clear();
|
||||
saveInstallCounts();
|
||||
}
|
||||
|
||||
private static File getDefaultLibDirectory() {
|
||||
private File getDefaultLibDirectory() {
|
||||
String home = SystemPropertyUtils
|
||||
.resolvePlaceholders("${spring.home:${SPRING_HOME:.}}");
|
||||
final File lib = new File(home, "lib");
|
||||
return lib;
|
||||
return new File(home, "lib");
|
||||
}
|
||||
|
||||
private static File getInstalled() {
|
||||
private File getInstalled() {
|
||||
return new File(getDefaultLibDirectory(), ".installed");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,34 +56,28 @@ public class UninstallCommand extends OptionParsingCommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected ExitStatus run(OptionSet options) throws Exception {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> args = (List<String>) options.nonOptionArguments();
|
||||
|
||||
try {
|
||||
if (options.has(this.allOption)) {
|
||||
if (!args.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Please use --all without specifying any dependencies");
|
||||
}
|
||||
|
||||
new Installer(options, this).uninstallAll();
|
||||
}
|
||||
if (args.isEmpty()) {
|
||||
throw new IllegalArgumentException(
|
||||
"Please specify at least one dependency, in the form group:artifact:version, to uninstall");
|
||||
}
|
||||
|
||||
new Installer(options, this).uninstall(args);
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
Log.error(message != null ? message : e.getClass().toString());
|
||||
catch (Exception ex) {
|
||||
String message = ex.getMessage();
|
||||
Log.error(message != null ? message : ex.getClass().toString());
|
||||
}
|
||||
|
||||
return ExitStatus.OK;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.codehaus.groovy.control.customizers.ImportCustomizer;
|
||||
import org.springframework.boot.cli.compiler.AstUtils;
|
||||
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
|
||||
import org.springframework.boot.cli.compiler.DependencyCustomizer;
|
||||
import org.springframework.boot.groovy.EnableRabbitMessaging;
|
||||
|
||||
/**
|
||||
* {@link CompilerAutoConfiguration} for Spring Rabbit.
|
||||
@@ -46,6 +45,7 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
|
||||
imports.addStarImports("org.springframework.amqp.rabbit.annotation",
|
||||
"org.springframework.amqp.rabbit.core",
|
||||
@@ -54,7 +54,7 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
|
||||
"org.springframework.amqp.rabbit.listener",
|
||||
"org.springframework.amqp.rabbit.listener.adapter",
|
||||
"org.springframework.amqp.core").addImports(
|
||||
EnableRabbitMessaging.class.getCanonicalName());
|
||||
org.springframework.boot.groovy.EnableRabbitMessaging.class.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ public class GroovyGrabDependencyResolverTests {
|
||||
public String[] getClasspath() {
|
||||
return new String[] { "." };
|
||||
}
|
||||
|
||||
};
|
||||
this.resolver = new GroovyGrabDependencyResolver(configuration);
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ import org.springframework.util.FileSystemUtils;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for {@link Installer}
|
||||
@@ -65,22 +65,17 @@ public class InstallerTests {
|
||||
@Test
|
||||
public void installNewDependency() throws Exception {
|
||||
File foo = createTemporaryFile("foo.jar");
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("foo"))).thenReturn(Arrays.asList(foo));
|
||||
given(this.resolver.resolve(Arrays.asList("foo"))).willReturn(Arrays.asList(foo));
|
||||
this.installer.install(Arrays.asList("foo"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(), containsInAnyOrder("foo.jar", ".installed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void installAndUninstall() throws Exception {
|
||||
File foo = createTemporaryFile("foo.jar");
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("foo"))).thenReturn(Arrays.asList(foo));
|
||||
|
||||
given(this.resolver.resolve(Arrays.asList("foo"))).willReturn(Arrays.asList(foo));
|
||||
this.installer.install(Arrays.asList("foo"));
|
||||
this.installer.uninstall(Arrays.asList("foo"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(), contains(".installed"));
|
||||
}
|
||||
|
||||
@@ -89,30 +84,24 @@ public class InstallerTests {
|
||||
File alpha = createTemporaryFile("alpha.jar");
|
||||
File bravo = createTemporaryFile("bravo.jar");
|
||||
File charlie = createTemporaryFile("charlie.jar");
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("bravo"))).thenReturn(
|
||||
given(this.resolver.resolve(Arrays.asList("bravo"))).willReturn(
|
||||
Arrays.asList(bravo, alpha));
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("charlie"))).thenReturn(
|
||||
given(this.resolver.resolve(Arrays.asList("charlie"))).willReturn(
|
||||
Arrays.asList(charlie, alpha));
|
||||
|
||||
this.installer.install(Arrays.asList("bravo"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(),
|
||||
containsInAnyOrder("alpha.jar", "bravo.jar", ".installed"));
|
||||
|
||||
this.installer.install(Arrays.asList("charlie"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(),
|
||||
containsInAnyOrder("alpha.jar", "bravo.jar", "charlie.jar", ".installed"));
|
||||
|
||||
this.installer.uninstall(Arrays.asList("bravo"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(),
|
||||
containsInAnyOrder("alpha.jar", "charlie.jar", ".installed"));
|
||||
|
||||
this.installer.uninstall(Arrays.asList("charlie"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(), containsInAnyOrder(".installed"));
|
||||
}
|
||||
|
||||
@@ -122,20 +111,17 @@ public class InstallerTests {
|
||||
File bravo = createTemporaryFile("bravo.jar");
|
||||
File charlie = createTemporaryFile("charlie.jar");
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("bravo"))).thenReturn(
|
||||
given(this.resolver.resolve(Arrays.asList("bravo"))).willReturn(
|
||||
Arrays.asList(bravo, alpha));
|
||||
|
||||
when(this.resolver.resolve(Arrays.asList("charlie"))).thenReturn(
|
||||
given(this.resolver.resolve(Arrays.asList("charlie"))).willReturn(
|
||||
Arrays.asList(charlie, alpha));
|
||||
|
||||
this.installer.install(Arrays.asList("bravo"));
|
||||
this.installer.install(Arrays.asList("charlie"));
|
||||
|
||||
assertThat(getNamesOfFilesInLib(),
|
||||
containsInAnyOrder("alpha.jar", "bravo.jar", "charlie.jar", ".installed"));
|
||||
|
||||
this.installer.uninstallAll();
|
||||
|
||||
assertThat(getNamesOfFilesInLib(), containsInAnyOrder(".installed"));
|
||||
}
|
||||
|
||||
@@ -152,4 +138,5 @@ public class InstallerTests {
|
||||
temporaryFile.deleteOnExit();
|
||||
return temporaryFile;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link DependencyCustomizer}
|
||||
@@ -56,13 +56,12 @@ public class DependencyCustomizerTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
when(this.resolver.getGroupId("spring-boot-starter-logging")).thenReturn(
|
||||
given(this.resolver.getGroupId("spring-boot-starter-logging")).willReturn(
|
||||
"org.springframework.boot");
|
||||
when(this.resolver.getArtifactId("spring-boot-starter-logging")).thenReturn(
|
||||
given(this.resolver.getArtifactId("spring-boot-starter-logging")).willReturn(
|
||||
"spring-boot-starter-logging");
|
||||
when(this.resolver.getVersion("spring-boot-starter-logging")).thenReturn("1.2.3");
|
||||
|
||||
given(this.resolver.getVersion("spring-boot-starter-logging"))
|
||||
.willReturn("1.2.3");
|
||||
this.moduleNode.addClass(this.classNode);
|
||||
this.dependencyCustomizer = new DependencyCustomizer(new GroovyClassLoader(
|
||||
getClass().getClassLoader()), this.moduleNode,
|
||||
@@ -169,4 +168,5 @@ public class DependencyCustomizerTests {
|
||||
private Object getMemberValue(AnnotationNode annotationNode, String member) {
|
||||
return ((ConstantExpression) annotationNode.getMember(member)).getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ import org.springframework.boot.cli.compiler.dependencies.ArtifactCoordinatesRes
|
||||
import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for {@link ResolveDependencyCoordinatesTransformation}
|
||||
@@ -73,9 +73,9 @@ public final class ResolveDependencyCoordinatesTransformationTests {
|
||||
|
||||
@Before
|
||||
public void setupExpectations() {
|
||||
when(this.coordinatesResolver.getGroupId("spring-core")).thenReturn(
|
||||
given(this.coordinatesResolver.getGroupId("spring-core")).willReturn(
|
||||
"org.springframework");
|
||||
when(this.coordinatesResolver.getVersion("spring-core")).thenReturn("4.0.0.RC1");
|
||||
given(this.coordinatesResolver.getVersion("spring-core")).willReturn("4.0.0.RC1");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user