From 9d861abfa62e7f7600a7845e3ca6a64d49f05359 Mon Sep 17 00:00:00 2001 From: David Nestorovic Date: Wed, 2 Apr 2025 16:29:23 +0200 Subject: [PATCH 1/2] Avoid adding duplicated junit entries on classpath --- .../org/graalvm/buildtools/maven/NativeTestMojo.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java index a59009a57..8e0302874 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java @@ -62,6 +62,7 @@ import org.eclipse.aether.resolution.DependencyResult; import org.graalvm.buildtools.utils.NativeImageConfigurationUtils; +import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; @@ -297,10 +298,19 @@ private List findJunitPlatformNativeJars(Set modulesAlreadyOnClass .stream() .map(ArtifactResult::getArtifact) .filter(a -> !modulesAlreadyOnClasspath.contains(new Module(a.getGroupId(), a.getArtifactId()))) + .filter(a -> imageClasspath.stream().noneMatch(entry -> matchGroup(entry, a.getGroupId()) && matchArtifact(entry, a.getArtifactId()))) .map(a -> a.getFile().toPath()) .collect(Collectors.toList()); } + private boolean matchGroup(Path entry, String groupId) { + return entry.toString().contains(groupId.replace(".", File.separator)); + } + + private boolean matchArtifact(Path entry, String artifactId) { + return entry.toString().contains(artifactId); + } + private static final class Module { private final String groupId; private final String artifactId; From 095a84d1a885aca3817a10454ffd6389fed11264 Mon Sep 17 00:00:00 2001 From: David Nestorovic Date: Wed, 9 Apr 2025 18:03:51 +0200 Subject: [PATCH 2/2] Use dependencies pom files to get gav coordinates required for filtering --- .../buildtools/maven/NativeTestMojo.java | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java index 8e0302874..8d18f6585 100644 --- a/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java +++ b/native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java @@ -61,7 +61,12 @@ import org.eclipse.aether.resolution.DependencyResolutionException; import org.eclipse.aether.resolution.DependencyResult; import org.graalvm.buildtools.utils.NativeImageConfigurationUtils; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; @@ -132,6 +137,8 @@ protected void addDependenciesToClasspath() throws MojoExecutionException { .filter(it -> it.getGroupId().startsWith(NativeImageConfigurationUtils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit")) .map(it -> it.getFile().toPath()) .forEach(imageClasspath::add); + + modules.addAll(collectJUnitModulesAlreadyOnClasspath()); var jars = findJunitPlatformNativeJars(modules); imageClasspath.addAll(jars); } @@ -298,17 +305,53 @@ private List findJunitPlatformNativeJars(Set modulesAlreadyOnClass .stream() .map(ArtifactResult::getArtifact) .filter(a -> !modulesAlreadyOnClasspath.contains(new Module(a.getGroupId(), a.getArtifactId()))) - .filter(a -> imageClasspath.stream().noneMatch(entry -> matchGroup(entry, a.getGroupId()) && matchArtifact(entry, a.getArtifactId()))) .map(a -> a.getFile().toPath()) .collect(Collectors.toList()); } - private boolean matchGroup(Path entry, String groupId) { - return entry.toString().contains(groupId.replace(".", File.separator)); + private Set collectJUnitModulesAlreadyOnClasspath() { + Set artifacts = new HashSet<>(); + for (Path entry : imageClasspath) { + if (isJUnitArtifact(entry)) { + File pom = getArtifactPOM(entry); + if (pom != null) { + artifacts.add(getModuleFromPOM(pom)); + } + } + } + + return artifacts; + } + + private boolean isJUnitArtifact(Path entry) { + return entry.toString().contains("junit"); } - private boolean matchArtifact(Path entry, String artifactId) { - return entry.toString().contains(artifactId); + private File getArtifactPOM(Path classpathEntry) { + List artifactContent = getArtifactContent(classpathEntry.getParent()); + List candidates = artifactContent.stream().filter(f -> f.getName().endsWith(".pom")).collect(Collectors.toList()); + return candidates.size() != 1 ? null : candidates.get(0); + } + + private List getArtifactContent(Path path) { + File[] content = path.toFile().listFiles(); + return content == null ? List.of() : List.of(content); + } + + private Module getModuleFromPOM(File pom) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setIgnoringElementContentWhitespace(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(pom); + + String groupId = doc.getElementsByTagName("groupId").item(0).getFirstChild().getTextContent(); + String artifactId = doc.getElementsByTagName("artifactId").item(0).getFirstChild().getTextContent(); + + return new Module(groupId, artifactId); + } catch (ParserConfigurationException | IOException | SAXException e) { + throw new RuntimeException("Cannot get maven coordinates from " + pom.getPath() + ". Reason: " + e.getMessage()); + } } private static final class Module {