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

Activated profiles should not always deactivate POM profiles that are active by default #4270

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
21 changes: 17 additions & 4 deletions rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -163,6 +160,22 @@ public List<MavenRepository> getEffectiveRepositories() {
.collect(Collectors.toList());
}

List<Profile> activeProfiles(final Iterable<String> explicitActiveProfiles) {
final List<Profile> pomActivatedProfiles =
profiles.stream()
.filter(p -> p.isActive(explicitActiveProfiles))
.collect(Collectors.toList());

if (!pomActivatedProfiles.isEmpty()) {
return pomActivatedProfiles;
}

return profiles
.stream()
.filter(p -> p.getActivation() != null && Boolean.TRUE.equals(p.getActivation().getActiveByDefault()))
.collect(Collectors.toList());
}

/**
* @param downloader A POM downloader to download dependencies and parents.
* @param ctx An execution context containing any maven-specific requirements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,17 @@ void resolveParentsRecursively(Pom requested) throws MavenDownloadingException {
private void resolveParentPropertiesAndRepositoriesRecursively(List<Pom> pomAncestry) throws MavenDownloadingException {
Pom pom = pomAncestry.get(0);

List<Profile> reallyActiveProfiles = pom.activeProfiles(activeProfiles);

//Resolve properties
for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergeProperties(profile.getProperties(), pom);
}
for (Profile profile : reallyActiveProfiles) {
mergeProperties(profile.getProperties(), pom);
}
mergeProperties(pom.getProperties(), pom);

//Resolve repositories (which may rely on properties ^^^)
for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergeRepositories(profile.getRepositories());
}
for (Profile profile : reallyActiveProfiles) {
mergeRepositories(profile.getRepositories());
}
mergeRepositories(pom.getRepositories());

Expand All @@ -431,11 +429,11 @@ private void resolveParentPropertiesAndRepositoriesRecursively(List<Pom> pomAnce
private void resolveParentDependenciesRecursively(List<Pom> pomAncestry) throws MavenDownloadingException {
Pom pom = pomAncestry.get(0);

for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergeDependencyManagement(profile.getDependencyManagement(), pomAncestry);
mergeRequestedDependencies(profile.getDependencies());
}
List<Profile> reallyActiveProfiles = pom.activeProfiles(activeProfiles);

for (Profile profile : reallyActiveProfiles) {
mergeDependencyManagement(profile.getDependencyManagement(), pomAncestry);
mergeRequestedDependencies(profile.getDependencies());
}

mergeDependencyManagement(pom.getDependencyManagement(), pomAncestry);
Expand Down
Loading