From f2f980391b46308cdef893a209d127f2db858d43 Mon Sep 17 00:00:00 2001 From: lprimak Date: Sat, 21 Dec 2024 18:04:09 -0600 Subject: [PATCH] enh: keep updated with maven 4 rc-2 consumer pom changes --- README.md | 3 ++ .../impl/maven/MavenWorkingSessionImpl.java | 40 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c481a156..f3f708ea 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ The project primarily focuses on supporting Maven-based repository structures, e - **Maven**: Version 3.3.9 or newer. - **Gradle**: Version 3 or newer. +## Maven 4 Feature: +- Add the following to your ~/.m2/maven.properties in order to run tests from +within your project's submodules: `maven.consumer.pom=true` or add `-Dmaven.consumer.pom=true` to your Maven package command line. ## Adding ShrinkWrap Resolvers to Your Project diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenWorkingSessionImpl.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenWorkingSessionImpl.java index e4f5444b..e30f3764 100644 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenWorkingSessionImpl.java +++ b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/MavenWorkingSessionImpl.java @@ -32,6 +32,7 @@ import java.util.Optional; import java.util.Properties; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Predicate; import java.util.logging.Level; import java.util.logging.Logger; @@ -200,10 +201,12 @@ public MavenWorkingSession loadPomFromFile(File pomFile, Properties userProperti private Collection resolveProjectLocal(final List depsForResolution, Set additionalDependencies) { Collection projectLocalDependencies = new ArrayList<>(depsForResolution.size()); + AtomicBoolean consumerPomFound = new AtomicBoolean(); for (MavenDependency dependency : depsForResolution) { Path resolved = resolveProjectLocal(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), Optional.ofNullable(dependency.getClassifier()), - Optional.ofNullable(dependency.getPackaging().getExtension()), additionalDependencies); + Optional.ofNullable(dependency.getPackaging().getExtension()), + additionalDependencies, consumerPomFound); if (resolved != null && resolved.toFile().exists()) { Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), dependency.getPackaging().getExtension(), dependency.getVersion(), @@ -215,12 +218,12 @@ private Collection resolveProjectLocal(final List classifier, Optional extension, - Set additionalDependencies) { + Set additionalDependencies, AtomicBoolean consumerPomFound) { Path projectLocalRepository = findProjectLocalRepository(); if (projectLocalRepository == null) { return null; @@ -228,7 +231,7 @@ private Path resolveProjectLocal(String groupId, String artifactId, String versi Predicate isNotEmpty = s -> !s.isEmpty(); processAdditionalDependencies(projectLocalRepository, groupId, artifactId, version, - additionalDependencies); + additionalDependencies, consumerPomFound); return projectLocalRepository.resolve(groupId).resolve(artifactId).resolve(version) .resolve(toVersionedArtifact(artifactId, version) @@ -242,19 +245,26 @@ private static String toVersionedArtifact(String artifactId, String version) { private void processAdditionalDependencies(Path projectLocalRepository, String groupId, String artifactId, String version, - Set additionalDependencies) { + Set additionalDependencies, + AtomicBoolean consumerPomFound) { Path directory = projectLocalRepository.resolve(groupId).resolve(artifactId).resolve(version); - File pom = directory.resolve(toVersionedArtifact(artifactId, version) + "-consumer.pom").toFile(); - if (!pom.exists()) { - pom = directory.resolve(toVersionedArtifact(artifactId, version) + ".pom").toFile(); + File consumerPom = directory.resolve(toVersionedArtifact(artifactId, version) + "-consumer.pom").toFile(); + File buildPom = directory.resolve(toVersionedArtifact(artifactId, version) + "-build.pom").toFile(); + if (!consumerPom.exists() && buildPom.exists()) { + // in some versions of maven, consumer pom is just generated with ".pom" extension + // in that case, build pom will also exist + consumerPom = directory.resolve(toVersionedArtifact(artifactId, version) + ".pom").toFile(); } - if (pom.exists()) { - Set transitiveDependencies = loadPomFromFile(pom).getParsedPomFile().getDependencies(); + + if (consumerPom.exists()) { + consumerPomFound.set(true); + Set transitiveDependencies = loadPomFromFile(consumerPom).getParsedPomFile().getDependencies(); transitiveDependencies.removeAll(additionalDependencies); if (!transitiveDependencies.isEmpty()) { additionalDependencies.addAll(transitiveDependencies); transitiveDependencies.forEach(dependency -> processAdditionalDependencies(projectLocalRepository, - dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), additionalDependencies)); + dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), + additionalDependencies, consumerPomFound)); } } } @@ -303,8 +313,12 @@ public Collection resolveDependencies(final MavenResoluti } else { Set allDependencies = new LinkedHashSet<>(depsForResolution); projectLocalDependencies = resolveProjectLocal(depsForResolution, allDependencies); - resolveFromRepository = filterFromLocal( - allDependencies.stream().collect(Collectors.toList()), projectLocalDependencies); + if (projectLocalDependencies.isEmpty()) { + resolveFromRepository = depsForResolution; + } else { + resolveFromRepository = filterFromLocal( + allDependencies.stream().collect(Collectors.toList()), projectLocalDependencies); + } } final CollectRequest request = new CollectRequest(MavenConverter.asDependencies(resolveFromRepository,