Skip to content

[#7501] Dependency order should be nearest first - #281

Open
belingueres wants to merge 1 commit into
apache:masterfrom
belingueres:MNG-6357
Open

belingueres wants to merge 1 commit into
apache:masterfrom
belingueres:MNG-6357

Conversation

@belingueres

Copy link
Copy Markdown
Contributor

Save the artifacts temporarily in a Map ordered by tree depth, and only
after finishing iterating over the dependency tree are the artifacts
added to the collection in depth order.

Note: IT-core test needs to be added replacing IT 3813 which does not apply any longer for a maven version containing this change.

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MNG-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MNG-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • [] You have run the Core IT successfully.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@belingueres

Copy link
Copy Markdown
Contributor Author

Please review this patch. Tested it and seems to solve:

@rfscholte

Copy link
Copy Markdown
Contributor

Looks good at first sight. This code deserves a unittest, that's better than relying on plugins. I don't think the unittest is too hard to write.
I noticed you're not using diamond operators. Looking at the code it should be written with the new Map methods, but that requires Java 8. The moment that Maven will require Java 8 is getting closer

@michael-o

Copy link
Copy Markdown
Member

@rfscholte We have more serious problems that introducing fancy Java 8 features.

DependencyFilter filter )
{
Map<Integer, List<org.apache.maven.artifact.Artifact>> artifactsByDepth =
new TreeMap<Integer, List<org.apache.maven.artifact.Artifact>>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use diamond <>. It is Java 1.7 feature.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i open IDEA, it shows me these issues related to Java version.

Map<Integer, List<org.apache.maven.artifact.Artifact>> artifactsByDepth =
new TreeMap<Integer, List<org.apache.maven.artifact.Artifact>>();

ArrayList<org.apache.maven.artifact.Artifact> firstLevelArtifacts =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaa, Collection or List better than ArrayList.

new TreeMap<Integer, List<org.apache.maven.artifact.Artifact>>();

ArrayList<org.apache.maven.artifact.Artifact> firstLevelArtifacts =
new ArrayList<org.apache.maven.artifact.Artifact>( nodes.size() );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

List<org.apache.maven.artifact.Artifact> artifactsCurrentDepth = artifactsByDepth.get( currentDepth );
if ( artifactsCurrentDepth == null )
{
artifactsCurrentDepth = new ArrayList<org.apache.maven.artifact.Artifact>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@jmhodges

Copy link
Copy Markdown
Contributor

The merge conflict is pretty straight-forward to fix (it's a simple collision in the pom.xml). Would love to see this go in! Happy to provide a new PR if that's preferable

@cowwoc

cowwoc commented Feb 18, 2023

Copy link
Copy Markdown

Per https://maven.apache.org/download.cgi Maven 4.x requires JDK 8. What is needed to move this PR forward?

@jira-importer

Copy link
Copy Markdown

Resolve #7501

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Code Review — PR #281

Verdict: ⚠️ Needs rebase

This PR changes RepositoryUtils.toArtifacts() to produce artifacts ordered by dependency tree depth (breadth-first / nearest-first) instead of depth-first. The approach using a TreeMap<Integer, List<Artifact>> keyed by depth is logically correct and addresses a legitimate issue (MNG-6357).

However, the PR is 4+ years stale and cannot merge.

Findings

# Severity File Details
1 🔴 High RepositoryUtils.java Stale path — File moved to impl/maven-core/... by MNG-8346. Merge state is CONFLICTING.
2 🔴 High RepositoryUtilsTest.java Test file conflict — A RepositoryUtilsTest.java already exists on master (added by commit 25c80d8ece). The PR's test class would need to be merged into the existing test file. Also, @author tags were removed from the codebase (commit 0b3246381d).

Behavioral concern

This change affects every caller of RepositoryUtils.toArtifacts() — including plugin classpath construction and project artifact resolution. While breadth-first ordering is arguably more correct, any code that implicitly depends on the current depth-first ordering could break. The PR description acknowledges that IT-3813 "does not apply any longer," suggesting awareness of this risk. A full integration test run is essential after rebasing.

Recommendation

If MNG-6357 is still desired: (1) rebase onto current master, (2) merge tests into the existing RepositoryUtilsTest.java, (3) remove @author tag, (4) run the full integration test suite to validate no regressions.


This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

gnodet added a commit to gnodet/maven that referenced this pull request Jul 10, 2026
Reviewed 3 PRs: #144 (COMMENT), apache#1125 (COMMENT), apache#281 (COMMENT).
36 total reviews posted. ~23 PRs remaining.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@belingueres

Copy link
Copy Markdown
Contributor Author

Let me know if this patch is still needed to do the recommended changes.

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR (MNG-6357, opened August 2019) changes dependency ordering to breadth-first (nearest-first). After 7 years, the codebase has evolved significantly, making this PR unmergeable in its current state:

  1. Target files relocated: All three changed files target maven-core/ which was relocated to impl/maven-core/ by MNG-8346. GitHub confirms the merge state is DIRTY/CONFLICTING.

  2. Extensive code drift: RepositoryUtils.java has been modified by 21+ commits since August 2019, including two full reformats (MNG-7601/MNG-7650), a Resolver 2.0 upgrade (MNG-7994), and a package move to org.apache.maven.impl (MNG-8508). The PR's diff is based on a 2019 snapshot that no longer applies.

  3. Missing required fallback: The original issue MNG-6357 explicitly states "there should be a system property to get the original order, just in case somebody needs it." This PR unconditionally replaces the ordering with no toggle or escape hatch.

Additionally, the recent comment from the author (July 2026) asks "Let me know if this patch is still needed." Given that MNG-6357 links to MNG-7852 ("is superseded by") and Maven 4.0 has undergone a major reorganization, the maintainers may want to clarify whether this issue is still relevant in Maven 4.x.

If the change is still desired, a fresh PR against impl/maven-core/ with current code style, the system-property fallback, and integration tests would be needed.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of gnodet

@belingueres

Copy link
Copy Markdown
Contributor Author

Hi @gnodet, I rebased the code.
Added code to configure which algorithm to use (setArtifactOrdering).
In addition to TreeMap based BFS originally proposed, I added a more memory efficient BFS2 alternative to choose from using List of Lists and a stack to keep the dependency trail.
Added assertions to verify the dependency trail lists.

@slachiewicz slachiewicz added the enhancement New feature or request label Aug 24, 2026
@slachiewicz slachiewicz added this to the 4.1.0 milestone Aug 28, 2026
@elharo
elharo self-requested a review August 28, 2026 12:41

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this actually doing?

I'm a hard no on anything that changes the dependency graphs Maven builds. That's a compatibility disaster. It is very likely the dependency resolution algorithm Maven chose decades ago was the wrong choice, but that choice is made and cannot be changed now.

try {
return parser.parseLiteral(dependencyGraph);
} catch (IOException e) {
fail("Failed the parsing of the dependency node graph");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to parse the

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.internal.test.util.DependencyGraphParser;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't depend on internal class from another package

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

*/
BFS(RepositoryUtils::toArtifactsBFS),
/**
* Breadth-first traversal of the dependency graph, using a List of List to store artifacts by depth, and a stack to keep the dependency trail. This is the new behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list of lists

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}

private static void toArtifactsBFS(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have several different BFS methods? what's different about them? Better names or comments or both are needed.

@belingueres belingueres Sep 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left only one now

}
}

public static void toArtifactsBFS2(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is public and the others are private?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed to private

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — PR #281 [MNG-6357]

Four code-level issues that would cause production failures or silent test coverage gaps, independent of the design-level compatibility concerns already raised by @elharo.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

}
}

private static ArtifactOrdering artifactOrdering = ArtifactOrdering.BFS;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Thread-safety: mutable global state without synchronization.

This is a non-volatile, non-synchronized mutable static field with a public setter. Maven's concurrent reactor (-T) runs dependency resolution on multiple threads simultaneously. Two problems:

  1. Visibility: Thread A calls setArtifactOrdering(DFS) — Thread B may never see the write (JMM allows caching in registers/L1).
  2. Mid-traversal corruption: If the ordering changes while toArtifactsDFS is recursing (it calls the public toArtifacts dispatcher — see next finding), the traversal switches strategy mid-tree.

At minimum, the field should be volatile. Better: make it an AtomicReference<ArtifactOrdering> or pass the strategy as a parameter rather than relying on global mutable state.

Suggested change
private static ArtifactOrdering artifactOrdering = ArtifactOrdering.BFS;
private static volatile ArtifactOrdering artifactOrdering = ArtifactOrdering.BFS;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid thread-safety issues altogether, perhaps we should avoid the static artifactOrdering variable, and let the client pass the ArtifactOrdering as a parameter to the toArtifact method, and leave (or deprecate?) the current toArtifact with the current DFS ordering hardcoded? WDYT?

*/
BFS(RepositoryUtils::toArtifactsBFS);

private ArtifactOrderingStrategy strategy;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Enum field should be final.

The strategy field is assigned only in the constructor but not declared final. Since enum constants are singletons, this field will never be reassigned, but omitting final is misleading and allows accidental mutation.

Suggested change
private ArtifactOrderingStrategy strategy;
private final ArtifactOrderingStrategy strategy;

Comment on lines +164 to 171
private static void toArtifactsDFS(
Collection<org.apache.maven.artifact.Artifact> artifacts,
Collection<? extends DependencyNode> nodes,
List<String> trail,
DependencyFilter filter) {
for (DependencyNode node : nodes) {
org.apache.maven.artifact.Artifact artifact = toArtifact(node.getDependency());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Bug: toArtifactsDFS recurses through the public dispatcher instead of calling itself.

The recursive call inside this method (at the end of the for loop, line 181 in the new file) is:

toArtifacts(artifacts, node.getChildren(), nodeTrail, filter);

This calls the public toArtifacts method which dispatches through artifactOrdering.getStrategy(). If the global ordering field is BFS, the DFS method recurses into BFS mid-traversal — producing a corrupted mix of orderings.

The original code was correct because toArtifacts WAS the recursive method. Now that it's a dispatcher, the DFS implementation must call itself:

toArtifactsDFS(artifacts, node.getChildren(), nodeTrail, filter);

assertNull(RepositoryUtils.toArtifact((org.apache.maven.artifact.Artifact) null));
}

public void testToArtifactsCollectionOrderedByNodeDepth(RepositoryUtils.ArtifactOrdering ordering) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Dead test: missing @Test or @ParameterizedTest — JUnit will never discover or execute this method.

This method:

  1. Has no @Test annotation
  2. Takes a parameter (ArtifactOrdering ordering) which JUnit 5 cannot inject without a @ParameterizedTest + @EnumSource
  3. Never uses the ordering parameter — it doesn't call setArtifactOrdering(ordering), so even if it ran, it would always test the default (BFS)
  4. Hardcodes BFS-order expectations, so it would fail if called with DFS

As written, this provides zero test coverage. It should be:

Suggested change
public void testToArtifactsCollectionOrderedByNodeDepth(RepositoryUtils.ArtifactOrdering ordering) {
@Test
void testToArtifactsCollectionOrderedByNodeDepth() {

And either:

  • Remove the ordering parameter and test only BFS, or
  • Use @ParameterizedTest @EnumSource(ArtifactOrdering.class) with setArtifactOrdering(ordering) at the start and separate expected values per ordering.

assertDependencyTrails(artifacts, expectedTrails);
}

public void testToArtifactsCollectionOrderedByNodeDepthWithFilter(RepositoryUtils.ArtifactOrdering ordering) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Same issue as above — this method is also never executed by JUnit. Missing @Test/@ParameterizedTest, unused ordering parameter, hardcoded BFS expectations.

Suggested change
public void testToArtifactsCollectionOrderedByNodeDepthWithFilter(RepositoryUtils.ArtifactOrdering ordering) {
@Test
void testToArtifactsCollectionOrderedByNodeDepthWithFilter() {

@gnodet gnodet changed the title [MNG-6357] Dependency order should be nearest first [#7501] Dependency order should be nearest first Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.