Example usage for org.apache.maven.artifact.resolver ResolutionListener OMIT_FOR_NEARER

List of usage examples for org.apache.maven.artifact.resolver ResolutionListener OMIT_FOR_NEARER

Introduction

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

Prototype

int OMIT_FOR_NEARER

To view the source code for org.apache.maven.artifact.resolver ResolutionListener OMIT_FOR_NEARER.

Click Source Link

Usage

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

private ResolutionNode checkPreviousNodes(ResolutionNode node, List listeners, List previousNodes)
        throws OverConstrainedVersionException {
    for (Iterator i = previousNodes.iterator(); i.hasNext();) {
        ResolutionNode previous = (ResolutionNode) i.next();
        if (previous.isActive()) {
            // Version mediation
            VersionRange previousRange = previous.getArtifact().getVersionRange();
            VersionRange currentRange = node.getArtifact().getVersionRange();
            // TODO: why do we force the version on it? what if they
            // don't match?
            if (previousRange == null) {
                // version was already resolved
                node.getArtifact().setVersion(previous.getArtifact().getVersion());
            } else if (currentRange == null) {
                // version was already resolved
                previous.getArtifact().setVersion(node.getArtifact().getVersion());
            } else {
                // TODO: shouldn't need to double up on this work, only
                // done for simplicity of handling recommended
                // version but the restriction is identical
                VersionRange newRange = previousRange.restrict(currentRange);
                // TODO: ick. this forces the OCE that should have come
                // from the previous call. It is still correct
                if (newRange.isSelectedVersionKnown(previous.getArtifact())) {
                    fireEvent(ResolutionListener.RESTRICT_RANGE, listeners, node, previous.getArtifact(),
                            newRange);//from w  w w . j  a v  a 2s. c  om
                }
                previous.getArtifact().setVersionRange(newRange);
                node.getArtifact().setVersionRange(currentRange.restrict(previousRange));

                // Select an appropriate available version from the (now
                // restricted) range
                // Note this version was selected before to get the
                // appropriate POM
                // But it was reset by the call to setVersionRange on
                // restricting the version
                ResolutionNode[] resetNodes = { previous, node };
                for (int j = 0; j < 2; j++) {
                    Artifact resetArtifact = resetNodes[j].getArtifact();
                    if (resetArtifact.getVersion() == null && resetArtifact.getVersionRange() != null
                            && resetArtifact.getAvailableVersions() != null) {

                        resetArtifact.selectVersion(resetArtifact.getVersionRange()
                                .matchVersion(resetArtifact.getAvailableVersions()).toString());
                        fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listeners, resetNodes[j]);
                    }
                }
            }

            // Conflict Resolution
            // TODO: use as conflict resolver(s), chain

            // TODO: should this be part of mediation?
            // previous one is more dominant
            if (previous.getDepth() <= node.getDepth()) {
                checkScopeUpdate(node, previous, listeners);
            } else {
                checkScopeUpdate(previous, node, listeners);
            }

            if (previous.getDepth() <= node.getDepth()) {
                // previous was nearer
                fireEvent(ResolutionListener.OMIT_FOR_NEARER, listeners, node, previous.getArtifact());
                node.disable();
                node = previous;
            } else {
                fireEvent(ResolutionListener.OMIT_FOR_NEARER, listeners, previous, node.getArtifact());
                previous.disable();
            }
        }
    }
    return node;
}

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

