Search in sources :

Example 1 with PatternInclusionsDependencyFilter

use of org.mule.test.runner.classification.PatternInclusionsDependencyFilter 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 2 with PatternInclusionsDependencyFilter

use of org.mule.test.runner.classification.PatternInclusionsDependencyFilter 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)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 FileUtils.toFile (org.apache.commons.io.FileUtils.toFile)2 Artifact (org.eclipse.aether.artifact.Artifact)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 Dependency (org.eclipse.aether.graph.Dependency)2 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)2 Exclusion (org.eclipse.aether.graph.Exclusion)2 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)2 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 Sets.newLinkedHashSet (com.google.common.collect.Sets.newLinkedHashSet)1 FileFilter (java.io.FileFilter)1 String.format (java.lang.String.format)1 Collection (java.util.Collection)1