Example usage for org.apache.maven.artifact.resolver ArtifactResolver resolveTransitively

List of usage examples for org.apache.maven.artifact.resolver ArtifactResolver resolveTransitively

Introduction

In this page you can find the example usage for org.apache.maven.artifact.resolver ArtifactResolver resolveTransitively.

Prototype

@Deprecated
    ArtifactResolutionResult resolveTransitively(Set<Artifact> artifacts, Artifact originatingArtifact,
            List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository,
            ArtifactMetadataSource source, List<ResolutionListener> listeners)
            throws ArtifactResolutionException, ArtifactNotFoundException;

Source Link

Usage

From source file:de.tarent.maven.plugins.pkg.Utils.java

License:Open Source License

/**
 * Gathers the project's artifacts and the artifacts of all its (transitive)
 * dependencies filtered by the given filter instance.
 * /*ww w .j a  v  a 2 s .  c o  m*/
 * @param filter
 * @return
 * @throws ArtifactResolutionException
 * @throws ArtifactNotFoundException
 * @throws ProjectBuildingException
 * @throws InvalidDependencyVersionException
 */
@SuppressWarnings("unchecked")
public static Set<Artifact> findArtifacts(ArtifactFilter filter, ArtifactFactory factory,
        ArtifactResolver resolver, MavenProject project, Artifact artifact, ArtifactRepository local,
        List<ArtifactRepository> remoteRepos, ArtifactMetadataSource metadataSource)
        throws ArtifactResolutionException, ArtifactNotFoundException, ProjectBuildingException,
        InvalidDependencyVersionException {

    ArtifactResolutionResult result = resolver.resolveTransitively(project.getDependencyArtifacts(), artifact,
            local, remoteRepos, metadataSource, filter);

    return (Set<Artifact>) result.getArtifacts();
}

From source file:org.mobicents.maven.plugin.eclipse.ClasspathWriter.java

License:Open Source License

/**
 * Writes the .classpath file for eclipse.
 * /* ww w.j  a va2 s . c  o m*/
 * @param projects
 *            the list of projects from which the .classpath will get its
 *            dependencies.
 * @param repositoryVariableName
 *            the name of the maven repository variable.
 * @param artifactFactory
 *            the factory for constructing artifacts.
 * @param artifactResolver
 *            the artifact resolver.
 * @param localRepository
 *            the local repository instance.
 * @param artifactMetadataSource
 * @param classpathArtifactTypes
 *            the artifacts types that are allowed in the classpath file.
 * @param remoteRepositories
 *            the list of remote repository instances.
 * @param resolveTransitiveDependencies
 *            whether or not dependencies shall be transitively resolved.
 * @param merge
 *            anything extra (not auto-generated), that should be "merged"
 *            into the generated .classpath
 * @param classpathExcludes
 * @param includeTestsDirectory
 * @param includeResourcesDirectory
 * @throws Exception
 */
