Search in sources :

Example 11 with DependencyFilter

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

the class AetherClassPathClassifier method buildTestRunnerUrlClassification.

/**
 * Application classification is being done by resolving the direct dependencies with scope
 * {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} for the rootArtifact. Due to Eclipse Aether resolution excludes by
 * {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} dependencies an imaginary pom will be created with these
 * dependencies as {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} so the dependency graph can be resolved (with
 * the same results as it will be obtained from Maven).
 * <p/>
 * If the rootArtifact was classified as plugin its {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} will be changed
 * to {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} in order to exclude them from the dependency graph.
 * <p/>
 * Filtering logic includes the following pattern to includes the patterns defined at
 * {@link ClassPathClassifierContext#getTestInclusions()}. It also excludes
 * {@link ClassPathClassifierContext#getExcludedArtifacts()}, {@link ClassPathClassifierContext#getTestExclusions()}.
 * <p/>
 * If the application artifact has not been classified as plugin its going to be resolved as {@value #JAR_EXTENSION} in order to
 * include this its compiled classes classification.
 *
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param directDependencies {@link List} of {@link Dependency} with direct dependencies for the rootArtifact
 * @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact @return {@link URL}s for application class loader
 * @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
 */
private List<URL> buildTestRunnerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
    logger.debug("Building application classification");
    Artifact rootArtifact = context.getRootArtifact();
    DependencyFilter dependencyFilter = new PatternInclusionsDependencyFilter(context.getTestInclusions());
    logger.debug("Using filter for dependency graph to include: '{}'", context.getTestInclusions());
    List<File> appFiles = newArrayList();
    List<String> exclusionsPatterns = newArrayList();
    if (APPLICATION.equals(rootArtifactType)) {
        logger.debug("RootArtifact identified as {} so is going to be added to application classification", APPLICATION);
        File rootArtifactOutputFile = resolveRootArtifactFile(rootArtifact);
        if (rootArtifactOutputFile != null) {
            appFiles.add(rootArtifactOutputFile);
        } else {
            logger.warn("rootArtifact '{}' identified as {} but doesn't have an output {} file", rootArtifact, rootArtifactType, JAR_EXTENSION);
        }
    } else {
        logger.debug("RootArtifact already classified as plugin or module, excluding it from application classification");
        exclusionsPatterns.add(rootArtifact.getGroupId() + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getArtifactId() + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getVersion());
    }
    directDependencies = directDependencies.stream().map(toTransform -> {
        if (toTransform.getScope().equals(TEST)) {
            if (TESTS_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) {
                // Exclude transitive dependencies of test-jar artifacts
                return toTransform.setScope(COMPILE).setExclusions(singleton(new Exclusion("*", "*", "*", "*")));
            } else {
                return toTransform.setScope(COMPILE);
            }
        }
        if ((PLUGIN.equals(rootArtifactType) || MULE_PLUGIN_CLASSIFIER.equals(toTransform.getArtifact().getClassifier()) || MULE_SERVICE_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) && toTransform.getScope().equals(COMPILE)) {
            return toTransform.setScope(PROVIDED);
        }
        if (rootArtifactType == MODULE && toTransform.getScope().equals(COMPILE)) {
            return toTransform.setScope(PROVIDED);
        }
        Artifact artifact = toTransform.getArtifact();
        if (context.getApplicationSharedLibCoordinates().contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) {
            return toTransform.setScope(COMPILE);
        }
        return toTransform;
    }).collect(toList());
    logger.debug("OR exclude: {}", context.getExcludedArtifacts());
    exclusionsPatterns.addAll(context.getExcludedArtifacts());
    if (!context.getTestExclusions().isEmpty()) {
        logger.debug("OR exclude application specific artifacts: {}", context.getTestExclusions());
        exclusionsPatterns.addAll(context.getTestExclusions());
    }
    try {
        List<Dependency> managedDependencies = newArrayList(dependencyResolver.readArtifactDescriptor(rootArtifact).getManagedDependencies());
        managedDependencies.addAll(directDependencies.stream().filter(directDependency -> !directDependency.getScope().equals(TEST)).collect(toList()));
        logger.debug("Resolving dependency graph for '{}' scope direct dependencies: {} and managed dependencies {}", TEST, directDependencies, managedDependencies);
        final Dependency rootTestDependency = new Dependency(new DefaultArtifact(rootArtifact.getGroupId(), rootArtifact.getArtifactId(), TESTS_CLASSIFIER, JAR_EXTENSION, rootArtifact.getVersion()), TEST);
        List<File> urls = dependencyResolver.resolveDependencies(rootTestDependency, directDependencies, managedDependencies, orFilter(dependencyFilter, new PatternExclusionsDependencyFilter(exclusionsPatterns)), rootArtifactRemoteRepositories);
        appFiles.addAll(urls);
    } catch (Exception e) {
        throw new IllegalStateException("Couldn't resolve dependencies for application '" + context.getRootArtifact() + "' classification", e);
    }
    List<URL> testRunnerLibUrls = newArrayList(toUrl(appFiles));
    logger.debug("Appending URLs to test runner plugin: {}", context.getTestRunnerPluginUrls());
    testRunnerLibUrls.addAll(context.getTestRunnerPluginUrls());
    resolveSnapshotVersionsToTimestampedFromClassPath(testRunnerLibUrls, context.getClassPathURLs());
    return testRunnerLibUrls;
}
Also used : 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) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) Exclusion(org.eclipse.aether.graph.Exclusion) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 12 with DependencyFilter

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

the class AetherClassPathClassifier method buildContainerUrlClassification.

