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..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,13 @@ 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; import java.nio.file.Files; @@ -131,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); } @@ -301,6 +309,51 @@ private List findJunitPlatformNativeJars(Set modulesAlreadyOnClass .collect(Collectors.toList()); } + 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 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 { private final String groupId; private final String artifactId;