From 21eb3464b1b594d10fda8cf40ed4ea8edb1ab56f Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 4 Oct 2024 21:31:43 +0200 Subject: [PATCH] Fix --- .../impl/model/DefaultModelBuilder.java | 15 ++-- .../DefaultProfileActivationContext.java | 4 +- .../impl/model/DefaultProfileSelector.java | 75 ++++++++++++++++--- .../profile/PropertyProfileActivator.java | 3 + 4 files changed, 77 insertions(+), 20 deletions(-) diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java index 9069b7ddbd22..6935afe419d6 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java @@ -720,7 +720,7 @@ private void loadFilePom( // during the raw to build transformation putSource(getGroupId(model), model.getArtifactId(), src); setRootModel(model); - Model activated = activateModel(model, false); + Model activated = activateModel(model, List.of(), false); for (String subproject : getSubprojects(activated)) { if (subproject == null || subproject.isEmpty()) { continue; @@ -1035,8 +1035,10 @@ Model resolveAndReadParentExternally(Model childModel) throws ModelBuilderExcept return parentModel; } - Model activateModel(Model model, boolean saveInfo) throws ModelBuilderException { - int nbProfiles = request.getProfiles().size() + model.getProfiles().size(); + Model activateModel(Model model, List parentProfiles, boolean saveInfo) throws ModelBuilderException { + int nbProfiles = request.getProfiles().size() + + parentProfiles.size() + + model.getProfiles().size(); if (nbProfiles == 0) { if (saveInfo) { result.setActivePomProfiles(List.of()); @@ -1049,6 +1051,7 @@ Model activateModel(Model model, boolean saveInfo) throws ModelBuilderException List profiles = new ArrayList<>(nbProfiles); profiles.addAll(model.getProfiles()); + profiles.addAll(parentProfiles); profiles.addAll(request.getProfiles()); boolean cascade = !MODEL_VERSION_4_0_0.equals(model.getModelVersion()); @@ -1077,15 +1080,13 @@ private Model readEffectiveModel() throws ModelBuilderException { Model parentModel = readParent(inputModel); - Model activatedParent = activateModel(parentModel, false).withProfiles(null); - - Model model = inheritanceAssembler.assembleModelInheritance(inputModel, activatedParent, request, this); + Model model = inheritanceAssembler.assembleModelInheritance(inputModel, parentModel, request, this); // model normalization model = modelNormalizer.mergeDuplicates(model, request, this); // profile activation - model = activateModel(model, true); + model = activateModel(model, parentModel.getProfiles(), true); // model interpolation Model resultModel = model; diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileActivationContext.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileActivationContext.java index fc26bdfeb6c9..f9e034c0972e 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileActivationContext.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileActivationContext.java @@ -174,11 +174,11 @@ public DefaultProfileActivationContext setProjectProperties(Map } public void addProfileProperties(Collection profiles) { - Map props = new HashMap<>(this.userProperties); + Map props = new HashMap<>(this.projectProperties); for (var profile : profiles) { props.putAll(profile.getProperties()); } - this.userProperties = unmodifiable(props); + this.projectProperties = unmodifiable(props); } private static List unmodifiable(List list) { diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileSelector.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileSelector.java index 0ed3db6c6704..1dda6ced376e 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileSelector.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultProfileSelector.java @@ -21,9 +21,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; import java.util.function.UnaryOperator; @@ -76,6 +76,16 @@ public List getActiveProfiles( ModelProblemCollector problems, boolean cascade) { + if (cascade) { + return getActiveProfilesCascading(orgProfiles, context, problems); + } else { + return getActiveProfilesNonCascading(orgProfiles, context, problems); + } + } + + public List getActiveProfilesNonCascading( + Collection profiles, ProfileActivationContext context, ModelProblemCollector problems) { + Collection activatedIds = new HashSet<>(context.getActiveProfileIds()); Collection deactivatedIds = new HashSet<>(context.getInactiveProfileIds()); @@ -83,25 +93,68 @@ public List getActiveProfiles( List activePomProfiles = new ArrayList<>(); List activePomProfilesByDefault = new ArrayList<>(); - List profiles = new ArrayList<>(orgProfiles); ProfileActivationInterpolator activationInterpolator = new ProfileActivationInterpolator(context, problems); - //noinspection LoopConditionNotUpdatedInsideLoop - do { - // Interpolate profile activation and keep them in a map keyed to be able to retrieve the original profiles - Map interpolated = new LinkedHashMap<>(); - for (var p : profiles) { - interpolated.put(activationInterpolator.apply(p), p); + for (String source : List.of(Profile.SOURCE_SETTINGS, Profile.SOURCE_POM)) { + // Iterate over the profiles and check if a given profile is activated + List activatedProfiles = new ArrayList<>(); + for (Profile profile : profiles) { + if (Objects.equals(source, profile.getSource())) { + Profile iprofile = activationInterpolator.apply(profile); + if (!deactivatedIds.contains(iprofile.getId())) { + boolean activated = activatedIds.contains(iprofile.getId()); + boolean active = isActive(iprofile, context, problems); + boolean activeByDefault = isActiveByDefault(iprofile); + if (activated || active || activeByDefault) { + if (Profile.SOURCE_POM.equals(profile.getSource())) { + if (activated || active) { + activePomProfiles.add(profile); + } else { + activePomProfilesByDefault.add(profile); + } + } else { + activeSettingsProfiles.add(profile); + } + activatedProfiles.add(profile); + } + } + } } + context.addProfileProperties(activatedProfiles); + } + List allActivated = new ArrayList<>(); + if (activePomProfiles.isEmpty()) { + allActivated.addAll(activePomProfilesByDefault); + } else { + allActivated.addAll(activePomProfiles); + } + allActivated.addAll(activeSettingsProfiles); + + return allActivated; + } + + public List getActiveProfilesCascading( + Collection orgProfiles, ProfileActivationContext context, ModelProblemCollector problems) { + + Collection activatedIds = new HashSet<>(context.getActiveProfileIds()); + Collection deactivatedIds = new HashSet<>(context.getInactiveProfileIds()); + + List activeSettingsProfiles = new ArrayList<>(); + List activePomProfiles = new ArrayList<>(); + List activePomProfilesByDefault = new ArrayList<>(); + + List profiles = new ArrayList<>(orgProfiles); + ProfileActivationInterpolator activationInterpolator = new ProfileActivationInterpolator(context, problems); + while (true) { // Iterate over the profiles and check if a given profile is activated List activatedProfiles = new ArrayList<>(); - for (Profile iprofile : interpolated.keySet()) { + for (Profile profile : List.copyOf(profiles)) { + Profile iprofile = activationInterpolator.apply(profile); if (!deactivatedIds.contains(iprofile.getId())) { boolean activated = activatedIds.contains(iprofile.getId()); boolean active = isActive(iprofile, context, problems); boolean activeByDefault = isActiveByDefault(iprofile); if (activated || active || activeByDefault) { - Profile profile = interpolated.get(iprofile); if (Profile.SOURCE_POM.equals(profile.getSource())) { if (activated || active) { activePomProfiles.add(profile); @@ -120,7 +173,7 @@ public List getActiveProfiles( break; } context.addProfileProperties(activatedProfiles); - } while (cascade); + } List allActivated = new ArrayList<>(); if (activePomProfiles.isEmpty()) { diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/PropertyProfileActivator.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/PropertyProfileActivator.java index a8eb0fc7cf16..884f4a994a67 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/PropertyProfileActivator.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/profile/PropertyProfileActivator.java @@ -70,6 +70,9 @@ public boolean isActive(Profile profile, ProfileActivationContext context, Model } String sysValue = context.getUserProperties().get(name); + if (sysValue == null) { + sysValue = context.getProjectProperties().get(name); + } if (sysValue == null) { sysValue = context.getSystemProperties().get(name); }