Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: keep updated with maven 4 rc-2 consumer pom changes #377

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public MavenRepositorySystem() {
* @return A working session spawned from the repository system.
*/
public DefaultRepositorySystemSession getSession(final Settings settings, boolean legacyLocalRepository) {
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(context.repositorySystemSession());

MavenManagerBuilder builder = new MavenManagerBuilder(context.repositorySystem(), settings);

Expand Down
Loading