|
61 | 61 | import org.eclipse.aether.resolution.DependencyResolutionException;
|
62 | 62 | import org.eclipse.aether.resolution.DependencyResult;
|
63 | 63 | import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;
|
| 64 | +import org.w3c.dom.Document; |
| 65 | +import org.xml.sax.SAXException; |
64 | 66 |
|
| 67 | +import javax.xml.parsers.DocumentBuilder; |
| 68 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 69 | +import javax.xml.parsers.ParserConfigurationException; |
65 | 70 | import java.io.File;
|
66 | 71 | import java.io.IOException;
|
67 | 72 | import java.io.UncheckedIOException;
|
@@ -132,6 +137,8 @@ protected void addDependenciesToClasspath() throws MojoExecutionException {
|
132 | 137 | .filter(it -> it.getGroupId().startsWith(NativeImageConfigurationUtils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit"))
|
133 | 138 | .map(it -> it.getFile().toPath())
|
134 | 139 | .forEach(imageClasspath::add);
|
| 140 | + |
| 141 | + modules.addAll(collectJUnitModulesAlreadyOnClasspath()); |
135 | 142 | var jars = findJunitPlatformNativeJars(modules);
|
136 | 143 | imageClasspath.addAll(jars);
|
137 | 144 | }
|
@@ -298,17 +305,53 @@ private List<Path> findJunitPlatformNativeJars(Set<Module> modulesAlreadyOnClass
|
298 | 305 | .stream()
|
299 | 306 | .map(ArtifactResult::getArtifact)
|
300 | 307 | .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()))) |
302 | 308 | .map(a -> a.getFile().toPath())
|
303 | 309 | .collect(Collectors.toList());
|
304 | 310 | }
|
305 | 311 |
|
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"); |
308 | 328 | }
|
309 | 329 |
|
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 | + } |
312 | 355 | }
|
313 | 356 |
|
314 | 357 | private static final class Module {
|
|
0 commit comments