Example usage for org.apache.maven.artifact Artifact setDependencyFilter

List of usage examples for org.apache.maven.artifact Artifact setDependencyFilter

Introduction

In this page you can find the example usage for org.apache.maven.artifact Artifact setDependencyFilter.

Prototype

void setDependencyFilter(ArtifactFilter artifactFilter);

Source Link

Usage

From source file:com.alibaba.citrus.maven.eclipse.base.ide.AbstractIdeSupportMojo.java

License:Apache License

/**
 * Apply exclusion filters to direct AND transitive dependencies.
 *
 * @param artifact//from ww w . j  av a 2 s  .c  o  m
 * @param dependency
 */
private void handleExclusions(Artifact artifact, Dependency dependency) {

    List exclusions = new ArrayList();
    for (Iterator j = dependency.getExclusions().iterator(); j.hasNext();) {
        Exclusion e = (Exclusion) j.next();
        exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); //$NON-NLS-1$
    }

    ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

    artifact.setDependencyFilter(newFilter);
}

From source file:com.github.zhve.ideaplugin.ArtifactDependencyResolver.java

License:Apache License

/**
 * Convert Dependency to Artifact/*w ww  . j a  v  a2  s  .  c om*/
 *
 * @param artifactFactory standard Maven's factory to create artifacts
 * @param dependency      dependency
 * @return artifact
 * @throws InvalidVersionSpecificationException if VersionRange is invalid
 */
private Artifact toDependencyArtifact(ArtifactFactory artifactFactory, Dependency dependency)
        throws InvalidVersionSpecificationException {
    // instantiate
    Artifact dependencyArtifact = artifactFactory.createDependencyArtifact(dependency.getGroupId(),
            dependency.getArtifactId(), VersionRange.createFromVersionSpec(dependency.getVersion()),
            dependency.getType(), dependency.getClassifier(),
            dependency.getScope() == null ? Artifact.SCOPE_COMPILE : dependency.getScope(), null,
            dependency.isOptional());

    // apply exclusions is needed
    if (!dependency.getExclusions().isEmpty()) {
        List<String> exclusions = new ArrayList<String>();
        for (Exclusion exclusion : dependency.getExclusions())
            exclusions.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
        dependencyArtifact.setDependencyFilter(new ExcludesArtifactFilter(exclusions));
    }

    // additional
    if (Artifact.SCOPE_SYSTEM.equalsIgnoreCase(dependency.getScope()))
        dependencyArtifact.setFile(new File(dependency.getSystemPath()));

    return dependencyArtifact;
}

From source file:com.github.zhve.ideaplugin.ArtifactDependencyResolver.java

License:Apache License

private static Artifact toDependencyArtifact(ArtifactFactory artifactFactory, Artifact dependency,
        String inheritedScope) {//from w w  w .j  a v  a  2  s  . c om
    Artifact dependencyArtifact = artifactFactory.createDependencyArtifact(dependency.getGroupId(),
            dependency.getArtifactId(), dependency.getVersionRange(), dependency.getType(),
            dependency.getClassifier(), dependency.getScope(), inheritedScope, dependency.isOptional());
    if (dependencyArtifact != null)
        dependencyArtifact.setDependencyFilter(dependency.getDependencyFilter());
    return dependencyArtifact;
}

From source file:org.apache.camel.guice.maven.RunMojo.java

License:Apache License

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;//w ww.ja  v a2  s . c  o m
        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 (Iterator<?> j = dependency.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

        art.setDependencyFilter(newFilter);

        artifacts.add(art);
    }

    return artifacts;
}

From source file:org.apache.tuscany.maven.plugin.eclipse.AbstractIdeSupportMojo.java

License:Apache License

/**
 * Returns the list of project artifacts. Also artifacts generated from referenced projects will be added, but with
 * the <code>resolved</code> property set to true.
 *
 * @return list of projects artifacts/* w w w .j  a v a  2 s  .co m*/
 * @throws MojoExecutionException if unable to parse dependency versions
 */