public void write(final List projects, final String repositoryVariableName,
        final ArtifactFactory artifactFactory, final ArtifactResolver artifactResolver,
        final ArtifactRepository localRepository, final ArtifactMetadataSource artifactMetadataSource,
        final Set classpathArtifactTypes, final List remoteRepositories,
        final boolean resolveTransitiveDependencies, final String merge, Set classpathExcludes,
        boolean includeResourcesDirectory) throws Exception {
    final String rootDirectory = PathNormalizer.normalizePath(this.project.getBasedir().toString());
    final File classpathFile = new File(rootDirectory, ".classpath");
    final FileWriter fileWriter = new FileWriter(classpathFile);
    final XMLWriter writer = new PrettyPrintXMLWriter(fileWriter, "UTF-8", null);
    writer.startElement("classpath");

    final Set projectArtifactIds = new LinkedHashSet();
    for (final Iterator iterator = projects.iterator(); iterator.hasNext();) {
        final MavenProject project = (MavenProject) iterator.next();
        final Artifact projectArtifact = artifactFactory.createArtifact(project.getGroupId(),
                project.getArtifactId(), project.getVersion(), null, project.getPackaging());
        projectArtifactIds.add(projectArtifact.getId());
    }

    // - collect the source roots for the root project (if they are any)
    Set<String> sourceRoots = collectSourceRoots(this.project, rootDirectory, writer,
            includeResourcesDirectory);

    final Set allArtifacts = new LinkedHashSet(this.project.createArtifacts(artifactFactory, null, null));

    for (final Iterator iterator = projects.iterator(); iterator.hasNext();) {
        final MavenProject project = (MavenProject) iterator.next();
        sourceRoots.addAll(collectSourceRoots(project, rootDirectory, writer, includeResourcesDirectory));
        final Set artifacts = project.createArtifacts(artifactFactory, null, null);
        // - get the direct dependencies
        for (final Iterator artifactIterator = artifacts.iterator(); artifactIterator.hasNext();) {
            final Artifact artifact = (Artifact) artifactIterator.next();
            // - don't attempt to resolve the artifact if its part of the
            // project (we
            // infer this if it has the same id has one of the projects or
            // is in
            // the same groupId).
            if (!projectArtifactIds.contains(artifact.getId())
                    && !project.getGroupId().equals(artifact.getGroupId())) {
                artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository);
                allArtifacts.add(artifact);
            } else {
                allArtifacts.add(artifact);
            }
        }
    }

    // we have all source roots now, sort and write
    for (String sourceRoot : sourceRoots) {
        logger.info("Adding src path " + sourceRoot);
        this.writeClasspathEntry(writer, "src", sourceRoot);
    }

    // - remove the project artifacts
    for (final Iterator iterator = projects.iterator(); iterator.hasNext();) {
        final MavenProject project = (MavenProject) iterator.next();
        final Artifact projectArtifact = project.getArtifact();
        if (projectArtifact != null) {
            for (final Iterator artifactIterator = allArtifacts.iterator(); artifactIterator.hasNext();) {
                final Artifact artifact = (Artifact) artifactIterator.next();
                final String projectId = projectArtifact.getArtifactId();
                final String projectGroupId = projectArtifact.getGroupId();
                final String artifactId = artifact.getArtifactId();
                final String groupId = artifact.getGroupId();
                if (artifactId.equals(projectId) && groupId.equals(projectGroupId)) {
                    artifactIterator.remove();
                }
            }
        }
    }

    // - now we resolve transitively, if we have the flag on
    if (resolveTransitiveDependencies) {
        final Artifact rootProjectArtifact = artifactFactory.createArtifact(this.project.getGroupId(),
                this.project.getArtifactId(), this.project.getVersion(), null, this.project.getPackaging());

        final OrArtifactFilter filter = new OrArtifactFilter();
        filter.add(new ScopeArtifactFilter(Artifact.SCOPE_COMPILE));
        filter.add(new ScopeArtifactFilter(Artifact.SCOPE_PROVIDED));
        filter.add(new ScopeArtifactFilter(Artifact.SCOPE_TEST));
        final ArtifactResolutionResult result = artifactResolver.resolveTransitively(allArtifacts,
                rootProjectArtifact, localRepository, remoteRepositories, artifactMetadataSource, filter);

        allArtifacts.clear();
        allArtifacts.addAll(result.getArtifacts());
    }

    // remove excluded ones
    for (Iterator i = allArtifacts.iterator(); i.hasNext();) {
        Artifact artifact = (Artifact) i.next();

        if (classpathExcludes != null) {
            if (classpathExcludes.contains(artifact.getGroupId())) {
                logger.info("Excluding " + artifact + " from .classpath, groupId is excluded");
                i.remove();
            } else if (classpathExcludes.contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) {
                logger.info("Excluding " + artifact + " from .classpath, groupId:artifactId is excluded");
                i.remove();
            } else if (classpathExcludes.contains(
                    artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion())) {
                logger.info(
                        "Excluding " + artifact + " from .classpath, groupId:artifactId:version is excluded");
                i.remove();
            }
        }
    }

    final List allArtifactPaths = new ArrayList(allArtifacts);
    for (final ListIterator iterator = allArtifactPaths.listIterator(); iterator.hasNext();) {
        final Artifact artifact = (Artifact) iterator.next();
        if (classpathArtifactTypes.contains(artifact.getType())) {
            File artifactFile = artifact.getFile();
            if (artifactFile == null) {
                artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository);
                artifactFile = artifact.getFile();
            }
            if (artifactFile != null) {
                final String path = StringUtils.replace(PathNormalizer.normalizePath(artifactFile.toString()),
                        PathNormalizer.normalizePath(localRepository.getBasedir()), repositoryVariableName);
                iterator.set(path);
            } else {
                iterator.remove();
            }
        } else {
            iterator.remove();
        }
    }

    // - sort the paths
    Collections.sort(allArtifactPaths);

    for (final Iterator iterator = allArtifactPaths.iterator(); iterator.hasNext();) {
        String path = (String) iterator.next();
        if (path.startsWith(repositoryVariableName)) {
            this.writeClasspathEntry(writer, "var", path);
        } else {
            if (path.startsWith(rootDirectory)) {
                path = StringUtils.replace(path, rootDirectory + '/', "");
            }
            this.writeClasspathEntry(writer, "lib", path);
        }
    }

    this.writeClasspathEntry(writer, "con", "org.eclipse.jdt.launching.JRE_CONTAINER");

    String outputPath = StringUtils.replace(
            PathNormalizer.normalizePath(this.project.getBuild().getOutputDirectory()), rootDirectory, "");
    if (outputPath.startsWith("/")) {
        outputPath = outputPath.substring(1, outputPath.length());
    }
    this.writeClasspathEntry(writer, "output", outputPath);

    if (StringUtils.isNotBlank(merge)) {
        writer.writeMarkup(merge);
    }
    writer.endElement();

    logger.info("Classpath file written --> '" + classpathFile + "'");
    IOUtil.close(fileWriter);
}