Skip to content

Commit

Permalink
Activated profiles should not always deactivate POM profiles that are…
Browse files Browse the repository at this point in the history
… active by default (#4270)

* [4269] partial

* Make activeProfiles method package private.

* Tweaks to test and add test showing issue with settings active profiles

* Update rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Slight polish

* Further polish

* Remove unused foobar profile from test

* Final bit of polish

* Document the purpose of the new method with quote of the docs

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent 01a3911 commit cd95e0e
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 25 deletions.
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

0 comments on commit cd95e0e

Please sign in to comment.