private Set getProjectArtifacts() throws MojoExecutionException {
    // keep it sorted, this should avoid random classpath order in tests
    Set artifacts = new TreeSet();

    for (Iterator dependencies = getProject().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(Messages.getString("AbstractIdeSupportMojo.unabletoparseversion", //$NON-NLS-1$
                    new Object[] { dependency.getArtifactId(), dependency.getVersion(), dependency.getManagementKey(),
                            e.getMessage() }),
                    e);
        }

        String type = dependency.getType();
        if (type == null) {
            type = Constants.PROJECT_PACKAGING_JAR;
        }
        String classifier = dependency.getClassifier();
        boolean optional = dependency.isOptional();
        String scope = dependency.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }

        Artifact art = getArtifactFactory().createDependencyArtifact(groupId, artifactId, versionRange, type,
                classifier, scope, optional);

        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            art.setFile(new File(dependency.getSystemPath()));
        }

        List exclusions = new ArrayList();
        for (Iterator j = dependency.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); //$NON-NLS-1$
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

        art.setDependencyFilter(newFilter);

        artifacts.add(art);
    }

    return artifacts;
}

From source file:org.codehaus.mojo.pluginsupport.MojoSupport.java

License:Apache License

protected Set getProjectArtifacts(final MavenProject project, final boolean resolve)
        throws MojoExecutionException {
    Set artifacts = new HashSet();

    Iterator dependencies = project.getDependencies().iterator();
    while (dependencies.hasNext()) {
        Dependency dep = (Dependency) dependencies.next();

        String groupId = dep.getGroupId();
        String artifactId = dep.getArtifactId();
        VersionRange versionRange = VersionRange.createFromVersion(dep.getVersion());
        String type = dep.getType();
        if (type == null) {
            type = "jar";
        }//from   w  w w.  j  a va  2 s  .c o  m

        String classifier = dep.getClassifier();
        boolean optional = dep.isOptional();
        String scope = dep.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }

        Artifact artifact = getArtifactFactory().createDependencyArtifact(groupId, artifactId, versionRange,
                type, classifier, scope, optional);

        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            artifact.setFile(new File(dep.getSystemPath()));
        }

        List exclusions = new ArrayList();
        for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
        artifact.setDependencyFilter(newFilter);

        if (resolve && !artifact.isResolved()) {
            log.debug("Resolving artifact: " + artifact);
            artifact = resolveArtifact(artifact);
        }

        artifacts.add(artifact);
    }

    return artifacts;
}

From source file:org.codehaus.mojo.sysdeo.ide.AbstractIdeSupportMojo.java

License:Apache License

/**
 * Returns the list of project artifacts. Also artifacts generated from referenced projects will be added, but with
 * the <code>resolved</code> property set to true.
 *
 * @return list of projects artifacts//from www. j  ava 2 s .  c o m
 * @throws MojoExecutionException if unable to parse dependency versions
 */
private Set getProjectArtifacts() throws MojoExecutionException {
    // keep it sorted, this should avoid random classpath order in tests
    Set artifacts = new TreeSet();

    for (Iterator dependencies = getProject().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(Messages.getString("unabletoparseversion", new Object[] { //$NON-NLS-1$
                    dependency.getArtifactId(), dependency.getVersion(), dependency.getManagementKey(),
                    e.getMessage() }), e);
        }

        String type = dependency.getType();
        if (type == null) {
            type = "jar"; //$NON-NLS-1$
        }
        String classifier = dependency.getClassifier();
        boolean optional = dependency.isOptional();
        String scope = dependency.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }

        Artifact art = getArtifactFactory().createDependencyArtifact(groupId, artifactId, versionRange, type,
                classifier, scope, optional);

        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            art.setFile(new File(dependency.getSystemPath()));
        }

        List exclusions = new ArrayList();
        for (Iterator j = dependency.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId()); //$NON-NLS-1$
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

        art.setDependencyFilter(newFilter);

        artifacts.add(art);
    }

    return artifacts;
}

From source file:org.eclipse.che.plugin.gwt.stub.ProjectStub.java

License:Open Source License

