This is an automated email from the ASF dual-hosted git repository.

maartenc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant-ivy.git


The following commit(s) were added to refs/heads/master by this push:
     new ea70bbe7 IVY-813: added a 'nearest' conflict manager
ea70bbe7 is described below

commit ea70bbe7e3c29394cab5fd3d3430a74abe6ac47b
Author: Maarten Coene <4728619+maart...@users.noreply.github.com>
AuthorDate: Sun Jun 8 12:57:50 2025 +0200

    IVY-813: added a 'nearest' conflict manager
---
 asciidoc/release-notes.adoc                        |  4 +-
 asciidoc/settings/conflict-managers.adoc           |  3 +
 .../org/apache/ivy/core/settings/IvySettings.java  |  8 +-
 .../plugins/conflict/NearestConflictManager.java   | 89 ++++++++++++++++++++
 .../conflict/NearestConflictManagerTest.java       | 96 ++++++++++++++++++++++
 .../ivy/plugins/conflict/ivysettings-nearest.xml   | 31 +++++++
 6 files changed, 227 insertions(+), 4 deletions(-)

diff --git a/asciidoc/release-notes.adoc b/asciidoc/release-notes.adoc
index 7f1d146d..e821260c 100644
--- a/asciidoc/release-notes.adoc
+++ b/asciidoc/release-notes.adoc
@@ -52,8 +52,9 @@ Note, if you have resolved dependencies with version of Ivy 
prior to 2.6.0, you
 - DOCUMENTATION: bla bla bla (jira:IVY-1234[]) (Thanks to Jane Doe)
 ////
 
-- FIX: improved Maven dependencyManagement matching for dependencies with a 
non-default type or classifier (IVY-1654) (Thanks to Mark Kittisopikul)
+- NEW: added a new `nearest` conflict manager, which handles conflicts in the 
same way that Maven does. (IVY-813) (Thanks to Eric Milles)
 - IMPROVEMENT: use Apache Commons Compress for pack200 handling to avoid 
issues on Java 14 and later. If pack200 is needed, make sure to add Apache 
Commons Compress to your classpath. (IVY-1652)
+- FIX: improved Maven dependencyManagement matching for dependencies with a 
non-default type or classifier (IVY-1654) (Thanks to Mark Kittisopikul)
 
 == Committers and Contributors
 
@@ -166,6 +167,7 @@ Here is the list of people who have contributed source code 
and documentation up
 * Glen Marchesani
 * Phil Messenger
 * Steve Miller
+* Eric Milles
 * Mathias Muller
 * Randy Nott
 * Peter Oxenham
diff --git a/asciidoc/settings/conflict-managers.adoc 
b/asciidoc/settings/conflict-managers.adoc
index 000e5785..aa75fe61 100644
--- a/asciidoc/settings/conflict-managers.adoc
+++ b/asciidoc/settings/conflict-managers.adoc
@@ -42,6 +42,9 @@ Here is a list of built-in conflict managers (which do not 
require anything in t
     * strict +
      this conflict manager throws an exception (i.e. causes a build failure) 
whenever a conflict is found.
 
+    * nearest (*__since 2.6.0__*) +
+     this conflict manager selects the nearest version in the dependency 
graph, i.e. the one which is closest to the root of the dependency graph. This 
is compatible with the Maven conflict handling.
+
 The two "latest" conflict managers also take into account the force attribute 
of the dependencies.
 In fact, direct dependencies can declare a force attribute (see 
link:../ivyfile/dependency{outfilesuffix}[dependency]), which indicates that 
the revision given in the direct dependency should be preferred over indirect 
dependencies.
 
diff --git a/src/java/org/apache/ivy/core/settings/IvySettings.java 
b/src/java/org/apache/ivy/core/settings/IvySettings.java
index 89591886..04e62bf1 100644
--- a/src/java/org/apache/ivy/core/settings/IvySettings.java
+++ b/src/java/org/apache/ivy/core/settings/IvySettings.java
@@ -33,10 +33,7 @@ import org.apache.ivy.core.module.id.ModuleId;
 import org.apache.ivy.core.module.id.ModuleRevisionId;
 import org.apache.ivy.core.module.id.ModuleRules;
 import org.apache.ivy.core.module.status.StatusManager;
-import org.apache.ivy.plugins.pack.ArchivePacking;
 import org.apache.ivy.core.pack.PackingRegistry;
-import org.apache.ivy.plugins.pack.OsgiBundlePacking;
-import org.apache.ivy.plugins.pack.ZipPacking;
 import org.apache.ivy.core.publish.PublishEngineSettings;
 import org.apache.ivy.core.repository.RepositoryManagementEngineSettings;
 import org.apache.ivy.core.resolve.ResolveEngineSettings;
@@ -52,6 +49,7 @@ import 
org.apache.ivy.plugins.circular.WarnCircularDependencyStrategy;
 import org.apache.ivy.plugins.conflict.ConflictManager;
 import org.apache.ivy.plugins.conflict.LatestCompatibleConflictManager;
 import org.apache.ivy.plugins.conflict.LatestConflictManager;
+import org.apache.ivy.plugins.conflict.NearestConflictManager;
 import org.apache.ivy.plugins.conflict.NoConflictManager;
 import org.apache.ivy.plugins.conflict.StrictConflictManager;
 import org.apache.ivy.plugins.latest.LatestLexicographicStrategy;
@@ -69,6 +67,9 @@ import org.apache.ivy.plugins.matcher.MapMatcher;
 import org.apache.ivy.plugins.matcher.PatternMatcher;
 import org.apache.ivy.plugins.matcher.RegexpPatternMatcher;
 import org.apache.ivy.plugins.namespace.Namespace;
+import org.apache.ivy.plugins.pack.ArchivePacking;
+import org.apache.ivy.plugins.pack.OsgiBundlePacking;
+import org.apache.ivy.plugins.pack.ZipPacking;
 import org.apache.ivy.plugins.parser.ModuleDescriptorParser;
 import org.apache.ivy.plugins.parser.ModuleDescriptorParserRegistry;
 import org.apache.ivy.plugins.parser.ParserSettings;
@@ -269,6 +270,7 @@ public class IvySettings implements SortEngineSettings, 
PublishEngineSettings, P
                 latestTimeStrategy));
         addConflictManager("all", new NoConflictManager());
         addConflictManager("strict", new StrictConflictManager());
