Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 4, 2024
1 parent a99273b commit 21eb346
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Profile> parentProfiles, boolean saveInfo) throws ModelBuilderException {
int nbProfiles = request.getProfiles().size()
+ parentProfiles.size()
+ model.getProfiles().size();
if (nbProfiles == 0) {
if (saveInfo) {
result.setActivePomProfiles(List.of());
Expand All @@ -1049,6 +1051,7 @@ Model activateModel(Model model, boolean saveInfo) throws ModelBuilderException

List<Profile> 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());
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ public DefaultProfileActivationContext setProjectProperties(Map<String, String>
}

public void addProfileProperties(Collection<Profile> profiles) {
Map<String, String> props = new HashMap<>(this.userProperties);
Map<String, String> props = new HashMap<>(this.projectProperties);
for (var profile : profiles) {
props.putAll(profile.getProperties());
}
this.userProperties = unmodifiable(props);
this.projectProperties = unmodifiable(props);
}

private static List<String> unmodifiable(List<String> list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,32 +76,85 @@ public List<Profile> getActiveProfiles(
ModelProblemCollector problems,
boolean cascade) {

if (cascade) {
return getActiveProfilesCascading(orgProfiles, context, problems);
} else {
return getActiveProfilesNonCascading(orgProfiles, context, problems);
}
}

public List<Profile> getActiveProfilesNonCascading(
Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {

Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());

List<Profile> activeSettingsProfiles = new ArrayList<>();
List<Profile> activePomProfiles = new ArrayList<>();
List<Profile> activePomProfilesByDefault = new ArrayList<>();

List<Profile> 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<Profile, Profile> 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<Profile> 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<Profile> allActivated = new ArrayList<>();
if (activePomProfiles.isEmpty()) {
allActivated.addAll(activePomProfilesByDefault);
} else {
allActivated.addAll(activePomProfiles);
}
allActivated.addAll(activeSettingsProfiles);

return allActivated;
}

public List<Profile> getActiveProfilesCascading(
Collection<Profile> orgProfiles, ProfileActivationContext context, ModelProblemCollector problems) {

Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());

List<Profile> activeSettingsProfiles = new ArrayList<>();
List<Profile> activePomProfiles = new ArrayList<>();
List<Profile> activePomProfilesByDefault = new ArrayList<>();

List<Profile> 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<Profile> 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);
Expand All @@ -120,7 +173,7 @@ public List<Profile> getActiveProfiles(
break;
}
context.addProfileProperties(activatedProfiles);
} while (cascade);
}

List<Profile> allActivated = new ArrayList<>();
if (activePomProfiles.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 21eb346

Please sign in to comment.