Skip to content

Commit

Permalink
[MRESOLVER-345] Conflict resolution in verbose mode is sensitive to v…
Browse files Browse the repository at this point in the history
…ersion ordering (apache#271)

Our two collector implementations produce slightly different dirty trees when in transitive dependencies ranges are in use. The new BF produces (w/ and w/o skipper) produces this dirty tree:
```
menforcer426:aid:ext:1 compile
+- menforcer426:bid:ext:1 compile
|  +- menforcer426:cid:ext:3 compile
|  +- menforcer426:cid:ext:2 compile
|  \- menforcer426:cid:ext:1 compile
+- menforcer426:cid:ext:3 compile
+- menforcer426:cid:ext:2 compile
\- menforcer426:cid:ext:1 compile
```
The "old" DF produces this one:
```
menforcer426:aid:ext:1 compile
+- menforcer426:bid:ext:1 compile
|  +- menforcer426:cid:ext:1 compile
|  +- menforcer426:cid:ext:2 compile
|  \- menforcer426:cid:ext:3 compile
+- menforcer426:cid:ext:1 compile
+- menforcer426:cid:ext:2 compile
\- menforcer426:cid:ext:3 compile
```

Spot the difference: the two dirty trees are "semantically" (or content-wise?) equal/same, except for the artifact ordering, where there was a version range (collector in this case discovers available versions that "fits" range and created nodes for them, one for each version that lies within version constraint). DF collector relies and provides "metadata based" ordering (as metadata contained the versions), while BF explicitly sorts them in descending order (for internal optimization purposes). Point is, both dirty trees are ok.

But, Conflict resolver in verbose mode for two inputs above produces different outputs. For DF with "divergence found",  while for BF "no divergence found" (correctly).

Overall changes in this PR:
* most are test and test related resources
* cosmetic changes like javadoc typos, adding override etc
* added reusable `DependencyGraphDumper`
* key changes are in `ConflictResolver` covered by `ConflictResolverTest` UT changes
* overall fix is to make `ConflictResolver` insensitive to input dirty tree version ordering, make sure output is same for "semantically" (or content-wise?) same inputs.

How tested this:
* built/installed this PR
* built maven-3.9.x with this resolver
* ran maven IT suite -- OK
* ran apache/maven-enforcer#259 w/ built maven (so fixed resolver is used). This PR contains only one reproducer IT that fails with any released Maven version -- OK w/ maven built as above.

Just realized how enforcer ITs are good source of inspiration for resolver use cases, so many if not all of new tests are actually inspired by enforcer ITs.

---

https://issues.apache.org/jira/browse/MRESOLVER-345
  • Loading branch information
cstamas authored Mar 24, 2023
1 parent f7657ce commit 267d288
Show file tree
Hide file tree
Showing 32 changed files with 780 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void main(String[] args) throws Exception {
GetDirectDependencies.main(args);
GetDependencyTree.main(args);
GetDependencyHierarchy.main(args);
DependencyHierarchyWithRanges.main(args);
ResolveArtifact.main(args);
ResolveTransitiveDependencies.main(args);
ReverseDependencyTree.main(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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
*
* http://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.maven.resolver.examples;

import java.io.File;
import java.util.Collections;

import org.apache.maven.resolver.examples.util.Booter;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;

/**
* Visualizes the transitive dependencies of an artifact similar to m2e's dependency hierarchy view. Artifact in this
* test is not "plain" one as is original "demo" {@link GetDependencyHierarchy}, but specially crafted for case
* described in MRESOLVER-345.
*
* @see <a href="https://issues.apache.org/jira/browse/MRESOLVER-345">MRESOLVER-345</a>
*/
public class DependencyHierarchyWithRanges {

/**
* Main.
*/
public static void main(String[] args) throws Exception {
System.out.println("------------------------------------------------------------");
System.out.println(DependencyHierarchyWithRanges.class.getSimpleName());

RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));

DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(system);

session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);

// this artifact is in "remote" repository in src/main/remote-repository
Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");

File remoteRepoBasedir = new File("src/main/remote-repository");

ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
descriptorRequest.setArtifact(artifact);
descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
"remote", "default", remoteRepoBasedir.toURI().toASCIIString())
.build()));
ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);

