Search in sources :

Example 6 with TransformableFilter

use of org.apache.maven.shared.artifact.filter.resolve.TransformableFilter in project maven-plugins by apache.

the class PurgeLocalRepositoryMojo method createPurgeArtifactsFilter.

/**
 * Create the includes exclude filter to use when resolving and purging dependencies Also excludes any "system"
 * scope dependencies
 *
 * @param project The Maven project.
 * @param dependencies The dependencies to use as a reference if we're excluding transitive dependencies
 * @param purgedArtifacts The artifacts already purged.
 * @return the created filter
 */
private TransformableFilter createPurgeArtifactsFilter(MavenProject project, List<Dependency> dependencies, Set<Artifact> purgedArtifacts) {
    List<TransformableFilter> subFilters = new ArrayList<TransformableFilter>();
    // System dependencies should never be purged
    subFilters.add(ScopeFilter.excluding(Artifact.SCOPE_SYSTEM));
    if (this.snapshotsOnly) {
        subFilters.add(new SnapshotsFilter());
    }
    // The CLI includes/excludes overrides configuration in the pom
    if (!StringUtils.isEmpty(this.include)) {
        this.includes = parseIncludes(this.include);
    }
    if (this.includes != null) {
        subFilters.add(new PatternInclusionsFilter(includes));
    }
    if (!StringUtils.isEmpty(this.exclude)) {
        this.excludes = parseIncludes(this.exclude);
    }
    if (this.excludes != null) {
        subFilters.add(new PatternExclusionsFilter(excludes));
    }
    if (!actTransitively) {
        subFilters.add(new DirectDependencyFilter(project.getArtifact(), dependencies));
    }
    List<String> exclusions = new ArrayList<String>(reactorProjects.size());
    // It doesn't make sense to include projects from the reactor here since they're likely not able to be resolved
    for (MavenProject reactorProject : reactorProjects) {
        exclusions.add(toPatternExcludes(reactorProject.getArtifact()));
    }
    // There is no need to consider a second time artifacts that were already purged (re-resolved or not)
    for (Artifact purgedArtifact : purgedArtifacts) {
        exclusions.add(toPatternExcludes(purgedArtifact));
    }
    subFilters.add(new PatternExclusionsFilter(exclusions));
    return new AndFilter(subFilters);
}
Also used : AndFilter(org.apache.maven.shared.artifact.filter.resolve.AndFilter) TransformableFilter(org.apache.maven.shared.artifact.filter.resolve.TransformableFilter) PatternInclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternInclusionsFilter) MavenProject(org.apache.maven.project.MavenProject) ArrayList(java.util.ArrayList) PatternExclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter) Artifact(org.apache.maven.artifact.Artifact)

Example 7 with TransformableFilter

use of org.apache.maven.shared.artifact.filter.resolve.TransformableFilter in project maven-plugins by apache.

the class PurgeLocalRepositoryMojo method purgeLocalRepository.

/**
 * Purges the local repository for the dependencies in the given Maven project.
 *
 * @param project Maven project.
 * @param resolvedArtifactsToPurge The artifacts that were already purged.
 * @throws MojoFailureException in case of errors during the purge.
 */
private void purgeLocalRepository(MavenProject project, Set<Artifact> purgedArtifacts) throws MojoFailureException {
    List<Dependency> dependencies = project.getDependencies();
    TransformableFilter dependencyFilter = createPurgeArtifactsFilter(project, dependencies, purgedArtifacts);
    Set<Artifact> resolvedArtifactsToPurge = getFilteredResolvedArtifacts(project, dependencies, 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);
    purgedArtifacts.addAll(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) Dependency(org.apache.maven.model.Dependency) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Example 8 with TransformableFilter

use of org.apache.maven.shared.artifact.filter.resolve.TransformableFilter in project maven-plugins by apache.

the class AbstractJavadocMojo method getPathElements.

/**
 * Method that gets the classpath and modulepath elements that will be specified in the javadoc
 * <code>-classpath</code> and <code>--module-path</code> parameter.
 * Since we have all the sources of the current reactor, it is sufficient to consider the
 * dependencies of the reactor modules, excluding the module artifacts which may not yet be available
 * when the reactor project is built for the first time.
 *
 * @return all classpath elements
 * @throws MavenReportException if any.
 */
private List<File> getPathElements() throws MavenReportException {
    List<File> classpathElements = new ArrayList<>();
    Map<String, Artifact> compileArtifactMap = new HashMap<>();
    if (isTest()) {
        classpathElements.addAll(getProjectBuildOutputDirs(project));
    }
    populateCompileArtifactMap(compileArtifactMap, project.getArtifacts());
    if (isAggregator() && project.isExecutionRoot()) {
        List<String> reactorArtifacts = new ArrayList<>();
        for (MavenProject p : reactorProjects) {
            reactorArtifacts.add(p.getGroupId() + ':' + p.getArtifactId());
        }
        TransformableFilter dependencyFilter = new AndFilter(Arrays.asList(new PatternExclusionsFilter(reactorArtifacts), getDependencyScopeFilter()));
        for (MavenProject subProject : reactorProjects) {
            if (subProject != project) {
                classpathElements.addAll(getProjectBuildOutputDirs(subProject));
                try {
                    StringBuilder sb = new StringBuilder();
                    sb.append("Compiled artifacts for ");
                    sb.append(subProject.getGroupId()).append(":");
                    sb.append(subProject.getArtifactId()).append(":");
                    sb.append(subProject.getVersion()).append('\n');
                    ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest();
                    buildingRequest = buildingRequest.setRemoteRepositories(subProject.getRemoteArtifactRepositories());
                    for (ArtifactResult artifactResult : dependencyResolver.resolveDependencies(buildingRequest, subProject.getDependencies(), null, dependencyFilter)) {
                        populateCompileArtifactMap(compileArtifactMap, Collections.singletonList(artifactResult.getArtifact()));
                        sb.append(artifactResult.getArtifact().getFile()).append('\n');
                    }
                    if (getLog().isDebugEnabled()) {
                        getLog().debug(sb.toString());
                    }
                } catch (DependencyResolverException e) {
                    throw new MavenReportException(e.getMessage(), e);
                }
            }
        }
    }
    for (Artifact a : compileArtifactMap.values()) {
        classpathElements.add(a.getFile());
    }
    if (additionalDependencies != null) {
        for (Dependency dependency : additionalDependencies) {
            Artifact artifact = resolveDependency(dependency);
            getLog().debug("add additional artifact with path " + artifact.getFile());
            classpathElements.add(artifact.getFile());
        }
    }
    return classpathElements;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) PatternExclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter) Dependency(org.apache.maven.model.Dependency) BootclasspathArtifact(org.apache.maven.plugins.javadoc.options.BootclasspathArtifact) JavadocPathArtifact(org.apache.maven.plugins.javadoc.options.JavadocPathArtifact) ResourcesArtifact(org.apache.maven.plugins.javadoc.options.ResourcesArtifact) Artifact(org.apache.maven.artifact.Artifact) TagletArtifact(org.apache.maven.plugins.javadoc.options.TagletArtifact) DocletArtifact(org.apache.maven.plugins.javadoc.options.DocletArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) AndFilter(org.apache.maven.shared.artifact.filter.resolve.AndFilter) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) TransformableFilter(org.apache.maven.shared.artifact.filter.resolve.TransformableFilter) MavenProject(org.apache.maven.project.MavenProject) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 9 with TransformableFilter

