Skip to content

Commit

Permalink
enh: keep updated with maven 4 rc-2 consumer pom changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lprimak committed Dec 22, 2024
1 parent 0d3d07f commit f2f9803
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,10 +201,12 @@ public MavenWorkingSession loadPomFromFile(File pomFile, Properties userProperti
private Collection<ArtifactResult> resolveProjectLocal(final List<MavenDependency> depsForResolution,
Set<MavenDependency> additionalDependencies) {
Collection<ArtifactResult> 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(),
Expand All @@ -215,20 +218,20 @@ private Collection<ArtifactResult> resolveProjectLocal(final List<MavenDependenc
projectLocalDependencies.add(result);
}
}
return projectLocalDependencies;
return consumerPomFound.get() ? projectLocalDependencies : Collections.emptyList();
}

private Path resolveProjectLocal(String groupId, String artifactId, String version,
Optional<String> classifier, Optional<String> extension,
Set<MavenDependency> additionalDependencies) {
Set<MavenDependency> additionalDependencies, AtomicBoolean consumerPomFound) {
Path projectLocalRepository = findProjectLocalRepository();
if (projectLocalRepository == null) {
return null;
}

Predicate<String> isNotEmpty = s -> !s.isEmpty();
processAdditionalDependencies(projectLocalRepository, groupId, artifactId, version,
additionalDependencies);
additionalDependencies, consumerPomFound);

return projectLocalRepository.resolve(groupId).resolve(artifactId).resolve(version)
.resolve(toVersionedArtifact(artifactId, version)
Expand All @@ -242,19 +245,26 @@ private static String toVersionedArtifact(String artifactId, String version) {

private void processAdditionalDependencies(Path projectLocalRepository, String groupId,
String artifactId, String version,
Set<MavenDependency> additionalDependencies) {
Set<MavenDependency> 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<MavenDependency> transitiveDependencies = loadPomFromFile(pom).getParsedPomFile().getDependencies();

if (consumerPom.exists()) {
consumerPomFound.set(true);
Set<MavenDependency> 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));
}
}
}
Expand Down Expand Up @@ -303,8 +313,12 @@ public Collection<MavenResolvedArtifact> resolveDependencies(final MavenResoluti
} else {
Set<MavenDependency> 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,
Expand Down

0 comments on commit f2f9803

Please sign in to comment.