private void fireEvent(int event, List listeners, ResolutionNode node, Artifact replacement,
        VersionRange newRange) {//from  w w w  .  j a  va 2s . c  o  m
    for (Iterator i = listeners.iterator(); i.hasNext();) {
        ResolutionListener listener = (ResolutionListener) i.next();

        switch (event) {
        case ResolutionListener.TEST_ARTIFACT:
            listener.testArtifact(node.getArtifact());
            break;
        case ResolutionListener.PROCESS_CHILDREN:
            listener.startProcessChildren(node.getArtifact());
            break;
        case ResolutionListener.FINISH_PROCESSING_CHILDREN:
            listener.endProcessChildren(node.getArtifact());
            break;
        case ResolutionListener.INCLUDE_ARTIFACT:
            listener.includeArtifact(node.getArtifact());
            break;
        case ResolutionListener.OMIT_FOR_NEARER:
            String version = node.getArtifact().getVersion();
            String replacementVersion = replacement.getVersion();
            if (version != null ? !version.equals(replacementVersion) : replacementVersion != null) {
                listener.omitForNearer(node.getArtifact(), replacement);
            }
            break;
        case ResolutionListener.OMIT_FOR_CYCLE:
            listener.omitForCycle(node.getArtifact());
            break;
        case ResolutionListener.UPDATE_SCOPE:
            listener.updateScope(node.getArtifact(), replacement.getScope());
            break;
        case ResolutionListener.UPDATE_SCOPE_CURRENT_POM:
            listener.updateScopeCurrentPom(node.getArtifact(), replacement.getScope());
            break;
        case ResolutionListener.MANAGE_ARTIFACT:
            listener.manageArtifact(node.getArtifact(), replacement);
            break;
        case ResolutionListener.SELECT_VERSION_FROM_RANGE:
            listener.selectVersionFromRange(node.getArtifact());
            break;
        case ResolutionListener.RESTRICT_RANGE:
            if (node.getArtifact().getVersionRange().hasRestrictions()
                    || replacement.getVersionRange().hasRestrictions()) {
                listener.restrictRange(node.getArtifact(), replacement, newRange);
            }
            break;
        default:
            throw new IllegalStateException("Unknown event: " + event);
        }
    }
}

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

License:Apache License

/**
 * FireEvent methods are used for sending events related resolution process
 * to the listeners passed as arguments.
 * /*from   w ww.  ja v  a2  s  . c  o  m*/
 * @param event
 *            Integer value of the event.
 * @param listener
 *            Listener which will be notified about the event.
 * @param node
 *            Node related to the event.
 * @param replacement
 *            Node which will replace the one passed in the previous
 *            argument.
 * @param newRange
 *            A new range which will be applied in case of RESTRICT_RANGE
 *            event.
 */
private void fireEvent(final int event, final DependencyTreeResolutionListener listener,
        final ResolutionNode node, final ResolutionNode replacement, final VersionRange newRange) {
    switch (event) {
    case ResolutionListener.TEST_ARTIFACT:
        listener.testArtifact(node.getArtifact());
        break;
    case ResolutionListener.PROCESS_CHILDREN:
        listener.startProcessChildren(node.getArtifact());
        break;
    case ResolutionListener.FINISH_PROCESSING_CHILDREN:
        listener.endProcessChildren(node.getArtifact());
        break;
    case ResolutionListener.INCLUDE_ARTIFACT:
        listener.includeArtifact(node);
        break;
    case ResolutionListener.OMIT_FOR_NEARER:
        listener.omitForNearer(node, replacement);
        break;
    case ResolutionListener.OMIT_FOR_CYCLE:
        listener.omitForCycle(node);
        break;
    case ResolutionListener.UPDATE_SCOPE:
        listener.updateScope(node, replacement.getArtifact().getScope());
        break;
    case ResolutionListener.UPDATE_SCOPE_CURRENT_POM:
        listener.updateScopeCurrentPom(node, replacement.getArtifact().getScope());
        break;
    case ResolutionListener.MANAGE_ARTIFACT_VERSION:
        if (listener instanceof ResolutionListenerForDepMgmt) {
            ResolutionListenerForDepMgmt asImpl = listener;
            asImpl.manageArtifactVersion(node.getArtifact(), replacement.getArtifact());
        } else {
            listener.manageArtifact(node.getArtifact(), replacement.getArtifact());
        }
        break;
    case ResolutionListener.MANAGE_ARTIFACT_SCOPE:
        if (listener instanceof ResolutionListenerForDepMgmt) {
            ResolutionListenerForDepMgmt asImpl = listener;
            asImpl.manageArtifactScope(node.getArtifact(), replacement.getArtifact());
        } else {
            listener.manageArtifact(node.getArtifact(), replacement.getArtifact());
        }
        break;
    case ResolutionListener.SELECT_VERSION_FROM_RANGE:
        listener.selectVersionFromRange(node.getArtifact());
        break;
    case ResolutionListener.RESTRICT_RANGE:
        if (node.getArtifact().getVersionRange().hasRestrictions()
                || replacement.getArtifact().getVersionRange().hasRestrictions()) {
            listener.restrictRange(node.getArtifact(), replacement.getArtifact(), newRange);
        }
        break;
    default:
        throw new IllegalStateException("Unknown event: " + event);
    }
}

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

