Search in sources :

Example 1 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project buck by facebook.

the class Resolver method getRunTimeTransitiveDeps.

private ImmutableMap<String, Artifact> getRunTimeTransitiveDeps(Iterable<Dependency> mavenCoords) throws RepositoryException {
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRequestContext(JavaScopes.RUNTIME);
    collectRequest.setRepositories(repos);
    for (Dependency dep : mavenCoords) {
        collectRequest.addDependency(dep);
    }
    DependencyFilter filter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, filter);
    DependencyResult dependencyResult = repoSys.resolveDependencies(session, dependencyRequest);
    ImmutableSortedMap.Builder<String, Artifact> knownDeps = ImmutableSortedMap.naturalOrder();
    for (ArtifactResult artifactResult : dependencyResult.getArtifactResults()) {
        Artifact node = artifactResult.getArtifact();
        knownDeps.put(buildKey(node), node);
    }
    return knownDeps.build();
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) STGroupString(org.stringtemplate.v4.STGroupString) CollectRequest(org.eclipse.aether.collection.CollectRequest) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 2 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project druid by druid-io.

the class PullDependencies method downloadExtension.

/**
   * Download the extension given its maven coordinate
   *
   * @param versionedArtifact The maven artifact of the extension
   * @param toLocation        The location where this extension will be downloaded to
   */
private void downloadExtension(Artifact versionedArtifact, File toLocation) {
    final CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(versionedArtifact, JavaScopes.RUNTIME));
    final DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, DependencyFilterUtils.andFilter(DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME), new DependencyFilter() {

        @Override
        public boolean accept(DependencyNode node, List<DependencyNode> parents) {
            String scope = node.getDependency().getScope();
            if (scope != null) {
                scope = scope.toLowerCase();
                if (scope.equals("provided")) {
                    return false;
                }
                if (scope.equals("test")) {
                    return false;
                }
                if (scope.equals("system")) {
                    return false;
                }
            }
            if (accept(node.getArtifact())) {
                return false;
            }
            for (DependencyNode parent : parents) {
                if (accept(parent.getArtifact())) {
                    return false;
                }
            }
            return true;
        }

        private boolean accept(final Artifact artifact) {
            return exclusions.contains(artifact.getGroupId());
        }
    }));
    try {
        log.info("Start downloading extension [%s]", versionedArtifact);
        final List<Artifact> artifacts = aether.resolveArtifacts(dependencyRequest);
        for (Artifact artifact : artifacts) {
            if (!exclusions.contains(artifact.getGroupId())) {
                log.info("Adding file [%s] at [%s]", artifact.getFile().getName(), toLocation.getAbsolutePath());
                FileUtils.copyFileToDirectory(artifact.getFile(), toLocation);
            } else {
                log.debug("Skipped Artifact[%s]", artifact);
            }
        }
    } catch (Exception e) {
        log.error(e, "Unable to resolve artifacts for [%s].", dependencyRequest);
        throw Throwables.propagate(e);
    }
    log.info("Finish downloading extension [%s]", versionedArtifact);
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project launcher by runelite.

the class ArtifactResolver method resolveArtifacts.

