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

Merged
Merged
24 changes: 22 additions & 2 deletions rewrite-maven/src/main/java/org/openrewrite/maven/tree/Pom.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toList;
import static org.openrewrite.internal.ListUtils.concatAll;

/**
Expand Down Expand Up @@ -160,7 +160,27 @@ public List<MavenRepository> getEffectiveRepositories() {
return r;
})
.distinct()
.collect(Collectors.toList());
.collect(toList());
}

/**
* "[activeByDefault] profile will automatically be active for all builds unless another profile in the same POM is
* activated using one of the previously described methods. All profiles that are active by default are automatically
* deactivated when a profile in the POM is activated on the command line or through its activation config."
*
* @param explicitActiveProfiles Any profiles explicitly activated on the command line.
* @return the effective profiles, given the explicitly active profiles, or failing that those active by default.
*/
List<Profile> effectiveProfiles(Iterable<String> explicitActiveProfiles) {
List<Profile> pomActivatedProfiles = profiles.stream()
.filter(p -> p.isActive(explicitActiveProfiles))
.collect(toList());
if (!pomActivatedProfiles.isEmpty()) {
return pomActivatedProfiles;
}
return profiles.stream()
.filter(p -> p.getActivation() != null && Boolean.TRUE.equals(p.getActivation().getActiveByDefault()))
.collect(toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,17 @@ void resolveParentsRecursively(Pom requested) throws MavenDownloadingException {
private void resolveParentPropertiesAndRepositoriesRecursively(List<Pom> pomAncestry) throws MavenDownloadingException {
Pom pom = pomAncestry.get(0);

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

//Resolve properties
for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergeProperties(profile.getProperties(), pom);
}
for (Profile profile : effectiveProfiles) {
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 : effectiveProfiles) {
mergeRepositories(profile.getRepositories());
}
mergeRepositories(pom.getRepositories());

Expand All @@ -435,11 +433,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> effectiveProfiles = pom.effectiveProfiles(activeProfiles);

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

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