Search in sources :

Example 1 with ArtifactIncludeFilterTransformer

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

the class ResourceResolver method resolveBundlesFromArtifacts.

private List<JavadocBundle> resolveBundlesFromArtifacts(final SourceResolverConfig config, final List<Artifact> artifacts) throws IOException {
    final List<Artifact> toResolve = new ArrayList<Artifact>(artifacts.size());
    for (final Artifact artifact : artifacts) {
        if (config.filter() != null && !new ArtifactIncludeFilterTransformer().transform(config.filter()).include(artifact)) {
            continue;
        }
        if (config.includeCompileSources()) {
            toResolve.add(createResourceArtifact(artifact, AbstractJavadocMojo.JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER, config));
        }
        if (config.includeTestSources()) {
            toResolve.add(createResourceArtifact(artifact, AbstractJavadocMojo.TEST_JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER, config));
        }
    }
    List<String> dirs = null;
    try {
        dirs = resolveAndUnpack(toResolve, config, RESOURCE_VALID_CLASSIFIERS, false);
    } catch (ArtifactResolutionException e) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(e.getMessage(), e);
        }
    } catch (ArtifactNotFoundException e) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(e.getMessage(), e);
        }
    }
    List<JavadocBundle> result = new ArrayList<JavadocBundle>();
    if (dirs != null) {
        for (String d : dirs) {
            File dir = new File(d);
            File resources = new File(dir, ResourcesBundleMojo.RESOURCES_DIR_PATH);
            JavadocOptions options = null;
            File javadocOptions = new File(dir, ResourcesBundleMojo.BUNDLE_OPTIONS_PATH);
            if (javadocOptions.exists()) {
                FileInputStream reader = null;
                try {
                    reader = new FileInputStream(javadocOptions);
                    options = new JavadocOptionsXpp3Reader().read(reader);
                } catch (XmlPullParserException e) {
                    IOException error = new IOException("Failed to parse javadoc options: " + e.getMessage());
                    error.initCause(e);
                    throw error;
                } finally {
                    close(reader);
                }
            }
            result.add(new JavadocBundle(options, resources));
        }
    }
    return result;
}
Also used : ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) FileInputStream(java.io.FileInputStream) JavadocOptionsXpp3Reader(org.apache.maven.plugin.javadoc.options.io.xpp3.JavadocOptionsXpp3Reader) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) JavadocOptions(org.apache.maven.plugin.javadoc.options.JavadocOptions) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) File(java.io.File)

Example 2 with ArtifactIncludeFilterTransformer

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

the class ResourceResolver method resolveAndUnpack.

private List<String> resolveAndUnpack(final List<Artifact> artifacts, final SourceResolverConfig config, final List<String> validClassifiers, final boolean propagateErrors) throws ArtifactResolutionException, ArtifactNotFoundException {
    // NOTE: Since these are '-sources' and '-test-sources' artifacts, they won't actually 
    // resolve transitively...this is just used to aggregate resolution failures into a single 
    // exception.
    final Set<Artifact> artifactSet = new LinkedHashSet<Artifact>(artifacts);
    final Artifact pomArtifact = config.project().getArtifact();
    final ArtifactRepository localRepo = config.localRepository();
    final List<ArtifactRepository> remoteRepos = config.project().getRemoteArtifactRepositories();
    final ArtifactFilter filter;
    if (config.filter() != null) {
        filter = new ArtifactIncludeFilterTransformer().transform(config.filter());
    } else {
        filter = null;
    }
    ArtifactFilter resolutionFilter = null;
    if (filter != null) {
        // Wrap the filter in a ProjectArtifactFilter in order to always include the pomArtifact for resolution.
        // NOTE that this is necessary, b/c the -sources artifacts are added dynamically to the pomArtifact
        // and the resolver also checks the dependency trail with the given filter, thus the pomArtifact has
        // to be explicitly included by the filter, otherwise the -sources artifacts won't be resolved.
        resolutionFilter = new ProjectArtifactFilter(pomArtifact, filter);
    }
    Map<String, Artifact> managed = config.project().getManagedVersionMap();
    final ArtifactResolutionResult resolutionResult = resolver.resolveTransitively(artifactSet, pomArtifact, managed, localRepo, remoteRepos, artifactMetadataSource, resolutionFilter);
    final List<String> result = new ArrayList<String>(artifacts.size());
    for (final Artifact a : (Collection<Artifact>) resolutionResult.getArtifacts()) {
        if (!validClassifiers.contains(a.getClassifier()) || (filter != null && !filter.include(a))) {
            continue;
        }
        final File d = new File(config.outputBasedir(), a.getArtifactId() + "-" + a.getVersion() + "-" + a.getClassifier());
        if (!d.exists()) {
            d.mkdirs();
        }
        try {
            final UnArchiver unArchiver = archiverManager.getUnArchiver(a.getType());
            unArchiver.setDestDirectory(d);
            unArchiver.setSourceFile(a.getFile());
            unArchiver.extract();
            result.add(d.getAbsolutePath());
        } catch (final NoSuchArchiverException e) {
            if (propagateErrors) {
                throw new ArtifactResolutionException("Failed to retrieve valid un-archiver component: " + a.getType(), a, e);
            }
        } catch (final ArchiverException e) {
            if (propagateErrors) {
                throw new ArtifactResolutionException("Failed to unpack: " + a.getId(), a, e);
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) Collection(java.util.Collection) File(java.io.File) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 3 with ArtifactIncludeFilterTransformer

use of org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer 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 4 with ArtifactIncludeFilterTransformer

use of org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer 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 5 with ArtifactIncludeFilterTransformer

use of org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer 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)

Aggregations

Artifact (org.apache.maven.artifact.Artifact)6 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)6 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)5 LinkedHashSet (java.util.LinkedHashSet)3 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 JavadocOptions (org.apache.maven.plugin.javadoc.options.JavadocOptions)1 JavadocOptionsXpp3Reader (org.apache.maven.plugin.javadoc.options.io.xpp3.JavadocOptionsXpp3Reader)1 ScopeFilter (org.apache.maven.shared.artifact.filter.resolve.ScopeFilter)1 TransformableFilter (org.apache.maven.shared.artifact.filter.resolve.TransformableFilter)1 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)1