+        addConflictManager("nearest", new NearestConflictManager());
 
         addMatcher(ExactPatternMatcher.INSTANCE);
         addMatcher(RegexpPatternMatcher.INSTANCE);
diff --git 
a/src/java/org/apache/ivy/plugins/conflict/NearestConflictManager.java 
b/src/java/org/apache/ivy/plugins/conflict/NearestConflictManager.java
new file mode 100644
index 00000000..798d4752
--- /dev/null
+++ b/src/java/org/apache/ivy/plugins/conflict/NearestConflictManager.java
@@ -0,0 +1,89 @@
+/*
+ *  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 org.apache.ivy.plugins.conflict;
+
+import java.util.*;
+
+import org.apache.ivy.core.resolve.IvyNode;
+import org.apache.ivy.core.resolve.IvyNodeCallers;
+import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
+
+public class NearestConflictManager extends AbstractConflictManager {
+
+    @Override
+    public Collection<IvyNode> resolveConflicts(IvyNode parent, 
Collection<IvyNode> conflicts) {
+        if (conflicts.size() < 2) {
+            return conflicts;
+        }
+
+        // list containing the direct children of the parent
+        List<IvyNode> children = new ArrayList<>(conflicts.size());
+
+        for (IvyNode node : conflicts) {
+            DependencyDescriptor dd = node.getDependencyDescriptor(parent);
+            if (dd != null && 
parent.getResolvedId().equals(dd.getParentRevisionId())) {
+                if (dd.isForce()) {
+                    return Collections.singleton(node);
+                }
+                children.add(node);
+            }
+        }
+
+        if (!children.isEmpty()) {
+            return children;
+        }
+
+        int distance = 0;
+        IvyNode node = null;
+
+        for (IvyNode child : conflicts) {
+            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 = Short.MAX_VALUE;
+
+        for (IvyNodeCallers.Caller caller : child.getCallers(conf)) {
+            if (caller.getModuleRevisionId().equals(parent.getResolvedId())) {
+                return 0;
+            }
+
+            int distance = 1 + calculateDistance(parent, conf, 
child.findNode(caller.getModuleRevisionId()));
+            if (distance < min) {
+                min = distance;
+            }
+        }
+
+        return min;
+    }
+}
\ No newline at end of file
diff --git 
a/test/java/org/apache/ivy/plugins/conflict/NearestConflictManagerTest.java 
b/test/java/org/apache/ivy/plugins/conflict/NearestConflictManagerTest.java
new file mode 100644
index 00000000..97481f2f
--- /dev/null
+++ b/test/java/org/apache/ivy/plugins/conflict/NearestConflictManagerTest.java
@@ -0,0 +1,96 @@
+/*
+ *  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 org.apache.ivy.plugins.conflict;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.text.ParseException;
+
+import org.apache.ivy.Ivy;
+import org.apache.ivy.TestFixture;
+import org.apache.ivy.TestHelper;
+import org.apache.ivy.core.report.ConfigurationResolveReport;
+import org.apache.ivy.core.report.ResolveReport;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class NearestConflictManagerTest {
+  private TestFixture fixture;
+
+  @Before
+  public void setUp() {
+    fixture = new TestFixture();
+    ConflictManager cm = fixture.getSettings().getConflictManager("nearest");
+    fixture.getSettings().setDefaultConflictManager(cm);
+  }
+
+  @After
+  public void tearDown() {
+    fixture.clean();
+  }
+
+  @Test
+  public void testInitFromSettings() throws Exception {
+    Ivy ivy = new Ivy();
+    
ivy.configure(NearestConflictManagerTest.class.getResource("ivysettings-nearest.xml"));
+    ConflictManager cm = ivy.getSettings().getDefaultConflictManager();
+    assertTrue(cm instanceof NearestConflictManager);
+  }
+
+  @Test
+  public void testNearestResolve1() throws Exception {
+    fixture.addMD("#A;1-> { #B;1.4 #C;1.0 }").addMD("#B;1.4->#D;1.5")
+           .addMD("#C;1.0->#D;1.6").addMD("#D;1.5").addMD("#D;1.6").init();
+    resolveAndAssert("#A;1", "#B;1.4, #C;1.0, #D;1.5");
+  }
+
+  @Test
+  public void testNearestResolve2() throws Exception {
+    fixture.addMD("#A;1-> { #B;1.4 #C;1.0 }").addMD("#B;1.4->#D;1.6")
+           .addMD("#C;1.0->#D;1.5").addMD("#D;1.5").addMD("#D;1.6").init();
+    resolveAndAssert("#A;1", "#B;1.4, #C;1.0, #D;1.6");
+  }
+
+  @Test
+  public void testNearestResolve3() throws Exception {
+    fixture.addMD("#A;1-> { #B;1.4 #C;1.0 
}").addMD("#B;1.4->#E;1.0").addMD("#E;1.0->#D;1.5")
+           .addMD("#C;1.0->#D;1.6").addMD("#D;1.5").addMD("#D;1.6").init();
+    resolveAndAssert("#A;1", "#B;1.4, #C;1.0, #D;1.6, #E;1.0");
+  }
+
+  @Test
+  public void testNearestResolve4() throws Exception {
+    fixture.addMD("#A;1-> { #B;1.4 #C;1.0 #D;1.5 
}").addMD("#B;1.4->#E;1.0").addMD("#E;1.0->#D;1.5")
+           .addMD("#C;1.0->#D;1.6").addMD("#D;1.5").addMD("#D;1.6").init();
+    resolveAndAssert("#A;1", "#B;1.4, #C;1.0, #D;1.5, #E;1.0");
+  }
+
+  private void resolveAndAssert(String mrid, String expectedModuleSet) throws 
ParseException,
+                                                                              
IOException {
+    ResolveReport report = fixture.resolve(mrid);
+    assertFalse(report.hasError());
+    ConfigurationResolveReport defaultReport = 
report.getConfigurationReport("default");
+    TestHelper.assertModuleRevisionIds(expectedModuleSet, 
defaultReport.getModuleRevisionIds());
+  }
+
+}
+
+
diff --git a/test/java/org/apache/ivy/plugins/conflict/ivysettings-nearest.xml 
b/test/java/org/apache/ivy/plugins/conflict/ivysettings-nearest.xml
new file mode 100644
index 00000000..48cb03f0
--- /dev/null
+++ b/test/java/org/apache/ivy/plugins/conflict/ivysettings-nearest.xml
@@ -0,0 +1,31 @@
+<!--
+   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.    
+-->
+<ivysettings>
+
+    <settings defaultResolver="test" defaultConflictManager="nearest" />
+    <caches defaultCacheDir="${ivy.basedir}/build/cache" />
+
+    <resolvers>
+        <filesystem name="test">
+            <ivy 
pattern="${ivy.basedir}/test/repositories/latest-compatible/[module]/[revision]/[artifact].[ext]"/>
+            <artifact
+                    
pattern="${ivy.basedir}/test/repositories/latest-compatible/[module]/[revision]/[artifact].[ext]"/>
+        </filesystem>
+    </resolvers>
+</ivysettings>

Reply via email to