Skip to content

Commit 095a84d

Browse files
committed
Use dependencies pom files to get gav coordinates required for filtering
1 parent 9d861ab commit 095a84d

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java

+48-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@
6161
import org.eclipse.aether.resolution.DependencyResolutionException;
6262
import org.eclipse.aether.resolution.DependencyResult;
6363
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
64+
import org.w3c.dom.Document;
65+
import org.xml.sax.SAXException;
6466

67+
import javax.xml.parsers.DocumentBuilder;
68+
import javax.xml.parsers.DocumentBuilderFactory;
69+
import javax.xml.parsers.ParserConfigurationException;
6570
import java.io.File;
6671
import java.io.IOException;
6772
import java.io.UncheckedIOException;
@@ -132,6 +137,8 @@ protected void addDependenciesToClasspath() throws MojoExecutionException {
132137
.filter(it -> it.getGroupId().startsWith(NativeImageConfigurationUtils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit"))
133138
.map(it -> it.getFile().toPath())
134139
.forEach(imageClasspath::add);
140+
141+
modules.addAll(collectJUnitModulesAlreadyOnClasspath());
135142
var jars = findJunitPlatformNativeJars(modules);
136143
imageClasspath.addAll(jars);
137144
}
@@ -298,17 +305,53 @@ private List<Path> findJunitPlatformNativeJars(Set<Module> modulesAlreadyOnClass
298305
.stream()
299306
.map(ArtifactResult::getArtifact)
300307
.filter(a -> !modulesAlreadyOnClasspath.contains(new Module(a.getGroupId(), a.getArtifactId())))
301-
.filter(a -> imageClasspath.stream().noneMatch(entry -> matchGroup(entry, a.getGroupId()) && matchArtifact(entry, a.getArtifactId())))
302308
.map(a -> a.getFile().toPath())
303309
.collect(Collectors.toList());
304310
}
305311

306-
private boolean matchGroup(Path entry, String groupId) {
307-
return entry.toString().contains(groupId.replace(".", File.separator));
312+
private Set<Module> collectJUnitModulesAlreadyOnClasspath() {
313+
Set<Module> artifacts = new HashSet<>();
314+
for (Path entry : imageClasspath) {
315+
if (isJUnitArtifact(entry)) {
316+
File pom = getArtifactPOM(entry);
317+
if (pom != null) {
318+
artifacts.add(getModuleFromPOM(pom));
319+
}
320+
}
321+
}
322+
323+
return artifacts;
324+
}
325+
326+
private boolean isJUnitArtifact(Path entry) {
327+
return entry.toString().contains("junit");
308328
}
309329

310-
private boolean matchArtifact(Path entry, String artifactId) {
311-
return entry.toString().contains(artifactId);
330+
private File getArtifactPOM(Path classpathEntry) {
331+
List<File> artifactContent = getArtifactContent(classpathEntry.getParent());
332+
List<File> candidates = artifactContent.stream().filter(f -> f.getName().endsWith(".pom")).collect(Collectors.toList());
333+
return candidates.size() != 1 ? null : candidates.get(0);
334+
}
335+
336+
private List<File> getArtifactContent(Path path) {
337+
File[] content = path.toFile().listFiles();
338+
return content == null ? List.of() : List.of(content);
339+
}
340+
341+
private Module getModuleFromPOM(File pom) {
342+
try {
343+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
344+
factory.setIgnoringElementContentWhitespace(true);
345+
DocumentBuilder builder = factory.newDocumentBuilder();
346+
Document doc = builder.parse(pom);
347+
348+
String groupId = doc.getElementsByTagName("groupId").item(0).getFirstChild().getTextContent();
349+
String artifactId = doc.getElementsByTagName("artifactId").item(0).getFirstChild().getTextContent();
350+
351+
return new Module(groupId, artifactId);
352+
} catch (ParserConfigurationException | IOException | SAXException e) {
353+
throw new RuntimeException("Cannot get maven coordinates from " + pom.getPath() + ". Reason: " + e.getMessage());
354+
}
312355
}
313356

314357
private static final class Module {

0 commit comments

Comments
 (0)