License:Apache License

/**
 * The heart of the tree builder. Recursively resolves provided artifact.
 * Output is passed to listeners, passed as argument, which are notified
 * about all dependencies detected in the tree. Resolving of each child node
 * is delegated to resolveChildNode method.
 * //  ww  w  .  ja  v a  2s.c o  m
 * @param originatingArtifact
 *            Rootnode of recursed subtree.
 * @param node
 *            Current node which is resolved.
 * @param resolvedArtifacts
 *            Map which is used for remembering already resolved artifacts.
 *            Artifacts are indexed by a key which calculation algorithm is
 *            the same as the one present in calculateDepKey method. Thanks
 *            to this map, duplicates and conflicts are detected and
 *            resolved.
 * @param managedVersions
 *            Information about dependency management extracted from the
 *            subtree rootnode - a maven project.
 * @param localRepository
 *            Local maven repository.
 * @param remoteRepositories
 *            Remote repositories provided by maven.
 * @param source
 *            ArtifactMetadataSource provided by maven.
 * @param filter
 *            Filter used for unfiltering artifacts which should not be
 *            included in the dependency tree.
 * @param listener
 *            Listener used for providing the output of the resolve process.
 * @param transitive
 *            If this parameter is false than the children of current node
 *            are not resolved.
 * 
 * @throws CyclicDependencyException
 *             Exception thrown when cyclic dependency detected.
 * @throws ArtifactResolutionException
 *             Exception thrown when a problem with artifact resolution
 *             occurs.
 * @throws OverConstrainedVersionException
 *             Occurs when ranges exclude each other and no valid value
 *             remains.
 * @throws ArtifactMetadataRetrievalException
 *             Error while retrieving repository metadata from the
 *             repository.
 * @throws NoSuchFieldException
 *             Signals that the class doesn't have a field of a specified
 *             name.
 * @throws SecurityException
 *             Thrown by the security manager to indicate a security
 *             violation.
 * @throws IllegalAccessException
 *             When illegal access is performed in the curse of java
 *             reflection operations.
 * @throws IllegalArgumentException
 *             Thrown to indicate that an illegal or inappropriate argument
 *             has been passed.
 */
