use of org.eclipse.aether.version.VersionConstraint in project fabric8 by jboss-fuse.
the class AetherBasedResolver method resolve.
private File resolve(List<LocalRepository> defaultRepos, List<RemoteRepository> remoteRepos, Artifact artifact) throws IOException {
if (artifact.getExtension().isEmpty()) {
artifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "jar", artifact.getVersion());
}
// Try with default repositories
try {
VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(artifact.getVersion());
if (vc.getVersion() != null) {
for (LocalRepository repo : defaultRepos) {
if (vc.getVersion().toString().endsWith("SNAPSHOT") && !handlesSnapshot(repo)) {
continue;
}
DefaultRepositorySystemSession session = newSession(repo);
try {
return m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, null, null)).getArtifact().getFile();
} catch (ArtifactResolutionException e) {
// Ignore
} finally {
releaseSession(session);
}
}
}
} catch (InvalidVersionSpecificationException e) {
// Should not happen
}
DefaultRepositorySystemSession session = newSession(null);
try {
artifact = resolveLatestVersionRange(session, remoteRepos, artifact);
ArtifactResult result = m_repoSystem.resolveArtifact(session, new ArtifactRequest(artifact, remoteRepos, null));
File resolved = result.getArtifact().getFile();
LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
return resolved;
} catch (ArtifactResolutionException e) {
// we know there's one ArtifactResult, because there was one ArtifactRequest
ArtifactResolutionException original = new ArtifactResolutionException(e.getResults(), "Error resolving artifact " + artifact.toString(), null);
original.setStackTrace(e.getStackTrace());
List<String> messages = new ArrayList<>(e.getResult().getExceptions().size());
List<Exception> suppressed = new ArrayList<>();
for (Exception ex : e.getResult().getExceptions()) {
messages.add(ex.getMessage() == null ? ex.getClass().getName() : ex.getMessage());
suppressed.add(ex);
}
IOException exception = new IOException(original.getMessage() + ": " + messages, original);
for (Exception ex : suppressed) {
exception.addSuppressed(ex);
}
LOG.warn(exception.getMessage(), exception);
for (Exception ex : suppressed) {
LOG.warn(" - " + ex.getMessage());
}
throw exception;
} catch (RepositoryException e) {
throw new IOException("Error resolving artifact " + artifact.toString(), e);
} finally {
releaseSession(session);
}
}
use of org.eclipse.aether.version.VersionConstraint in project fabric8 by jboss-fuse.
the class ReplaceConflictingVersionResolver method selectVersion.
private void selectVersion(DependencyNode node, DependencyNode parent, int depth, Map<DependencyNode, Integer> depths, ConflictGroup group, Map<?, ?> conflictIds) throws RepositoryException {
Integer smallestDepth = depths.get(node);
if (smallestDepth == null || smallestDepth > depth) {
depths.put(node, depth);
} else {
return;
}
Object key = conflictIds.get(node);
if (group.key.equals(key)) {
Position pos = new Position(parent, depth);
if (parent != null) {
group.positions.add(pos);
}
if (!group.isAcceptable(node.getVersion())) {
return;
}
group.candidates.put(node, pos);
VersionConstraint versionConstraint = node.getVersionConstraint();
if (versionConstraint != null && versionConstraint.getRange() != null && isNotEmpty(versionConstraint.getRange())) {
group.constraints.add(versionConstraint);
}
if (group.version == null || isNearer(pos, node.getVersion(), group.position, group.version)) {
group.version = node.getVersion();
group.versionDependency = node;
group.position = pos;
}
if (!group.isAcceptable(group.version)) {
group.version = null;
group.versionDependency = null;
for (Iterator<Map.Entry<DependencyNode, Position>> it = group.candidates.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<DependencyNode, Position> entry = it.next();
Version version = entry.getKey().getVersion();
pos = entry.getValue();
if (!group.isAcceptable(version)) {
it.remove();
} else if (group.version == null || isNearer(pos, version, group.position, group.version)) {
group.version = version;
group.versionDependency = entry.getKey();
group.position = pos;
}
}
if (group.version == null) {
Collection<String> versions = new LinkedHashSet<String>();
for (VersionConstraint constraint : group.constraints) {
versions.add(constraint.toString());
}
// TODO, FIXME
throw new UnsolvableVersionConflictException(Collections.<List<DependencyNode>>emptyList());
}
}
}
for (DependencyNode child : node.getChildren()) {
selectVersion(child, node, depth + 1, depths, group, conflictIds);
}
}
use of org.eclipse.aether.version.VersionConstraint in project fabric8 by jboss-fuse.
the class MavenProxyServletSupport method download.
@Override
public File download(String path) throws InvalidMavenArtifactRequest {
if (path == null) {
throw new InvalidMavenArtifactRequest();
}
Matcher artifactMatcher = ARTIFACT_REQUEST_URL_REGEX.matcher(path);
Matcher metdataGaMatcher = ARTIFACT_GA_METADATA_URL_REGEX.matcher(path);
if (metdataGaMatcher.matches()) {
LOGGER.info("Received request for maven metadata : {}", path);
Metadata metadata = null;
try {
metadata = convertPathToMetadata(path);
// Only handle xxx/maven-metadata.xml requests
if (!"maven-metadata.xml".equals(metadata.getType()) || metdataGaMatcher.group(7) != null) {
return null;
}
if (isHostedRepository()) {
// do not resolve metadata via Aether - simply look it up in hosted storage
// This is important, because `mvn deploy` or `mvn fabric8:deploy` goals, even if they PUT
// artifacts and metadata also GET metadata (not artifacts)
File hostedMetadata = new File(uploadRepository, path);
LOGGER.debug("Getting metadata from hosted storage : {}", hostedMetadata);
if (hostedMetadata.isFile()) {
File tmpFile = Files.createTempFile(runtimeProperties.getDataPath());
Files.copy(hostedMetadata, tmpFile);
return tmpFile;
}
// never look up in io.fabric8.maven.defaultRepositories or io.fabric8.maven.repositories
return null;
}
// Try with default repositories - first metadata found is returned
VersionConstraint vc = new GenericVersionScheme().parseVersionConstraint(metadata.getVersion());
if (vc.getVersion() != null) {
for (LocalRepository repo : resolver.getDefaultRepositories()) {
if (vc.getVersion().toString().endsWith("SNAPSHOT") && !resolver.handlesSnapshot(repo)) {
continue;
}
// clone session to swap local repository manager
DefaultRepositorySystemSession localSession = new DefaultRepositorySystemSession(session);
localSession.setLocalRepositoryManager(system.newLocalRepositoryManager(localSession, repo));
LOGGER.debug("Getting metadata from default repository : {}", repo.getBasedir());
List<MetadataResult> results = system.resolveMetadata(localSession, Collections.singletonList(new MetadataRequest(metadata, null, null)));
File file = processMetadataResults(metadata, results);
if (file != null) {
return file;
}
}
}
// fallback to remote repositories - this time results are merged
List<MetadataRequest> requests = new ArrayList<>();
for (RemoteRepository repository : repositories) {
MetadataRequest request = new MetadataRequest(metadata, repository, null);
request.setFavorLocalRepository(false);
requests.add(request);
}
MetadataRequest request = new MetadataRequest(metadata, null, null);
request.setFavorLocalRepository(true);
requests.add(request);
List<MetadataResult> results = system.resolveMetadata(session, requests);
File result = processMetadataResults(metadata, results);
if (result != null) {
return result;
}
} catch (Exception e) {
LOGGER.warn(String.format("Could not find metadata : %s due to %s", metadata, e.getMessage()), e);
return null;
}
// If no matching metadata found return nothing
return null;
} else if (artifactMatcher.matches()) {
LOGGER.info("Received request for maven artifact : {}", path);
Artifact artifact = convertPathToArtifact(path);
try {
if (artifact.getExtension() != null && (artifact.getExtension().endsWith(".sha1") || artifact.getExtension().endsWith(".md5"))) {
return null;
}
File download = resolver.resolveFile(artifact);
File tmpFile = Files.createTempFile(runtimeProperties.getDataPath());
Files.copy(download, tmpFile);
return tmpFile;
} catch (Exception e) {
LOGGER.warn(String.format("Could not find artifact : %s due to %s", artifact, e.getMessage()), e);
return null;
}
}
return null;
}
Aggregations