fix(java): order the IDE build's Maven classpath as Maven does (#7767)

The build model listed a dependency's own dependencies after all of its siblings instead of right after it, so of two artifacts with the same classes (dom4j:dom4j 1.6.1 and org.dom4j:dom4j 2.x) javac could load the one Maven does not. Walk the resolved graph in pre-order and rebuild openidea-java-build.jar.

Closes #7743
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-03 17:47:45 +08:00
1 джерело fc60719d4b
коміт 69c9521e85
3 files changed
+47 -2

No files matched your search

+41
Переглянути файл
@@ -416,6 +416,47 @@ fn builds_maven_modules_with_javac_and_only_what_changed() {
assert!(f.out("t.app/test-classes/a/AggTest.class").is_file());
}
/// #7743: a dependency's own dependencies come before its next sibling on the classpath, as in
/// `mvn compile`, so of two artifacts with the same classes (`dom4j:dom4j` 1.x and
/// `org.dom4j:dom4j` 2.x) javac sees the one Maven does.
#[test]
fn maven_classpath_lists_dependencies_in_maven_order() {
let module = |name: &str, deps: &[&str]| {
let deps: String = deps
.iter()
.map(|d| {
format!("<dependency><groupId>t</groupId><artifactId>{d}</artifactId><version>1</version></dependency>")
})
.collect();
format!(
"<project><modelVersion>4.0.0</modelVersion><parent><groupId>t</groupId><artifactId>root</artifactId>\
<version>1</version></parent><artifactId>{name}</artifactId><dependencies>{deps}</dependencies></project>"
)
};
let root = ROOT_POM.replace(
"<module>lib</module>",
"<module>newdom</module><module>olddom</module><module>x</module><module>lib</module>",
);
let (newdom, olddom, x, lib) =
(module("newdom", &[]), module("olddom", &[]), module("x", &["newdom"]), module("lib", &["x", "olddom"]));
let Some(f) = Fixture::new(&[
("pom.xml", &root),
("newdom/pom.xml", &newdom),
("olddom/pom.xml", &olddom),
("x/pom.xml", &x),
("lib/pom.xml", &lib),
("newdom/src/main/java/d/Element.java", "package d;\npublic class Element { public void fresh() {} }\n"),
("olddom/src/main/java/d/Element.java", "package d;\npublic class Element {}\n"),
("app/src/main/java/a/A.java", "package a;\nclass A { void f() { new d.Element().fresh(); } }\n"),
]) else {
return;
};
let built = f.launch(false);
assert_eq!(built.failure, None, "{:?}", built.messages);
let position = |name: &str| built.class_paths.iter().position(|p| *p == f.out(&format!("t.{name}/classes")));
assert!(position("newdom") < position("olddom"), "{:?}", built.class_paths);
}
#[test]
fn maps_resources_to_the_output_paths_the_build_tool_writes() {
let tmp = tempfile::tempdir().unwrap();
+6 -2
Переглянути файл
@@ -323,15 +323,19 @@ public final class BuildModel extends AbstractMavenLifecycleParticipant {
}
}
/** Pre-order, as Maven lists `project.getArtifacts()` for classpaths. */
/**
* Pre-order, as Maven lists `project.getArtifacts()` for classpaths: a dependency's own
* dependencies come before its next sibling, so jars with the same classes but different
* coordinates (`dom4j:dom4j` and `org.dom4j:dom4j`) resolve as they do in `mvn compile`.
*/
private static void collect(DependencyNode node, List<DependencyNode> out, Set<String> seen) {
for (DependencyNode child : node.getChildren()) {
Artifact a = child.getArtifact();
String key = a == null ? String.valueOf(System.identityHashCode(child))
: a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getExtension() + ":" + a.getClassifier();
if (seen.add(key)) out.add(child);
collect(child, out, seen);
}
for (DependencyNode child : node.getChildren()) collect(child, out, seen);
}
/**
Бінарний файл не відображається.