private void recurse(final Artifact originatingArtifact, final ResolutionNode node, final Map resolvedArtifacts,
        final ManagedVersionMap managedVersions, final ArtifactRepository localRepository,
        final List remoteRepositories, final ArtifactMetadataSource source, final ArtifactFilter filter,
        final DependencyTreeResolutionListener listener, final boolean transitive,
        final Set<String> separatedGroupIds) throws CyclicDependencyException, ArtifactResolutionException,
        OverConstrainedVersionException, ArtifactMetadataRetrievalException, SecurityException,
        NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    try {

        fireEvent(ResolutionListener.TEST_ARTIFACT, listener, node);
        Object key = node.getKey();

        // TODO: Does this check need to happen here? Had to add the same
        // call
        // below when we iterate on child nodes -- will that suffice?
        if (managedVersions.containsKey(key)) {
            manageArtifact(node, managedVersions);
        }

        List previousNodes = (List) resolvedArtifacts.get(key);
        if (previousNodes != null) {
            for (Iterator i = previousNodes.iterator(); i.hasNext();) {
                ResolutionNode previous = (ResolutionNode) i.next();

                if (previous.isActive()) {
                    // Version mediation
                    VersionRange previousRange = previous.getArtifact().getVersionRange();
                    VersionRange currentRange = node.getArtifact().getVersionRange();

                    if (previousRange != null && currentRange != null) {
                        // TODO: shouldn't need to double up on this work,
                        // only
                        // done for simplicity of handling recommended
                        // version but the restriction is identical
                        VersionRange newRange = previousRange.restrict(currentRange);
                        // TODO: ick. this forces the OCE that should have
                        // come
                        // from the previous call. It is still correct
                        if (newRange.isSelectedVersionKnown(previous.getArtifact())) {
                            fireEvent(ResolutionListener.RESTRICT_RANGE, listener, node, previous, newRange);
                        }
                        previous.getArtifact().setVersionRange(newRange);
                        node.getArtifact().setVersionRange(currentRange.restrict(previousRange));

                        // Select an appropriate available version from the
                        // (now
                        // restricted) range
                        // Note this version was selected before to get the
                        // appropriate POM
                        // But it was reset by the call to setVersionRange
                        // on
                        // restricting the version
                        ResolutionNode[] resetNodes = { previous, node };
                        for (int j = 0; j < 2; j++) {
                            Artifact resetArtifact = resetNodes[j].getArtifact();

                            // MNG-2123: if the previous node was not a
                            // range,
                            // then it wouldn't have any available
                            // versions. We just clobbered the selected
                            // version
                            // above. (why? i have no idea.)
                            // So since we are here and this is ranges we
                            // must
                            // go figure out the version (for a third
                            // time...)
                            if (resetArtifact.getVersion() == null && resetArtifact.getVersionRange() != null) {

                                // go find the version. This is a total
                                // hack.
                                // See previous comment.
                                List versions = resetArtifact.getAvailableVersions();
                                if (versions == null) {
                                    try {
                                        versions = source.retrieveAvailableVersions(resetArtifact,
                                                localRepository, remoteRepositories);
                                        resetArtifact.setAvailableVersions(versions);
                                    } catch (ArtifactMetadataRetrievalException e) {
                                        resetArtifact.setDependencyTrail(node.getDependencyTrail());
                                        throw e;
                                    }
                                }
                                // end hack

                                // MNG-2861: match version can return null
                                ArtifactVersion selectedVersion = resetArtifact.getVersionRange()
                                        .matchVersion(resetArtifact.getAvailableVersions());
                                if (selectedVersion != null) {
                                    resetArtifact.selectVersion(selectedVersion.toString());
                                } else {
                                    throw new OverConstrainedVersionException(
                                            " Unable to find a version in "
                                                    + resetArtifact.getAvailableVersions()
                                                    + " to match the range " + resetArtifact.getVersionRange(),
                                            resetArtifact);
                                }
                                fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listener,
                                        resetNodes[j]);
                            }
                        }
                    }

                    // Conflict Resolution
                    // TODO: use as conflict resolver(s), chain

                    // TODO: should this be part of mediation?
                    // previous one is more dominant
                    ResolutionNode nearest;
                    ResolutionNode farthest;
                    if (previous.getDepth() <= node.getDepth()) {
                        nearest = previous;
                        farthest = node;
                    } else {
                        nearest = node;
                        farthest = previous;
                    }

                    if (checkScopeUpdate(farthest, nearest)) {
                        // if we need to update scope of nearest to use
                        // farthest
                        // scope, use the nearest version, but farthest
                        // scope
                        nearest.disable();
                        farthest.getArtifact().setVersion(nearest.getArtifact().getVersion());
                        fireEvent(ResolutionListener.OMIT_FOR_NEARER, listener, nearest, farthest);
                    } else {
                        farthest.disable();
                        fireEvent(ResolutionListener.OMIT_FOR_NEARER, listener, farthest, nearest);
                    }
                }
            }
        } else {
            previousNodes = new ArrayList();
            resolvedArtifacts.put(key, previousNodes);
        }
        previousNodes.add(node);

        if (node.isActive()) {
            fireEvent(ResolutionListener.INCLUDE_ARTIFACT, listener, node);
        }

        // don't pull in the transitive deps of a system-scoped dependency.
        if (node.isActive() && !Artifact.SCOPE_SYSTEM.equals(node.getArtifact().getScope())) {
            fireEvent(ResolutionListener.PROCESS_CHILDREN, listener, node);
            if (transitive) {
                Artifact parentArtifact = node.getArtifact();
                for (Iterator i = node.getChildrenIterator(); i.hasNext();) {
                    ResolutionNode child = (ResolutionNode) i.next();
                    if (!filter.include(child.getArtifact())) {
                        continue;
                    }
                    /*
                     * rotgier: In case of regular dependencies provided
                     * scope is simply ignored (artifact versions specified
                     * there conflict with the ones of runtime deps)
                     */
                    if (Artifact.SCOPE_PROVIDED.equals(child.getArtifact().getScope())) {
                        continue;
                    }
                    changeArtifactCoreToOsgi(node, child, separatedGroupIds, listener);
                    boolean isContinue = resolveChildNode(node, child, filter, managedVersions, listener,
                            source, parentArtifact);
                    if (isContinue) {
                        continue;
                    }
                    List<String> extractedSeparatedGroupIds = extractSeparatedGroupIds(child.getArtifact(),
                            remoteRepositories);
                    Set<String> combinedSeparatedGroupIds = new HashSet<String>(separatedGroupIds);
                    combinedSeparatedGroupIds.addAll(extractedSeparatedGroupIds);
                    recurse(originatingArtifact, child, resolvedArtifacts, managedVersions, localRepository,
                            child.getRemoteRepositories(), source, filter, listener, true,
                            combinedSeparatedGroupIds);
                }
                List runtimeDeps = getRuntimeDeps(node.getArtifact(), managedVersions, remoteRepositories);
                Field childrenField = node.getClass().getDeclaredField("children");
                childrenField.setAccessible(true);
                List nodesChildren = (List) childrenField.get(node);
                /* nodesChildren can be empty when dealing with parent POMs */
                if (nodesChildren == Collections.EMPTY_LIST) {
                    nodesChildren = new ArrayList();
                    childrenField.set(node, nodesChildren);
                }
                for (Object runtimeDepObj : runtimeDeps) {
                    DependencyNode runtimeDep = (DependencyNode) runtimeDepObj;
                    Artifact artifact = runtimeDep.getArtifact();
                    ResolutionNode childRuntime = new ResolutionNode(artifact, node.getRemoteRepositories(),
                            node);
                    /*
                     * rotgier: In case of runtime dependencies provided
                     * scope should be allowed
                     */
                    if (!filter.include(childRuntime.getArtifact())) {

                        if (!Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) {
                            continue;
                        }
                    }
                    changeArtifactCoreToOsgi(node, childRuntime, separatedGroupIds, listener);
                    boolean isContinue = resolveChildNode(node, childRuntime, filter, managedVersions, listener,
                            source, parentArtifact);
                    if (isContinue) {
                        continue;
                    }
                    List<String> extractedSeparatedGroupIds = extractSeparatedGroupIds(
                            childRuntime.getArtifact(), remoteRepositories);
                    Set<String> combinedSeparatedGroupIds = new HashSet<String>(separatedGroupIds);
                    combinedSeparatedGroupIds.addAll(extractedSeparatedGroupIds);
                    recurse(originatingArtifact, childRuntime, resolvedArtifacts, managedVersions,
                            localRepository, childRuntime.getRemoteRepositories(), source, filter, listener,
                            true, combinedSeparatedGroupIds);
                    nodesChildren.add(childRuntime);
                }
            }
            fireEvent(ResolutionListener.FINISH_PROCESSING_CHILDREN, listener, node);
        }
    } catch (Exception ex) {
        StringBuilder msg = new StringBuilder();
        msg.append(String.format("\nUnpredicted exception during dependency tree recursion at node %s",
                FilteringVisitorSupport.stringify(node.getArtifact())));
        msg.append("\nNode's parent tree:\n");
        msg.append(printNodeParentsTree(node));
        throw new IllegalStateException(msg.toString(), ex);
    }
}