Search in sources :

Example 26 with ArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-plugins by apache.

the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.

private Set<Artifact> getFilteredResolvedArtifacts(MavenProject project, Set<Artifact> artifacts, TransformableFilter filter) {
    try {
        Iterable<ArtifactResult> results = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), project.getModel(), filter);
        Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
        for (ArtifactResult artResult : results) {
            resolvedArtifacts.add(artResult.getArtifact());
        }
        return resolvedArtifacts;
    } catch (DependencyResolverException e) {
        getLog().info("Unable to resolve all dependencies for : " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + ". Falling back to non-transitive mode for initial artifact resolution.");
    }
    Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
    ArtifactFilter artifactFilter = filter.transform(new ArtifactIncludeFilterTransformer());
    // purge anyway
    for (Artifact artifact : artifacts) {
        if (artifactFilter.include(artifact)) {
            try {
                ArtifactResult artifactResult = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), artifact);
                resolvedArtifacts.add(artifactResult.getArtifact());
            } catch (ArtifactResolverException e) {
                getLog().debug("Unable to resolve artifact: " + artifact);
            }
        }
    }
    return resolvedArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult)

Example 27 with ArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project felix by apache.

the class AbstractDependencyFilter method filteredDependencies.

private void filteredDependencies(final ArtifactFilter artifactFilter, Collection<Artifact> filteredDependencies) {
    CollectingDependencyNodeVisitor collectingDependencyNodeVisitor = new CollectingDependencyNodeVisitor();
    final Artifact rootArtifact = m_dependencyGraph.getArtifact();
    ArtifactFilter filter = new ArtifactFilter() {

        public boolean include(Artifact artifact) {
            return artifact.equals(rootArtifact) || artifactFilter.include(artifact);
        }
    };
    DependencyNodeFilter dependencyNodeFilter = new ArtifactDependencyNodeFilter(filter);
    dependencyNodeFilter = new TrimmingDependencyNodeFilter(dependencyNodeFilter);
    DependencyNodeVisitor dependencyNodeVisitor = new FilteringDependencyNodeVisitor(collectingDependencyNodeVisitor, dependencyNodeFilter);
    dependencyNodeVisitor = new BuildingDependencyNodeVisitor(dependencyNodeVisitor);
    m_dependencyGraph.accept(dependencyNodeVisitor);
    List<DependencyNode> dependencyNodes = collectingDependencyNodeVisitor.getNodes();
    Set<String> ids = new LinkedHashSet<String>(dependencyNodes.size());
    for (DependencyNode dependencyNode : dependencyNodes) {
        Artifact artifact = dependencyNode.getArtifact();
        String id = artifact.getId();
        ids.add(id);
    }
    for (Iterator<Artifact> iterator = filteredDependencies.iterator(); iterator.hasNext(); ) {
        Artifact artifact = iterator.next();
        String id = artifact.getId();
        if (!ids.contains(id)) {
            iterator.remove();
        }
    }
}
Also used : FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) LinkedHashSet(java.util.LinkedHashSet) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter) Artifact(org.apache.maven.artifact.Artifact) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) DependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor) BuildingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) BuildingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode)

Example 28 with ArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project felix by apache.

the class AbstractDependencyFilter method processInstructions.

protected final void processInstructions(String header) throws MojoExecutionException {
    Map<String, Attrs> instructions = OSGiHeader.parseHeader(MISSING_KEY_PATTERN.matcher(header).replaceAll("$1$2*;$3"));
    Collection<Artifact> availableDependencies = new LinkedHashSet<Artifact>(m_dependencyArtifacts);
    for (Iterator<Map.Entry<String, Attrs>> clauseIterator = instructions.entrySet().iterator(); clauseIterator.hasNext(); ) {
        String inline = "false";
        // always start with a fresh *modifiable* collection for each unique clause
        Collection<Artifact> filteredDependencies = new LinkedHashSet<Artifact>(availableDependencies);
        // CLAUSE: REGEXP --> { ATTRIBUTE MAP }
        Map.Entry<String, Attrs> clause = clauseIterator.next();
        String primaryKey = clause.getKey().replaceFirst("~+$", "");
        boolean isNegative = primaryKey.startsWith("!");
        if (isNegative) {
            primaryKey = primaryKey.substring(1);
        }
        final AndArtifactFilter andArtifactFilter = new AndArtifactFilter();
        if (!"*".equals(primaryKey)) {
            ArtifactFilter filter = new DependencyFilter(primaryKey) {

                @Override
                public boolean include(Artifact dependency) {
                    return super.matches(dependency.getArtifactId());
                }
            };
            // FILTER ON MAIN CLAUSE
            andArtifactFilter.add(filter);
        }
        for (Iterator<Map.Entry<String, String>> attrIterator = clause.getValue().entrySet().iterator(); attrIterator.hasNext(); ) {
            final ArtifactFilter filter;
            // ATTRIBUTE: KEY --> REGEXP
            Map.Entry<String, String> attr = attrIterator.next();
            if ("groupId".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue()) {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches(dependency.getGroupId());
                    }
                };
            } else if ("artifactId".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue()) {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches(dependency.getArtifactId());
                    }
                };
            } else if ("version".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue()) {

                    @Override
                    public boolean include(Artifact dependency) {
                        try {
                            // use the symbolic version if available (ie. 1.0.0-SNAPSHOT)
                            return super.matches(dependency.getSelectedVersion().toString());
                        } catch (Exception e) {
                            return super.matches(dependency.getVersion());
                        }
                    }
                };
            } else if ("scope".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue(), "compile") {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches(dependency.getScope());
                    }
                };
            } else if ("type".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue(), "jar") {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches(dependency.getType());
                    }
                };
            } else if ("classifier".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue()) {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches(dependency.getClassifier());
                    }
                };
            } else if ("optional".equals(attr.getKey())) {
                filter = new DependencyFilter(attr.getValue(), "false") {

                    @Override
                    public boolean include(Artifact dependency) {
                        return super.matches("" + dependency.isOptional());
                    }
                };
            } else if ("inline".equals(attr.getKey())) {
                inline = attr.getValue();
                continue;
            } else {
                throw new MojoExecutionException("Unexpected attribute " + attr.getKey());
            }
            // FILTER ON EACH ATTRIBUTE
            andArtifactFilter.add(filter);
        }
        filteredDependencies(andArtifactFilter, filteredDependencies);
        if (isNegative) {
            // negative clauses reduce the set of available artifacts
            availableDependencies.removeAll(filteredDependencies);
            if (!clauseIterator.hasNext()) {
                // assume there's an implicit * missing at the end
                processDependencies(availableDependencies, inline);
            }
        } else {
            // positive clause; doesn't alter the available artifacts
            processDependencies(filteredDependencies, inline);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Attrs(aQute.bnd.header.Attrs) Artifact(org.apache.maven.artifact.Artifact) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) Map(java.util.Map) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter)