CollectRequest collectRequest = new CollectRequest();
collectRequest.setRootArtifact(descriptorResult.getArtifact());
collectRequest.setDependencies(descriptorResult.getDependencies());
collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
collectRequest.setRepositories(descriptorRequest.getRepositories());

CollectResult collectResult = system.collectDependencies(session, collectRequest);

collectResult.getRoot().accept(Booter.DUMPER_SOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.maven.resolver.examples;

import org.apache.maven.resolver.examples.util.Booter;
import org.apache.maven.resolver.examples.util.ConsoleDependencyGraphDumper;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
Expand Down Expand Up @@ -67,6 +66,6 @@ public static void main(String[] args) throws Exception {

CollectResult collectResult = system.collectDependencies(session, collectRequest);

collectResult.getRoot().accept(new ConsoleDependencyGraphDumper());
collectResult.getRoot().accept(Booter.DUMPER_SOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.maven.resolver.examples;

import org.apache.maven.resolver.examples.util.Booter;
import org.apache.maven.resolver.examples.util.ConsoleDependencyGraphDumper;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
Expand Down Expand Up @@ -54,6 +53,6 @@ public static void main(String[] args) throws Exception {

CollectResult collectResult = system.collectDependencies(session, collectRequest);

collectResult.getRoot().accept(new ConsoleDependencyGraphDumper());
collectResult.getRoot().accept(Booter.DUMPER_SOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.apache.maven.resolver.examples.util.Booter;
import org.apache.maven.resolver.examples.util.ConsoleDependencyGraphDumper;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
import org.eclipse.aether.util.repository.AuthenticationBuilder;

Expand Down Expand Up @@ -121,8 +123,13 @@ public void deploy(Artifact artifact, Artifact pom, String remoteRepository) thr
}

private void displayTree(DependencyNode node, StringBuilder sb) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
node.accept(new ConsoleDependencyGraphDumper(new PrintStream(os)));
sb.append(os.toString());
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name());
node.accept(new DependencyGraphDumper(ps::println));
sb.append(os);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;

/**
* A helper to boot the repository system and a repository system session.
Expand All @@ -39,6 +40,8 @@ public class Booter {

public static final String SISU = "sisu";

public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);

public static String selectFactory(String[] args) {
if (args == null || args.length == 0) {
return SERVICE_LOCATOR;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>a</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>b</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<version>[1.0, 3.0]</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>b</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<version>[1.0, 3.0]</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<version>1.0</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<version>2.0</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<version>3.0</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
http://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.
-->
<metadata>
<groupId>org.apache.maven.resolver.demo.mresolver345</groupId>
<artifactId>c</artifactId>
<versioning>
<latest>3.0</latest>
<release>3.0</release>
<versions>
<version>1.0</version>
<version>2.0</version>
<version>3.0</version>
</versions>
<lastUpdated>20230320074804</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,23 @@ public void testDependencyManagement_DefaultDependencyManager() throws Dependenc
assertEqualSubtree(expectedTree, toDependencyResult(result.getRoot(), "compile", null));
}

@Test
public void testTransitiveDepsUseRangesDirtyTree() throws DependencyCollectionException, IOException {
// Note: DF depends on version order (ultimately the order of versions as returned by VersionRangeResolver
// that in case of Maven, means order as in maven-metadata.xml
// BF on the other hand explicitly sorts versions from range in descending order
//
// Hence, the "dirty tree" of two will not match.
DependencyNode root = parser.parseResource(getTransitiveDepsUseRangesDirtyTreeResource());
Dependency dependency = root.getDependency();
CollectRequest request = new CollectRequest(dependency, singletonList(repository));

CollectResult result = collector.collectDependencies(session, request);
assertEqualSubtree(root, result.getRoot());
}

protected abstract String getTransitiveDepsUseRangesDirtyTreeResource();

private DependencyNode toDependencyResult(
final DependencyNode root, final String rootScope, final Boolean optional) {
// Make the root artifact resultion result a dependency resolution result for the subtree check.
Expand Down
Loading

0 comments on commit 267d288

Please sign in to comment.