use of org.eclipse.aether.RepositorySystem in project bazel by bazelbuild.
the class MavenDownloader method download.
/**
* Download the Maven artifact to the output directory. Returns the path to the jar.
*/
public Path download(String name, WorkspaceAttributeMapper mapper, Path outputDirectory, MavenServerValue serverValue) throws IOException, EvalException {
this.name = name;
this.outputDirectory = outputDirectory;
String url = serverValue.getUrl();
Server server = serverValue.getServer();
Artifact artifact;
String artifactId = mapper.get("artifact", Type.STRING);
String sha1 = mapper.isAttributeValueExplicitlySpecified("sha1") ? mapper.get("sha1", Type.STRING) : null;
if (sha1 != null && !KeyType.SHA1.isValid(sha1)) {
throw new IOException("Invalid SHA-1 for maven_jar " + name + ": '" + sha1 + "'");
}
try {
artifact = new DefaultArtifact(artifactId);
} catch (IllegalArgumentException e) {
throw new IOException(e.getMessage());
}
boolean isCaching = repositoryCache.isEnabled() && KeyType.SHA1.isValid(sha1);
if (isCaching) {
Path downloadPath = getDownloadDestination(artifact);
Path cachedDestination = repositoryCache.get(sha1, downloadPath, KeyType.SHA1);
if (cachedDestination != null) {
return cachedDestination;
}
}
MavenConnector connector = new MavenConnector(outputDirectory.getPathString());
RepositorySystem system = connector.newRepositorySystem();
RepositorySystemSession session = connector.newRepositorySystemSession(system);
RemoteRepository repository = new RemoteRepository.Builder(name, MavenServerValue.DEFAULT_ID, url).setAuthentication(new MavenAuthentication(server)).build();
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(ImmutableList.of(repository));
try {
ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
artifact = artifactResult.getArtifact();
} catch (ArtifactResolutionException e) {
throw new IOException("Failed to fetch Maven dependency: " + e.getMessage());
}
Path downloadPath = outputDirectory.getRelative(artifact.getFile().getAbsolutePath());
// Verify checksum.
if (!Strings.isNullOrEmpty(sha1)) {
RepositoryCache.assertFileChecksum(sha1, downloadPath, KeyType.SHA1);
}
if (isCaching) {
repositoryCache.put(sha1, downloadPath, KeyType.SHA1);
}
return downloadPath;
}
use of org.eclipse.aether.RepositorySystem in project spring-boot by spring-projects.
the class ModifiedClassPathRunner method resolveCoordinates.
private List<URL> resolveCoordinates(String[] coordinates) throws Exception {
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
RepositorySystem repositorySystem = serviceLocator.getService(RepositorySystem.class);
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(new RemoteRepository.Builder("central", "default", "http://central.maven.org/maven2").build()));
collectRequest.setDependencies(createDependencies(coordinates));
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
List<URL> resolvedArtifacts = new ArrayList<URL>();
for (ArtifactResult artifact : result.getArtifactResults()) {
resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
}
return resolvedArtifacts;
}
use of org.eclipse.aether.RepositorySystem in project spf4j by zolyfarkas.
the class MavenRepositoryUtils method getVersions.
public static List<Version> getVersions(final List<RemoteRepository> repos, final File localRepo, final String groupId, final String artifactId, final String versionExpr) throws VersionRangeResolutionException {
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession session = getRepositorySystemSession(repositorySystem, localRepo);
return getVersions(groupId, artifactId, versionExpr, repos, repositorySystem, session);
}
use of org.eclipse.aether.RepositorySystem in project spf4j by zolyfarkas.
the class MavenRepositoryUtils method getRepositorySystem.
@Nonnull
public static RepositorySystem getRepositorySystem() {
DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class, FileTransporterFactory.class);
serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
serviceLocator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
@Override
public void serviceCreationFailed(final Class<?> type, final Class<?> impl, final Throwable exception) {
LOG.error("Error creating service {}, {}", new Object[] { type, impl, exception });
}
});
RepositorySystem service = serviceLocator.getService(RepositorySystem.class);
if (service == null) {
throw new IllegalStateException("No repository system in " + serviceLocator);
}
return service;
}
use of org.eclipse.aether.RepositorySystem in project spf4j by zolyfarkas.
the class MavenRepositoryUtils method resolveArtifact.
public static File resolveArtifact(final List<RemoteRepository> repos, final File localRepo, final String groupId, final String artifactId, final String classifier, final String extension, final String versionExpr) throws ArtifactResolutionException {
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession session = getRepositorySystemSession(repositorySystem, localRepo);
return resolveArtifact(groupId, artifactId, classifier, extension, versionExpr, repos, repositorySystem, session);
}
Aggregations