Search in sources :

Example 1 with VersionConstraint

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);
    }
}
Also used : VersionConstraint(org.eclipse.aether.version.VersionConstraint) LocalRepository(org.eclipse.aether.repository.LocalRepository) RepositoryException(org.eclipse.aether.RepositoryException) IOException(java.io.IOException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) File(java.io.File) JarFile(java.util.jar.JarFile) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 2 with VersionConstraint

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VersionConstraint(org.eclipse.aether.version.VersionConstraint) Version(org.eclipse.aether.version.Version) DependencyNode(org.eclipse.aether.graph.DependencyNode) TreeMap(java.util.TreeMap) Map(java.util.Map) UnsolvableVersionConflictException(org.eclipse.aether.collection.UnsolvableVersionConflictException)

Example 3 with VersionConstraint

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;
}
Also used : VersionConstraint(org.eclipse.aether.version.VersionConstraint) Matcher(java.util.regex.Matcher) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.eclipse.aether.metadata.Metadata) LocalRepository(org.eclipse.aether.repository.LocalRepository) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) MetadataResult(org.eclipse.aether.resolution.MetadataResult) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

VersionConstraint (org.eclipse.aether.version.VersionConstraint)3 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JarFile (java.util.jar.JarFile)2 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 LocalRepository (org.eclipse.aether.repository.LocalRepository)2 GenericVersionScheme (org.eclipse.aether.util.version.GenericVersionScheme)2 FileNotFoundException (java.io.FileNotFoundException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Matcher (java.util.regex.Matcher)1