Ability to download artifacts sources, javadoc, tests and tests sources

This commit is contained in:
BoykoAlex
2016-11-15 19:30:25 -05:00
parent 3e9f097c2f
commit 34f56ae882
4 changed files with 105 additions and 4 deletions

View File

@@ -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<ArtifactRepository> 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<ArtifactRepository> remoteRepositories,
MavenExecutionRequest executionRequest) throws MavenException {
if (remoteRepositories == null) {
try {
remoteRepositories = getArtifactRepositories();
} catch (MavenException e) {
// we've tried
remoteRepositories = Collections.emptyList();
}
}
final List<ArtifactRepository> _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<ArtifactRepository> 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);
}
}
}

View File

@@ -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<Path> 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()));

View File

@@ -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();
}

View File

@@ -1,2 +1,2 @@
**/classpath.txt
**/bin/**