use of org.apache.maven.shared.artifact.filter.resolve.TransformableFilter in project maven-plugins by apache.

the class AbstractJavadocMojo method getDependencySourcePaths.

/**
 * Resolve dependency sources so they can be included directly in the javadoc process. To customize this,
 * override {@link AbstractJavadocMojo#configureDependencySourceResolution(SourceResolverConfig)}.
 * @return List of source paths.
 * @throws MavenReportException {@link MavenReportException}
 */
protected final Map<String, Collection<String>> getDependencySourcePaths() throws MavenReportException {
    try {
        if (sourceDependencyCacheDir.exists()) {
            FileUtils.forceDelete(sourceDependencyCacheDir);
            sourceDependencyCacheDir.mkdirs();
        }
    } catch (IOException e) {
        throw new MavenReportException("Failed to delete cache directory: " + sourceDependencyCacheDir + "\nReason: " + e.getMessage(), e);
    }
    final SourceResolverConfig config = getDependencySourceResolverConfig();
    final List<TransformableFilter> andFilters = new ArrayList<>();
    final List<String> dependencyIncludes = dependencySourceIncludes;
    final List<String> dependencyExcludes = dependencySourceExcludes;
    if (!includeTransitiveDependencySources || isNotEmpty(dependencyIncludes) || isNotEmpty(dependencyExcludes)) {
        if (!includeTransitiveDependencySources) {
            andFilters.add(createDependencyArtifactFilter());
        }
        if (isNotEmpty(dependencyIncludes)) {
            andFilters.add(new PatternInclusionsFilter(dependencyIncludes));
        }
        if (isNotEmpty(dependencyExcludes)) {
            andFilters.add(new PatternExclusionsFilter(dependencyExcludes));
        }
        config.withFilter(new AndFilter(andFilters));
    }
    try {
        return resourceResolver.resolveDependencySourcePaths(config);
    } catch (final ArtifactResolutionException e) {
        throw new MavenReportException("Failed to resolve one or more javadoc source/resource artifacts:\n\n" + e.getMessage(), e);
    } catch (final ArtifactNotFoundException e) {
        throw new MavenReportException("Failed to resolve one or more javadoc source/resource artifacts:\n\n" + e.getMessage(), e);
    }
}
Also used : PatternInclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternInclusionsFilter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PatternExclusionsFilter(org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter) AndFilter(org.apache.maven.shared.artifact.filter.resolve.AndFilter) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) TransformableFilter(org.apache.maven.shared.artifact.filter.resolve.TransformableFilter) SourceResolverConfig(org.apache.maven.plugins.javadoc.resolver.SourceResolverConfig) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

TransformableFilter (org.apache.maven.shared.artifact.filter.resolve.TransformableFilter)9 ArrayList (java.util.ArrayList)6 Artifact (org.apache.maven.artifact.Artifact)6 AndFilter (org.apache.maven.shared.artifact.filter.resolve.AndFilter)6 PatternExclusionsFilter (org.apache.maven.shared.artifact.filter.resolve.PatternExclusionsFilter)6 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)5 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)5 PatternInclusionsFilter (org.apache.maven.shared.artifact.filter.resolve.PatternInclusionsFilter)5 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)3 Dependency (org.apache.maven.model.Dependency)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 MavenProject (org.apache.maven.project.MavenProject)3 MavenReportException (org.apache.maven.reporting.MavenReportException)3 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)3 IOException (java.io.IOException)2 File (java.io.File)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 SourceResolverConfig (org.apache.maven.plugin.javadoc.resolver.SourceResolverConfig)1 BootclasspathArtifact (org.apache.maven.plugins.javadoc.options.BootclasspathArtifact)1