/**
 * Container classification is being done by resolving the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct
 * dependencies of the rootArtifact. Is uses the exclusions defined in
 * {@link ClassPathClassifierContext#getProvidedExclusions()} to filter the dependency graph plus
 * {@link ClassPathClassifierContext#getExcludedArtifacts()}.
 * <p/>
 * In order to resolve correctly the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct dependencies it will
 * get for each one the manage dependencies and use that list to resolve the graph.
 *
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param pluginUrlClassifications {@link PluginUrlClassification}s to check if rootArtifact was classified as plugin
 * @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact
 * @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
 * @return {@link List} of {@link URL}s for the container class loader
 */
private List<URL> buildContainerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, List<ArtifactUrlClassification> serviceUrlClassifications, List<PluginUrlClassification> pluginUrlClassifications, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
    directDependencies = directDependencies.stream().filter(getContainerDirectDependenciesFilter(rootArtifactType)).filter(dependency -> {
        Artifact artifact = dependency.getArtifact();
        return (!serviceUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent() && !pluginUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent());
    }).map(depToTransform -> depToTransform.setScope(COMPILE)).collect(toList());
    // Add logging dependencies to avoid every module from having to declare this dependency.
    // This brings the slf4j bridges required by transitive dependencies of the container to its classpath
    // TODO MULE-10837 Externalize this dependency along with the other commonly used container dependencies.
    directDependencies.add(new Dependency(new DefaultArtifact(RUNTIME_GROUP_ID, LOGGING_ARTIFACT_ID, JAR_EXTENSION, muleVersion), COMPILE));
    logger.debug("Selected direct dependencies to be used for resolving container dependency graph (changed to compile in " + "order to resolve the graph): {}", directDependencies);
    Set<Dependency> managedDependencies = selectContainerManagedDependencies(context, directDependencies, rootArtifactType, rootArtifactRemoteRepositories);
    logger.debug("Collected managed dependencies from direct provided dependencies to be used for resolving container " + "dependency graph: {}", managedDependencies);
    List<String> excludedFilterPattern = newArrayList(context.getProvidedExclusions());
    excludedFilterPattern.addAll(context.getExcludedArtifacts());
    if (!pluginUrlClassifications.isEmpty()) {
        excludedFilterPattern.addAll(pluginUrlClassifications.stream().map(pluginUrlClassification -> pluginUrlClassification.getArtifactId()).collect(toList()));
    }
    if (!serviceUrlClassifications.isEmpty()) {
        excludedFilterPattern.addAll(serviceUrlClassifications.stream().map(serviceUrlClassification -> serviceUrlClassification.getArtifactId()).collect(toList()));
    }
    logger.debug("Resolving dependencies for container using exclusion filter patterns: {}", excludedFilterPattern);
    final DependencyFilter dependencyFilter = new PatternExclusionsDependencyFilter(excludedFilterPattern);
    List<URL> containerUrls;
    try {
        containerUrls = toUrl(dependencyResolver.resolveDependencies(null, directDependencies, newArrayList(managedDependencies), dependencyFilter, rootArtifactRemoteRepositories));
    } catch (Exception e) {
        throw new IllegalStateException("Couldn't resolve dependencies for Container", e);
    }
    containerUrls = containerUrls.stream().filter(url -> {
        String file = toFile(url).getAbsolutePath();
        return !(endsWithIgnoreCase(file, POM_XML) || endsWithIgnoreCase(file, POM_EXTENSION) || endsWithIgnoreCase(file, ZIP_EXTENSION));
    }).collect(toList());
    if (MODULE.equals(rootArtifactType)) {
        File rootArtifactOutputFile = resolveRootArtifactFile(context.getRootArtifact());
        if (rootArtifactOutputFile == null) {
            throw new IllegalStateException("rootArtifact (" + context.getRootArtifact() + ") identified as MODULE but doesn't have an output");
        }
        containerUrls.add(0, toUrl(rootArtifactOutputFile));
    }
    resolveSnapshotVersionsToTimestampedFromClassPath(containerUrls, context.getClassPathURLs());
    return containerUrls;
}
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) 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) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URL(java.net.URL) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 13 with DependencyFilter

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

the class DependencyResolver method resolve.

/**
 * Resolve dependencies and return downloaded information of artifacts.
 *
 * @param dependencies the list of dependency
 * @return downloaded information of artifacts
 * @throws DependencyResolutionException If the dependency tree could not be built or any dependency
 *     artifact could not be resolved.
 * @throws ArtifactResolutionException If the artifact could not be resolved.
 */
public List<ArtifactResult> resolve(List<Dependency> dependencies) throws DependencyResolutionException, ArtifactResolutionException {
    if (dependencies.size() == 0) {
        return Collections.EMPTY_LIST;
    }
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(dependencies.get(0));
    for (int idx = 1; idx < dependencies.size(); idx++) {
        collectRequest.addDependency(dependencies.get(idx));
    }
    for (RemoteRepository repository : remoteRepositories) {
        collectRequest.addRepository(repository);
    }
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
    return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) CollectRequest(org.eclipse.aether.collection.CollectRequest)

Example 14 with DependencyFilter

use of org.eclipse.aether.graph.DependencyFilter in project pinpoint by naver.

the class DependencyResolver method resolveArtifactsAndDependencies.

public List<File> resolveArtifactsAndDependencies(List<Artifact> artifacts) throws DependencyResolutionException {
    List<Dependency> dependencies = new ArrayList<>();
    for (Artifact artifact : artifacts) {
        dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME));
    }
    CollectRequest collectRequest = new CollectRequest((Dependency) null, dependencies, repositories);
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
    DependencyResult result = system.resolveDependencies(session, dependencyRequest);
    List<File> files = new ArrayList<>();
    for (ArtifactResult artifactResult : result.getArtifactResults()) {
        files.add(artifactResult.getArtifact().getFile());
    }
    return files;
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ArrayList(java.util.ArrayList) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

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