Skip to content

Commit

Permalink
[4269] Correctly return the active profiles from a pom
Browse files Browse the repository at this point in the history
This is just PoC code.  I'm out of time as my vacation is starting:)

 openrewrite#4269
  • Loading branch information
Samuel Cox committed Jun 29, 2024
1 parent a6c3ece commit 22a9435
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
23 changes: 23 additions & 0 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,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -157,6 +158,28 @@ public List<MavenRepository> getEffectiveRepositories() {
.collect(Collectors.toList());
}

public List<Profile> activeProfiles(final Iterable<String> userSpecifiedProfiles) {
// pre-compute? I think this is immutable, but not sure

// TODO This is should probably be coded differently or have methods renamed/etc.
// I just did this quickly before vacation starts:)
final List<Profile> explicitActiveProfiles =
getProfiles().stream()
.filter(p -> p.isActive(userSpecifiedProfiles))
.collect(Collectors.toList());

// activeByDefault profiles should be active even if they don't exist
// in userSpecifiedProfiles _unless_ a profile was active.
if (!explicitActiveProfiles.isEmpty()) {
return explicitActiveProfiles;
}

return getProfiles().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 @@ -385,20 +385,12 @@ void resolveParentsRecursively(Pom requested) throws MavenDownloadingException {
private void resolveParentPropertiesAndRepositoriesRecursively(List<Pom> pomAncestry) throws MavenDownloadingException {
Pom pom = pomAncestry.get(0);

//Resolve properties
for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergeProperties(profile.getProperties(), pom);
}
//Resolve properties and repositories
for (final Profile profile : pom.activeProfiles(activeProfiles)) {
mergeProperties(profile.getProperties(), pom);
mergeRepositories(profile.getRepositories());
}
mergeProperties(pom.getProperties(), pom);

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

if (pom.getParent() != null) {
Expand All @@ -419,11 +411,9 @@ 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(), pom);
mergeRequestedDependencies(profile.getDependencies());
}
for (Profile profile : pom.activeProfiles(activeProfiles)) {
mergeDependencyManagement(profile.getDependencyManagement(), pom);
mergeRequestedDependencies(profile.getDependencies());
}

mergeDependencyManagement(pom.getDependencyManagement(), pom);
Expand Down Expand Up @@ -451,11 +441,9 @@ private void resolveParentDependenciesRecursively(List<Pom> pomAncestry) throws
private void resolveParentPluginsRecursively(List<Pom> pomAncestry) throws MavenDownloadingException {
Pom pom = pomAncestry.get(0);

for (Profile profile : pom.getProfiles()) {
if (profile.isActive(activeProfiles)) {
mergePluginManagement(profile.getPluginManagement());
mergePlugins(profile.getPlugins());
}
for (Profile profile : pom.activeProfiles(activeProfiles)) {
mergePluginManagement(profile.getPluginManagement());
mergePlugins(profile.getPlugins());
}

mergePluginManagement(pom.getPluginManagement());
Expand Down

0 comments on commit 22a9435

Please sign in to comment.