|
63 | 63 | import org.graalvm.buildtools.utils.FileUtils; |
64 | 64 | import org.graalvm.buildtools.utils.JUnitUtils; |
65 | 65 | import org.graalvm.buildtools.utils.NativeImageConfigurationUtils; |
| 66 | +import org.w3c.dom.Document; |
| 67 | +import org.xml.sax.SAXException; |
66 | 68 |
|
| 69 | +import javax.xml.parsers.DocumentBuilder; |
| 70 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 71 | +import javax.xml.parsers.ParserConfigurationException; |
| 72 | +import java.io.File; |
67 | 73 | import java.io.IOException; |
68 | 74 | import java.io.UncheckedIOException; |
69 | 75 | import java.nio.file.Files; |
@@ -135,6 +141,8 @@ protected void addDependenciesToClasspath() throws MojoExecutionException { |
135 | 141 | .filter(it -> it.getGroupId().startsWith(NativeImageConfigurationUtils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit")) |
136 | 142 | .map(it -> it.getFile().toPath()) |
137 | 143 | .forEach(imageClasspath::add); |
| 144 | + |
| 145 | + modules.addAll(collectJUnitModulesAlreadyOnClasspath()); |
138 | 146 | var jars = findJunitPlatformNativeJars(modules); |
139 | 147 | imageClasspath.addAll(jars); |
140 | 148 | } |
@@ -310,6 +318,51 @@ private List<Path> findJunitPlatformNativeJars(Set<Module> modulesAlreadyOnClass |
310 | 318 | .collect(Collectors.toList()); |
311 | 319 | } |
312 | 320 |
|
| 321 | + private Set<Module> collectJUnitModulesAlreadyOnClasspath() { |
| 322 | + Set<Module> artifacts = new HashSet<>(); |
| 323 | + for (Path entry : imageClasspath) { |
| 324 | + if (isJUnitArtifact(entry)) { |
| 325 | + File pom = getArtifactPOM(entry); |
| 326 | + if (pom != null) { |
| 327 | + artifacts.add(getModuleFromPOM(pom)); |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + return artifacts; |
| 333 | + } |
| 334 | + |
| 335 | + private boolean isJUnitArtifact(Path entry) { |
| 336 | + return entry.toString().contains("junit"); |
| 337 | + } |
| 338 | + |
| 339 | + private File getArtifactPOM(Path classpathEntry) { |
| 340 | + List<File> artifactContent = getArtifactContent(classpathEntry.getParent()); |
| 341 | + List<File> candidates = artifactContent.stream().filter(f -> f.getName().endsWith(".pom")).collect(Collectors.toList()); |
| 342 | + return candidates.size() != 1 ? null : candidates.get(0); |
| 343 | + } |
| 344 | + |
| 345 | + private List<File> getArtifactContent(Path path) { |
| 346 | + File[] content = path.toFile().listFiles(); |
| 347 | + return content == null ? List.of() : List.of(content); |
| 348 | + } |
| 349 | + |
| 350 | + private Module getModuleFromPOM(File pom) { |
| 351 | + try { |
| 352 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 353 | + factory.setIgnoringElementContentWhitespace(true); |
| 354 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 355 | + Document doc = builder.parse(pom); |
| 356 | + |
| 357 | + String groupId = doc.getElementsByTagName("groupId").item(0).getFirstChild().getTextContent(); |
| 358 | + String artifactId = doc.getElementsByTagName("artifactId").item(0).getFirstChild().getTextContent(); |
| 359 | + |
| 360 | + return new Module(groupId, artifactId); |
| 361 | + } catch (ParserConfigurationException | IOException | SAXException e) { |
| 362 | + throw new RuntimeException("Cannot get maven coordinates from " + pom.getPath() + ". Reason: " + e.getMessage()); |
| 363 | + } |
| 364 | + } |
| 365 | + |
313 | 366 | private static final class Module { |
314 | 367 | private final String groupId; |
315 | 368 | private final String artifactId; |
|
0 commit comments