From 34f56ae882564ffc31829a39726b3ad758bfcc5c Mon Sep 17 00:00:00 2001 From: BoykoAlex Date: Tue, 15 Nov 2016 19:30:25 -0500 Subject: [PATCH] Ability to download artifacts sources, javadoc, tests and tests sources --- .../ide/vscode/commons/maven/MavenBridge.java | 77 +++++++++++++++++++ .../ide/vscode/commons/maven/MavenCore.java | 21 +++++ .../vscode/commons/maven/MavenException.java | 9 ++- .../commons/project-test-harness/.gitignore | 2 +- 4 files changed, 105 insertions(+), 4 deletions(-) diff --git a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenBridge.java b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenBridge.java index 59e7a1670..969269d77 100644 --- a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenBridge.java +++ b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenBridge.java @@ -12,9 +12,11 @@ package org.springframework.ide.vscode.commons.maven; import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -28,6 +30,7 @@ import java.util.Properties; import org.apache.maven.DefaultMaven; import org.apache.maven.Maven; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -95,6 +98,9 @@ import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.dag.CycleDetectedException; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; import org.slf4j.ILoggerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -800,5 +806,76 @@ class MavenBridge { } } } + + public Artifact resolve(String groupId, String artifactId, String version, String type, String classifier, + List remoteRepositories, MavenExecutionRequest request) throws MavenException { + Artifact artifact = lookup(RepositorySystem.class).createArtifactWithClassifier(groupId, artifactId, version, + type, classifier); + + return resolve(artifact, remoteRepositories, request); + } + + public Artifact resolve(final Artifact artifact, List remoteRepositories, + MavenExecutionRequest executionRequest) throws MavenException { + if (remoteRepositories == null) { + try { + remoteRepositories = getArtifactRepositories(); + } catch (MavenException e) { + // we've tried + remoteRepositories = Collections.emptyList(); + } + } + final List _remoteRepositories = remoteRepositories; + + org.eclipse.aether.RepositorySystem repoSystem = lookup(org.eclipse.aether.RepositorySystem.class); + + ArtifactRequest request = new ArtifactRequest(); + request.setArtifact(RepositoryUtils.toArtifact(artifact)); + request.setRepositories(RepositoryUtils.toRepos(_remoteRepositories)); + + ArtifactResult result; + try { + result = repoSystem.resolveArtifact(createRepositorySession(executionRequest), request); + } catch (ArtifactResolutionException ex) { + result = ex.getResults().get(0); + } + + setLastUpdated(executionRequest.getLocalRepository(), _remoteRepositories, artifact); + + if (result.isResolved()) { + artifact.selectVersion(result.getArtifact().getVersion()); + artifact.setFile(result.getArtifact().getFile()); + artifact.setResolved(true); + } else { + throw new MavenException(result.getExceptions().toArray(new Exception[result.getExceptions().size()])); + } + + return artifact; + } + + /* package */void setLastUpdated(ArtifactRepository localRepository, List remoteRepositories, + Artifact artifact) throws MavenException { + + Properties lastUpdated = loadLastUpdated(localRepository, artifact); + + String timestamp = Long.toString(System.currentTimeMillis()); + + for (ArtifactRepository repository : remoteRepositories) { + lastUpdated.setProperty(getLastUpdatedKey(repository, artifact), timestamp); + } + + File lastUpdatedFile = getLastUpdatedFile(localRepository, artifact); + try { + lastUpdatedFile.getParentFile().mkdirs(); + BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(lastUpdatedFile)); + try { + lastUpdated.store(os, null); + } finally { + IOUtil.close(os); + } + } catch (IOException ex) { + throw new MavenException(ex); + } + } } diff --git a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenCore.java b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenCore.java index 52ea4578a..acf5d7ea1 100644 --- a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenCore.java +++ b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenCore.java @@ -65,6 +65,11 @@ import com.google.common.base.Suppliers; */ public class MavenCore { + private static final String CLASSIFIER_SOURCES = "sources"; + private static final String CLASSIFIER_JAVADOC = "javadoc"; + private static final String CLASSIFIER_TESTS = "tests"; + private static final String CLASSIFIER_TESTSOURCES = "test-sources"; + public static final String JAVA_IO_TMPDIR = "java.io.tmpdir"; private static final String JAVA_HOME = "java.home"; private static final String JAVA_RUNTIME_VERSION = "java.runtime.version"; @@ -235,6 +240,22 @@ public class MavenCore { return artifacts; } + public Artifact getSources(Artifact artifact) throws MavenException { + return maven.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), CLASSIFIER_SOURCES, null, maven.createExecutionRequest()); + } + + public Artifact getJavadoc(Artifact artifact) throws MavenException { + return maven.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), CLASSIFIER_JAVADOC, null, maven.createExecutionRequest()); + } + + public Artifact getTests(Artifact artifact) throws MavenException { + return maven.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), CLASSIFIER_TESTS, null, maven.createExecutionRequest()); + } + + public Artifact getTestSources(Artifact artifact) throws MavenException { + return maven.resolve(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), CLASSIFIER_TESTSOURCES, null, maven.createExecutionRequest()); + } + public Stream getJreLibs() throws MavenException { String s = (String) maven.createExecutionRequest().getSystemProperties().get(JAVA_BOOT_CLASS_PATH); return Arrays.stream(s.split(File.pathSeparator)).map(File::new).filter(f -> f.canRead()).map(f -> Paths.get(f.toURI())); diff --git a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenException.java b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenException.java index 40dc239cd..f28bcbe5e 100644 --- a/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenException.java +++ b/vscode-extensions/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenException.java @@ -10,6 +10,9 @@ *******************************************************************************/ package org.springframework.ide.vscode.commons.maven; +import java.util.Arrays; +import java.util.stream.Collectors; + /** * Wrapper for Maven exceptions * @@ -20,13 +23,13 @@ public class MavenException extends Exception { private static final long serialVersionUID = 1L; - private Throwable t; + private Throwable[] t; public MavenException() { super(); } - public MavenException(Throwable t) { + public MavenException(Throwable... t) { super(); this.t = t; } @@ -34,7 +37,7 @@ public class MavenException extends Exception { @Override public String getMessage() { if (t != null) { - return t.getMessage(); + return String.join("\n", Arrays.stream(t).map(t -> t.getMessage()).collect(Collectors.toList())); } return super.getMessage(); } diff --git a/vscode-extensions/commons/project-test-harness/.gitignore b/vscode-extensions/commons/project-test-harness/.gitignore index 10c9f8a89..145500786 100644 --- a/vscode-extensions/commons/project-test-harness/.gitignore +++ b/vscode-extensions/commons/project-test-harness/.gitignore @@ -1,2 +1,2 @@ **/classpath.txt - +**/bin/**