Search in sources :

Example 11 with ArtifactFilter

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

the class AddDependencySetsTask method resolveDependencyArtifacts.

Set<Artifact> resolveDependencyArtifacts(final DependencySet dependencySet) throws InvalidAssemblerConfigurationException {
    final Set<Artifact> dependencyArtifacts = new LinkedHashSet<Artifact>();
    if (resolvedArtifacts != null) {
        dependencyArtifacts.addAll(resolvedArtifacts);
    }
    if (dependencySet.isUseProjectArtifact()) {
        final Artifact projectArtifact = project.getArtifact();
        if ((projectArtifact != null) && (projectArtifact.getFile() != null)) {
            dependencyArtifacts.add(projectArtifact);
        } else {
            logger.warn("Cannot include project artifact: " + projectArtifact + "; it doesn't have an associated file or directory.");
        }
    }
    if (dependencySet.isUseProjectAttachments()) {
        final List<Artifact> attachments = project.getAttachedArtifacts();
        if (attachments != null) {
            for (final Artifact attachment : attachments) {
                if (attachment.getFile() != null) {
                    dependencyArtifacts.add(attachment);
                } else {
                    logger.warn("Cannot include attached artifact: " + project.getId() + " for project: " + project.getId() + "; it doesn't have an associated file or directory.");
                }
            }
        }
    }
    if (dependencySet.isUseTransitiveFiltering()) {
        logger.debug("Filtering dependency artifacts USING transitive dependency path information.");
    } else {
        logger.debug("Filtering dependency artifacts WITHOUT transitive dependency path information.");
    }
    final ScopeFilter scopeFilter = FilterUtils.newScopeFilter(dependencySet.getScope());
    final ArtifactFilter filter = new ArtifactIncludeFilterTransformer().transform(scopeFilter);
    FilterUtils.filterArtifacts(dependencyArtifacts, dependencySet.getIncludes(), dependencySet.getExcludes(), dependencySet.isUseStrictFiltering(), dependencySet.isUseTransitiveFiltering(), logger, filter);
    return dependencyArtifacts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ScopeFilter(org.apache.maven.shared.artifact.filter.resolve.ScopeFilter) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) Artifact(org.apache.maven.artifact.Artifact)

Example 12 with ArtifactFilter

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

the class RunMojo method getAllDependencies.

// generic method to retrieve all the transitive dependencies
private Collection<Artifact> getAllDependencies() throws MojoExecutionException {
    List<Artifact> artifacts = new ArrayList<Artifact>();
    for (Iterator<?> dependencies = project.getDependencies().iterator(); dependencies.hasNext(); ) {
        Dependency dependency = (Dependency) dependencies.next();
        String groupId = dependency.getGroupId();
        String artifactId = dependency.getArtifactId();
        VersionRange versionRange;
        try {
            versionRange = VersionRange.createFromVersionSpec(dependency.getVersion());
        } catch (InvalidVersionSpecificationException e) {
            throw new MojoExecutionException("unable to parse version", e);
        }
        String type = dependency.getType();
        if (type == null) {
            type = "jar";
        }
        String classifier = dependency.getClassifier();
        boolean optional = dependency.isOptional();
        String scope = dependency.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }
        Artifact art = this.artifactFactory.createDependencyArtifact(groupId, artifactId, versionRange, type, classifier, scope, null, optional);
        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            art.setFile(new File(dependency.getSystemPath()));
        }
        List<String> exclusions = new ArrayList<String>();
        for (Exclusion exclusion : dependency.getExclusions()) {
            exclusions.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
        }
        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
        art.setDependencyFilter(newFilter);
        artifacts.add(art);
    }
    return artifacts;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Dependency(org.apache.maven.model.Dependency) ExecutableDependency(org.codehaus.mojo.exec.ExecutableDependency) Artifact(org.apache.maven.artifact.Artifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) Exclusion(org.apache.maven.model.Exclusion) File(java.io.File)

Example 13 with ArtifactFilter

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

the class PurgeLocalRepositoryMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (isSkip()) {
        getLog().info("Skipping plugin execution");
        return;
    }
    if (!StringUtils.isEmpty(manualInclude)) {
        manualIncludes = this.parseIncludes(manualInclude);
    }
    // If it's a manual purge, the only step is to delete from the local repo
    if (manualIncludes != null && manualIncludes.size() > 0) {
        manualPurge(manualIncludes);
        return;
    }
    Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    TransformableFilter dependencyFilter = createPurgeArtifactsFilter(dependencyArtifacts);
    Set<Artifact> resolvedArtifactsToPurge = getFilteredResolvedArtifacts(project, dependencyArtifacts, dependencyFilter);
    if (resolvedArtifactsToPurge.isEmpty()) {
        getLog().info("No artifacts included for purge for project: " + project.getId());
        return;
    }
    verbose("Purging dependencies for project: " + project.getId());
    purgeArtifacts(resolvedArtifactsToPurge);
    if (reResolve) {
        ArtifactFilter artifactFilter = dependencyFilter.transform(new ArtifactIncludeFilterTransformer());
        try {
            reResolveArtifacts(project, resolvedArtifactsToPurge, artifactFilter);
        } catch (ArtifactResolutionException e) {
            String failureMessage = "Failed to refresh project dependencies for: " + project.getId();
            MojoFailureException failure = new MojoFailureException(failureMessage);
            failure.initCause(e);
            throw failure;
        } catch (ArtifactNotFoundException e) {
            String failureMessage = "Failed to refresh project dependencies for: " + project.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) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Example 14 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 15 with ArtifactFilter

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

the class TreeMojo method execute.

// Mojo methods -----------------------------------------------------------
/*
     * @see org.apache.maven.plugin.Mojo#execute()
     */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (isSkip()) {
        getLog().info("Skipping plugin execution");
        return;
    }
    try {
        String dependencyTreeString;
        // TODO: note that filter does not get applied due to MSHARED-4
        ArtifactFilter artifactFilter = createResolvingArtifactFilter();
        if (verbose) {
            // To fix we probably need a different DependencyCollector in Aether, which doesn't remove nodes which
            // have already been resolved.
            getLog().info("Verbose not supported since maven-dependency-plugin 3.0");
        }
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        buildingRequest.setProject(project);
        // non-verbose mode use dependency graph component, which gives consistent results with Maven version
        // running
        rootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, artifactFilter, reactorProjects);
        dependencyTreeString = serializeDependencyTree(rootNode);
        if (outputFile != null) {
            DependencyUtil.write(dependencyTreeString, outputFile, this.appendOutput, getLog());
            getLog().info("Wrote dependency tree to: " + outputFile);
        } else {
            DependencyUtil.log(dependencyTreeString, getLog());
        }
    } catch (DependencyGraphBuilderException exception) {
        throw new MojoExecutionException("Cannot build project dependency graph", exception);
    } catch (IOException exception) {
        throw new MojoExecutionException("Cannot serialise project dependency graph", exception);
    }
}
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) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) IOException(java.io.IOException) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Aggregations

ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)22 Artifact (org.apache.maven.artifact.Artifact)16 ArrayList (java.util.ArrayList)11 ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)6 LinkedHashSet (java.util.LinkedHashSet)5 PatternIncludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter)5 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)5 File (java.io.File)4 AndArtifactFilter (org.apache.maven.artifact.resolver.filter.AndArtifactFilter)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 PatternExcludesArtifactFilter (org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter)4 StrictPatternExcludesArtifactFilter (org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter)4 StrictPatternIncludesArtifactFilter (org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter)4 IOException (java.io.IOException)3 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)3 ArtifactMetadataSource (org.apache.maven.artifact.metadata.ArtifactMetadataSource)3 ResolutionGroup (org.apache.maven.artifact.metadata.ResolutionGroup)3 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)3 ExcludesArtifactFilter (org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter)3 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)3