Search in sources :

Example 6 with AndArtifactFilter

use of org.apache.maven.artifact.resolver.filter.AndArtifactFilter 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 7 with AndArtifactFilter

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

the class DefaultRepositoryAssembler method buildRepositoryFilter.

private ArtifactFilter buildRepositoryFilter(RepositoryInfo repository, MavenProject project) {
    AndArtifactFilter filter = new AndArtifactFilter();
    ArtifactFilter scopeFilter = new ScopeArtifactFilter(repository.getScope());
    filter.add(scopeFilter);
    // ----------------------------------------------------------------------------
    // Includes
    // 
    // We'll take everything if no includes are specified to try and make
    // this
    // process more maintainable. Don't want to have to update the assembly
    // descriptor everytime the POM is updated.
    // ----------------------------------------------------------------------------
    List<String> includes = repository.getIncludes();
    if ((includes == null) || includes.isEmpty()) {
        List<String> patterns = new ArrayList<String>();
        Set<Artifact> projectArtifacts = project.getDependencyArtifacts();
        if (projectArtifacts != null) {
            for (Artifact artifact : projectArtifacts) {
                patterns.add(artifact.getDependencyConflictId());
            }
        }
        PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(patterns, true);
        filter.add(includeFilter);
    } else {
        filter.add(new PatternIncludesArtifactFilter(repository.getIncludes(), true));
    }
    // ----------------------------------------------------------------------------
    // Excludes
    // 
    // We still want to make it easy to exclude a few things even if we
    // slurp
    // up everything.
    // ----------------------------------------------------------------------------
    List<String> excludes = repository.getExcludes();
    if ((excludes != null) && !excludes.isEmpty()) {
        filter.add(new PatternExcludesArtifactFilter(repository.getExcludes(), true));
    }
    return filter;
}
Also used : ScopeArtifactFilter(org.apache.maven.shared.artifact.filter.ScopeArtifactFilter) PatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.shared.artifact.filter.ScopeArtifactFilter) PatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) Artifact(org.apache.maven.artifact.Artifact)

Example 8 with AndArtifactFilter

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

the class FilterUtils method filterArtifacts.

public static void filterArtifacts(final Set<Artifact> artifacts, final List<String> includes, final List<String> excludes, final boolean strictFiltering, final boolean actTransitively, final Logger logger, final ArtifactFilter... additionalFilters) throws InvalidAssemblerConfigurationException {
    final List<ArtifactFilter> allFilters = new ArrayList<ArtifactFilter>();
    final AndArtifactFilter filter = new AndArtifactFilter();
    if (additionalFilters != null && additionalFilters.length > 0) {
        for (final ArtifactFilter additionalFilter : additionalFilters) {
            if (additionalFilter != null) {
                filter.add(additionalFilter);
            }
        }
    }
    if (!includes.isEmpty()) {
        final ArtifactFilter includeFilter = new PatternIncludesArtifactFilter(includes, actTransitively);
        filter.add(includeFilter);
        allFilters.add(includeFilter);
    }
    if (!excludes.isEmpty()) {
        final ArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excludes, actTransitively);
        filter.add(excludeFilter);
        allFilters.add(excludeFilter);
    }
    for (final Iterator<Artifact> it = artifacts.iterator(); it.hasNext(); ) {
        final Artifact artifact = it.next();
        if (!filter.include(artifact)) {
            it.remove();
            if (logger.isDebugEnabled()) {
                logger.debug(artifact.getId() + " was removed by one or more filters.");
            }
        }
    }
    reportFilteringStatistics(allFilters, logger);
    for (final ArtifactFilter f : allFilters) {
        if (f instanceof StatisticsReportingArtifactFilter) {
            final StatisticsReportingArtifactFilter sFilter = (StatisticsReportingArtifactFilter) f;
            if (strictFiltering && sFilter.hasMissedCriteria()) {
                throw new InvalidAssemblerConfigurationException("One or more filters had unmatched criteria. Check debug log for more information.");
            }
        }
    }
}
Also used : PatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) StatisticsReportingArtifactFilter(org.apache.maven.shared.artifact.filter.StatisticsReportingArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) PatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) ArrayList(java.util.ArrayList) StatisticsReportingArtifactFilter(org.apache.maven.shared.artifact.filter.StatisticsReportingArtifactFilter) InvalidAssemblerConfigurationException(org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) Artifact(org.apache.maven.artifact.Artifact)

Example 9 with AndArtifactFilter

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

the class FilterUtils method filterProjects.

public static Set<MavenProject> filterProjects(final Set<MavenProject> projects, final List<String> includes, final List<String> excludes, final boolean actTransitively, final Logger logger) {
    final List<PatternIncludesArtifactFilter> allFilters = new ArrayList<PatternIncludesArtifactFilter>();
    final AndArtifactFilter filter = new AndArtifactFilter();
    if (!includes.isEmpty()) {
        final PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(includes, actTransitively);
        filter.add(includeFilter);
        allFilters.add(includeFilter);
    }
    if (!excludes.isEmpty()) {
        final PatternExcludesArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excludes, actTransitively);
        filter.add(excludeFilter);
        allFilters.add(excludeFilter);
    }
    Set<MavenProject> result = new LinkedHashSet<MavenProject>(projects.size());
    for (MavenProject project : projects) {
        final Artifact artifact = project.getArtifact();
        if (filter.include(artifact)) {
            result.add(project);
        }
    }
    for (final PatternIncludesArtifactFilter f : allFilters) {
        if (f != null) {
            f.reportMissedCriteria(logger);
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MavenProject(org.apache.maven.project.MavenProject) PatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

AndArtifactFilter (org.apache.maven.artifact.resolver.filter.AndArtifactFilter)9 Artifact (org.apache.maven.artifact.Artifact)8 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)6 ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)4 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 PatternExcludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter)3 PatternIncludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Attrs (aQute.bnd.header.Attrs)1 Map (java.util.Map)1 SpecificScopesArtifactFilter (org.apache.maven.ant.tasks.support.SpecificScopesArtifactFilter)1 TypesArtifactFilter (org.apache.maven.ant.tasks.support.TypesArtifactFilter)1 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)1 ArtifactResolutionRequest (org.apache.maven.artifact.resolver.ArtifactResolutionRequest)1 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)1 ExcludesArtifactFilter (org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter)1 TypeArtifactFilter (org.apache.maven.artifact.resolver.filter.TypeArtifactFilter)1 Log (org.apache.maven.plugin.logging.Log)1 InvalidAssemblerConfigurationException (org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException)1