[ 
https://issues.apache.org/jira/browse/IVY-813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17941671#comment-17941671
 ] 

Eric Milles commented on IVY-813:
---------------------------------

You can add this to your config without too much effort:
{code:xml}
<ivysettings>
    <!-- ... -->
    <classpath file="${ivy.settings.dir}/conflict-managers.jar" />
    <typedef name="nearest-cm" 
classname="ant.ivy.plugins.conflict.NearestConflictManager" />
    <!-- ... -->
    <conflict-managers><nearest-cm/></conflict-managers>
    <settings defaultConflictManager="nearest" />
</ivysettings>
{code}

I am an ASF member and contributor, so this can be taken into Ivy if you wish.  
I tried to stick to Java 7 APIs since that looks to be the target as of Ivy 
2.5.3.

{code:java}
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package ant.ivy.plugins.conflict;

import java.util.*;

import org.apache.ivy.core.resolve.IvyNode;
import org.apache.ivy.core.resolve.IvyNodeCallers;
import org.apache.ivy.plugins.conflict.AbstractConflictManager;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;

/**
 * @see https://issues.apache.org/jira/browse/IVY-813
 */
public class NearestConflictManager extends AbstractConflictManager
{
    public NearestConflictManager()
    {
        setName("nearest");
    }

    @Override
    public Collection<IvyNode> resolveConflicts(IvyNode parent, 
Collection<IvyNode> children)
    {
        if (children.size() < 2) return children;

        trace("clash: " + children);

        //

        List<IvyNode> zero = new ArrayList<>(children.size());

        for (IvyNode child : children)
        {
            DependencyDescriptor dd = child.getDependencyDescriptor(parent);
            if (dd != null && 
dd.getParentRevisionId().equals(parent.getResolvedId()))
            {
                zero.add(child);

                if (dd.isForce())
                {
                    trace("force " + child + " for " + 
dd.getParentRevisionId());

                    return Collections.singletonList(child);
                }
            }
        }

        if (!zero.isEmpty()) return zero; // explicit dependency element(s)

        //

        int distance = 0;
        IvyNode node = null;

        for (IvyNode child : children)
        {
            if (node == null)
            {
                node = child;
                // the first child has path through VisitNode
                distance = 
node.getData().getCurrentVisitNode().getPath().size() - 2;
            }
            else
            {
                for (String conf : parent.getRequiredConfigurations())
                {
                    int hops = calculateDistance(parent, conf, child);
                    if (hops <= distance)
                    {
                        distance = hops; node = child; // nearer or sooner
                    }
                }
            }
        }

        return Collections.singletonList(node);
    }

    private int calculateDistance(IvyNode parent, String conf, IvyNode child)
    {
        int min = Integer.MAX_VALUE;

        for (IvyNodeCallers.Caller caller : child.getCallers(conf))
        {
            if (caller.getModuleRevisionId().equals(parent.getResolvedId()))
            {
                return 0;
            }
            else
            {
                int d = 1 + calculateDistance(parent, conf, 
child.findNode(caller.getModuleRevisionId()));

                if (d < min) min = d;
            }
        }

        return min;
    }

    private static void trace(Object object)
    {
        org.apache.ivy.util.Message.debug("\t" + object);
    }
}
{code}

> Add a nearest conflict manager
> ------------------------------
>
>                 Key: IVY-813
>                 URL: https://issues.apache.org/jira/browse/IVY-813
>             Project: Ivy
>          Issue Type: Improvement
>          Components: Maven Compatibility
>            Reporter: Scokart Gilles
>            Priority: Major
>
> The maven conflict managment use the nearest strategy (always get closest 
> transitive dep regardless of version specifications)
> To be compatible with maven repository, ivy must have the possibility to use 
> this strategy also.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to