public List<ArtifactResult> resolveArtifacts(Artifact artifact) throws DependencyResolutionException {
    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system);
    DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
    collectRequest.setRepositories(repositories);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
    List<ArtifactResult> results = system.resolveDependencies(session, dependencyRequest).getArtifactResults();
    // check to see if they're from the right repository
    validate(results);
    return results;
}
Also used : RepositorySystem(org.eclipse.aether.RepositorySystem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 4 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project mule by mulesoft.

the class AetherClassPathClassifier method buildPluginUrlClassification.

/**
 * Classifies an {@link Artifact} recursively. {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} dependencies will be
 * resolved for building the {@link URL}'s for the class loader. Once classified the node is added to {@link Map} of
 * artifactsClassified.
 *
 * @param artifactToClassify {@link Artifact} that represents the artifact to be classified
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param artifactsClassified {@link Map} that contains already classified plugins
 * @param rootArtifactRemoteRepositories remote repositories defined at the root artifact.
 */
private void buildPluginUrlClassification(Artifact artifactToClassify, ClassPathClassifierContext context, Predicate<Dependency> directDependenciesFilter, Set<ArtifactClassificationNode> artifactsClassified, List<RemoteRepository> rootArtifactRemoteRepositories) {
    List<URL> urls;
    try {
        final DependencyFilter dependencyFilter = andFilter(classpathFilter(COMPILE), new PatternExclusionsDependencyFilter(context.getExcludedArtifacts()), orFilter(new PatternExclusionsDependencyFilter("*:*:*:" + MULE_PLUGIN_CLASSIFIER + ":*"), new PatternInclusionsDependencyFilter(toId(artifactToClassify))));
        urls = toUrl(dependencyResolver.resolveDependencies(new Dependency(artifactToClassify, COMPILE), emptyList(), emptyList(), dependencyFilter, rootArtifactRemoteRepositories));
    } catch (Exception e) {
        throw new IllegalStateException("Couldn't resolve dependencies for artifact: '" + artifactToClassify + "' classification", e);
    }
    List<Dependency> directDependencies;
    List<ArtifactClassificationNode> artifactDependencies = newArrayList();
    try {
        directDependencies = dependencyResolver.getDirectDependencies(artifactToClassify, rootArtifactRemoteRepositories);
    } catch (ArtifactDescriptorException e) {
        throw new IllegalStateException("Couldn't get direct dependencies for artifact: '" + artifactToClassify + "'", e);
    }
    logger.debug("Searching for dependencies on direct dependencies of artifact {}", artifactToClassify);
    List<Artifact> pluginArtifactDependencies = filterArtifacts(directDependencies, directDependenciesFilter);
    logger.debug("Artifacts {} identified a plugin dependencies for plugin {}", pluginArtifactDependencies, artifactToClassify);
    pluginArtifactDependencies.stream().map(artifact -> {
        Optional<ArtifactClassificationNode> artifactClassificationNodeOptional = findArtifactClassified(artifactsClassified, artifact);
        if (!artifactClassificationNodeOptional.isPresent()) {
            buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
        } else {
            if (!areCompatibleVersions(artifactClassificationNodeOptional.get().getArtifact().getVersion(), artifact.getVersion())) {
                throw new IllegalStateException(format("Incompatible version of artifacts found: %s and %s", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact)));
            }
            logger.debug("Checking for highest version of artifact, already discovered: '{}' versus: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
            if (isHighestVersion(artifact.getVersion(), artifactClassificationNodeOptional.get().getArtifact().getVersion())) {
                artifactsClassified.remove(artifactClassificationNodeOptional.get());
                buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
                logger.warn("Replacing artifact: '{}' for highest version: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
            }
        }
        return findArtifactClassified(artifactsClassified, artifact).orElseThrow(() -> new IllegalStateException(format("Should %s be already added to the list of artifacts classified", toId(artifact))));
    }).forEach(artifactDependencies::add);
    final List<Class> exportClasses = getArtifactExportedClasses(artifactToClassify, context, rootArtifactRemoteRepositories);
    resolveSnapshotVersionsToTimestampedFromClassPath(urls, context.getClassPathURLs());
    ArtifactClassificationNode artifactUrlClassification = new ArtifactClassificationNode(artifactToClassify, urls, exportClasses, artifactDependencies);
    logger.debug("Artifact discovered: {}", toId(artifactUrlClassification.getArtifact()));
    artifactsClassified.add(artifactUrlClassification);
}
Also used : PLUGIN(org.mule.test.runner.api.ArtifactClassificationType.PLUGIN) ListIterator(java.util.ListIterator) URL(java.net.URL) Optional.of(java.util.Optional.of) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) LoggerFactory(org.slf4j.LoggerFactory) DependencyFilterUtils.orFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.orFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) APPLICATION(org.mule.test.runner.api.ArtifactClassificationType.APPLICATION) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Collections.singleton(java.util.Collections.singleton) DependencyFilterUtils.andFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.andFilter) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactIdUtils.toId(org.eclipse.aether.util.artifact.ArtifactIdUtils.toId) PROVIDED(org.eclipse.aether.util.artifact.JavaScopes.PROVIDED) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) String.format(java.lang.String.format) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) Optional.empty(java.util.Optional.empty) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Extension(org.mule.runtime.extension.api.annotation.Extension) Dependency(org.eclipse.aether.graph.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) COMPILE(org.eclipse.aether.util.artifact.JavaScopes.COMPILE) MODULE(org.mule.test.runner.api.ArtifactClassificationType.MODULE) VersionChecker.areCompatibleVersions(org.mule.maven.client.internal.util.VersionChecker.areCompatibleVersions) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Sets.newLinkedHashSet(com.google.common.collect.Sets.newLinkedHashSet) IOException(java.io.IOException) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Collectors.toList(java.util.stream.Collectors.toList) FileFilter(java.io.FileFilter) StringUtils.endsWithIgnoreCase(org.apache.commons.lang3.StringUtils.endsWithIgnoreCase) VersionChecker.isHighestVersion(org.mule.maven.client.internal.util.VersionChecker.isHighestVersion) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyFilterUtils.classpathFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.classpathFilter) Collections(java.util.Collections) Optional(java.util.Optional) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) URL(java.net.URL) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException)

Example 5 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project zeppelin by apache.

the class DependencyResolver method getArtifactsWithDep.

/**
 * @param dependency
 * @param excludes list of pattern can either be of the form groupId:artifactId
 * @return
 * @throws Exception
 */
@Override
public List<ArtifactResult> getArtifactsWithDep(String dependency, Collection<String> excludes) throws RepositoryException {
    Artifact artifact = new DefaultArtifact(dependency);
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
    PatternExclusionsDependencyFilter exclusionFilter = new PatternExclusionsDependencyFilter(excludes);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
    synchronized (repos) {
        for (RemoteRepository repo : repos) {
            collectRequest.addRepository(repo);
        }
    }
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, DependencyFilterUtils.andFilter(exclusionFilter, classpathFilter));
    try {
        return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
    } catch (NullPointerException | DependencyResolutionException ex) {
        throw new RepositoryException(String.format("Cannot fetch dependencies for %s", dependency), ex);
    }
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) PatternExclusionsDependencyFilter(org.eclipse.aether.util.filter.PatternExclusionsDependencyFilter) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) RepositoryException(org.eclipse.aether.RepositoryException) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) PatternExclusionsDependencyFilter(org.eclipse.aether.util.filter.PatternExclusionsDependencyFilter)

Aggregations

DependencyFilter (org.eclipse.aether.graph.DependencyFilter)14 Dependency (org.eclipse.aether.graph.Dependency)10 Artifact (org.eclipse.aether.artifact.Artifact)9 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)9 CollectRequest (org.eclipse.aether.collection.CollectRequest)9 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)9 File (java.io.File)7 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)6 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)5 IOException (java.io.IOException)4 List (java.util.List)4 DependencyResult (org.eclipse.aether.resolution.DependencyResult)4 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 Sets.newLinkedHashSet (com.google.common.collect.Sets.newLinkedHashSet)2