Example 29 with ArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-dependency-plugin by apache.

the class TreeMojo method createResolvingArtifactFilter.

// private methods --------------------------------------------------------
/**
 * Gets the artifact filter to use when resolving the dependency tree.
 *
 * @return the artifact filter
 */
private ArtifactFilter createResolvingArtifactFilter() {
    ArtifactFilter filter;
    // filter scope
    if (scope != null) {
        getLog().debug("+ Resolving dependency tree for scope '" + scope + "'");
        filter = new ScopeArtifactFilter(scope);
    } else {
        filter = null;
    }
    return filter;
}
Also used : StrictPatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) StrictPatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)

Example 30 with ArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-dependency-plugin by apache.

the class PurgeLocalRepositoryMojo method purgeLocalRepository.

/**
 * Purges the local repository for the dependencies in the given Maven project.
 *
 * @param theProject Maven project.
 * @param resolvedArtifactsToPurge The artifacts that were already purged.
 * @throws MojoFailureException in case of errors during the purge.
 */
private void purgeLocalRepository(MavenProject theProject, Set<Artifact> purgedArtifacts) throws MojoFailureException {
    List<Dependency> dependencies = theProject.getDependencies();
    TransformableFilter dependencyFilter = createPurgeArtifactsFilter(theProject, dependencies, purgedArtifacts);
    Set<Artifact> resolvedArtifactsToPurge = getFilteredResolvedArtifacts(theProject, dependencies, dependencyFilter);
    if (resolvedArtifactsToPurge.isEmpty()) {
        getLog().info("No artifacts included for purge for project: " + theProject.getId());
        return;
    }
    verbose("Purging dependencies for project: " + theProject.getId());
    purgeArtifacts(resolvedArtifactsToPurge);
    purgedArtifacts.addAll(resolvedArtifactsToPurge);
    if (reResolve) {
        ArtifactFilter artifactFilter = dependencyFilter.transform(new ArtifactIncludeFilterTransformer());
        try {
            reResolveArtifacts(theProject, resolvedArtifactsToPurge, artifactFilter);
        } catch (ArtifactResolutionException e) {
            String failureMessage = "Failed to refresh project dependencies for: " + theProject.getId();
            MojoFailureException failure = new MojoFailureException(failureMessage);
            failure.initCause(e);
            throw failure;
        } catch (ArtifactNotFoundException e) {
            String failureMessage = "Failed to refresh project dependencies for: " + theProject.getId();
            MojoFailureException failure = new MojoFailureException(failureMessage);
            failure.initCause(e);
            throw failure;
        }
    }
}
Also used : ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) TransformableFilter(org.apache.maven.shared.artifact.filter.resolve.TransformableFilter) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Dependency(org.apache.maven.model.Dependency) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)44 Artifact (org.apache.maven.artifact.Artifact)32 ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)20 ArrayList (java.util.ArrayList)18 AndArtifactFilter (org.apache.maven.artifact.resolver.filter.AndArtifactFilter)12 LinkedHashSet (java.util.LinkedHashSet)10 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)10 StrictPatternExcludesArtifactFilter (org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter)8 Dependency (org.apache.maven.model.Dependency)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 StrictPatternIncludesArtifactFilter (org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter)7 File (java.io.File)6 IOException (java.io.IOException)6 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)6 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)5 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)4 ExcludesArtifactFilter (org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter)4 PatternExcludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter)4 PatternIncludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter)4 ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)4