IVY-813: added a 'nearest' conflict manager

This commit is contained in:
Maarten Coene 2025-06-08 12:57:50 +02:00
parent 60a1ff1b11
commit ea70bbe7e3
No known key found for this signature in database
GPG Key ID: 5BE0BA8CB80602AE
6 changed files with 227 additions and 4 deletions

View File

@ -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

View File

@ -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.

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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>