private void setupDependencyArtifacts(Model model) {
    Set<Artifact> artifacts = new HashSet<>();
    ArtifactStubFactory artifactStubFactory = new ArtifactStubFactory();

    for (Dependency dependency : model.getDependencies()) {
        Artifact artifact;

        try {/*from   w  w w.  ja  v  a2s.  c o  m*/
            artifact = artifactStubFactory.createArtifact(dependency.getGroupId(), dependency.getArtifactId(),
                    System.getProperty("currentVersion"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        List<String> patterns = dependency.getExclusions().stream()
                .map(exclusion -> exclusion.getGroupId() + ":" + exclusion.getArtifactId())
                .collect(Collectors.toList());

        artifact.setDependencyFilter(new ExcludesArtifactFilter(patterns));
        artifacts.add(artifact);
    }

    setDependencyArtifacts(artifacts);
}

From source file:org.jfrog.jade.plugins.ide.AbstractIdeMojo.java

License:Apache License

protected Set getProjectArtifacts() throws InvalidVersionSpecificationException {
    Set<Artifact> artifacts = new HashSet<Artifact>();

    for (Iterator dependencies = executedProject.getDependencies().iterator(); dependencies.hasNext();) {
        Dependency dep = (Dependency) dependencies.next();

        String groupId = dep.getGroupId();
        String artifactId = dep.getArtifactId();
        VersionRange versionRange = VersionRange.createFromVersionSpec(dep.getVersion());
        String type = dep.getType();
        if (type == null) {
            type = "jar";
        }/* w  w  w .j  a v a  2  s .  co m*/
        String classifier = dep.getClassifier();
        boolean optional = dep.isOptional();
        String scope = dep.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }

        Artifact artifact = getArtifactFactory().createDependencyArtifact(groupId, artifactId, versionRange,
                type, classifier, scope, optional);

        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            artifact.setFile(new File(dep.getSystemPath()));
        }

        List<String> exclusions = new ArrayList<String>();
        for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

        artifact.setDependencyFilter(newFilter);

        artifacts.add(artifact);
    }

    return artifacts;
}

From source file:org.universAAL.maven.treebuilder.DependencyTreeBuilder.java

License:Apache License

/**
 * Method resolves provided node with the use of provided
 * ArtifactMetadataSource and taking into account ManagedVersionMap. Output
 * is passed to listeners, passed as argument, which are notified about all
 * events related to dependencies detected in the tree.
 * /*from   ww  w. ja va2 s .c om*/
 * @param parentNode
 *            Parent node
 * @param child
 *            Child node
 * @param filter
 *            Filter for filtering artifacts for the resolving process.
 * @param managedVersions
 *            Map of managed versions.
 * @param listener
 *            Listener to be notified about events related to resolution
 *            process.
 * @param source
 *            ArtifactMetadataSource object passed by maven.
 * @param parentArtifact
 *            Parent artifact
 * @return returns true if the child should be recursively resolved.
 * @throws OverConstrainedVersionException
 *             Occurs when ranges exclude each other and no valid value
 *             remains.
 * @throws ArtifactMetadataRetrievalException
 *             Error while retrieving repository metadata from the
 *             repository
 */
private boolean resolveChildNode(final ResolutionNode parentNode, final ResolutionNode child,
        final ArtifactFilter filter, final ManagedVersionMap managedVersions,
        final DependencyTreeResolutionListener listener, final ArtifactMetadataSource source,
        final Artifact parentArtifact)
        throws OverConstrainedVersionException, ArtifactMetadataRetrievalException {
    // We leave in optional ones, but don't pick up its dependencies
    if (!child.isResolved() && (!child.getArtifact().isOptional() || child.isChildOfRootNode())) {
        Artifact artifact = child.getArtifact();
        artifact.setDependencyTrail(parentNode.getDependencyTrail());

        List childRemoteRepositories = child.getRemoteRepositories();
        try {
            Object childKey;
            do {
                childKey = child.getKey();

                if (managedVersions.containsKey(childKey)) {
                    // If this child node is a managed dependency,
                    // ensure
                    // we are using the dependency management
                    // version
                    // of this child if applicable b/c we want to
                    // use the
                    // managed version's POM, *not* any other
                    // version's POM.
                    // We retrieve the POM below in the retrieval
                    // step.
                    manageArtifact(child, managedVersions);

                    // Also, we need to ensure that any exclusions
                    // it presents are
                    // added to the artifact before we retrieve the
                    // metadata
                    // for the artifact; otherwise we may end up
                    // with unwanted
                    // dependencies.
                    Artifact ma = (Artifact) managedVersions.get(childKey);
                    ArtifactFilter managedExclusionFilter = ma.getDependencyFilter();
                    if (null != managedExclusionFilter) {
                        if (null != artifact.getDependencyFilter()) {
                            AndArtifactFilter aaf = new AndArtifactFilter();
                            aaf.add(artifact.getDependencyFilter());
                            aaf.add(managedExclusionFilter);
                            artifact.setDependencyFilter(aaf);
                        } else {
                            artifact.setDependencyFilter(managedExclusionFilter);
                        }
                    }
                }

                if (artifact.getVersion() == null) {
                    // set the recommended version
                    // TODO: maybe its better to just pass the range
                    // through to retrieval and use a
                    // transformation?
                    ArtifactVersion version;
                    if (artifact.isSelectedVersionKnown()) {
                        version = artifact.getSelectedVersion();
                    } else {
                        // go find the version
                        List versions = artifact.getAvailableVersions();
                        if (versions == null) {
                            versions = source.retrieveAvailableVersions(artifact, localRepository,
                                    childRemoteRepositories);
                            artifact.setAvailableVersions(versions);
                        }

                        Collections.sort(versions);

                        VersionRange versionRange = artifact.getVersionRange();

                        version = versionRange.matchVersion(versions);

                        if (version == null) {
                            if (versions.isEmpty()) {
                                throw new OverConstrainedVersionException(
                                        "No versions are present in the repository for the artifact with a range "
                                                + versionRange,
                                        artifact, childRemoteRepositories);
                            }

                            throw new OverConstrainedVersionException("Couldn't find a version in " + versions
                                    + " to match range " + versionRange, artifact, childRemoteRepositories);
                        }
                    }

                    // this is dangerous because
                    // artifact.getSelectedVersion() can
                    // return null. However it is ok here because we
                    // first check if the
                    // selected version is known. As currently coded
                    // we can't get a null here.
                    artifact.selectVersion(version.toString());
                    fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listener, child);
                }

                // rotgier: it is not compatible with maven 3
                // Artifact relocated = source.retrieveRelocatedArtifact(
                // artifact, localRepository, childRemoteRepositories);
                // if (relocated != null && !artifact.equals(relocated)) {
                // relocated.setDependencyFilter(artifact
                // .getDependencyFilter());
                // artifact = relocated;
                // child.setArtifact(artifact);
                // }
            } while (!childKey.equals(child.getKey()));

            if (parentArtifact != null && parentArtifact.getDependencyFilter() != null
                    && !parentArtifact.getDependencyFilter().include(artifact)) {
                // MNG-3769: the [probably relocated] artifact is
                // excluded.
                // We could process exclusions on relocated artifact
                // details in the
                // MavenMetadataSource.createArtifacts(..) step, BUT
                // that would
                // require resolving the POM from the repository
                // very early on in
                // the build.
                return true;
            }

            ResolutionGroup rGroup = source.retrieve(artifact, localRepository, childRemoteRepositories);

            // TODO might be better to have source.retrieve() throw
            // a specific exception for this situation
            // and catch here rather than have it return null
            if (rGroup == null) {
                // relocated dependency artifact is declared
                // excluded, no need to add and recurse further
                return true;
            }
            child.addDependencies(rGroup.getArtifacts(), rGroup.getResolutionRepositories(), filter);

        } catch (CyclicDependencyException e) {
            // would like to throw this, but we have crappy stuff in
            // the repo

            fireEvent(ResolutionListener.OMIT_FOR_CYCLE, listener,
                    new ResolutionNode(e.getArtifact(), childRemoteRepositories, child));
        } catch (ArtifactMetadataRetrievalException e) {
            artifact.setDependencyTrail(parentNode.getDependencyTrail());
            throw e;
        }
    } else {
        return true;
    }
    return false;
}