Skip to content

Commit

Permalink
[MNG-5235] interpolate available properties during default profile se…
Browse files Browse the repository at this point in the history
…lection
  • Loading branch information
mbenson committed Mar 19, 2024
1 parent 33788fd commit d42c7e7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.model.Activation;
import org.apache.maven.model.Profile;
Expand All @@ -35,6 +40,10 @@
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblemCollectorRequest;
import org.apache.maven.model.profile.activation.ProfileActivator;
import org.apache.maven.model.v4.MavenTransformer;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;

/**
* Calculates the active profiles among a given collection of profiles.
Expand All @@ -47,7 +56,7 @@ public class DefaultProfileSelector implements ProfileSelector {
private final List<ProfileActivator> activators;

public DefaultProfileSelector() {
this.activators = new ArrayList<>();
this(new ArrayList<>());
}

@Inject
Expand Down Expand Up @@ -76,16 +85,24 @@ public List<org.apache.maven.api.model.Profile> getActiveProfilesV4(
@Override
public List<Profile> getActiveProfiles(
Collection<Profile> profiles, ProfileActivationContext context, ModelProblemCollector problems) {

if (profiles.stream().map(Profile::getId).distinct().count() < profiles.size()) {
// invalid profile specification
return Collections.emptyList();
}
Collection<String> activatedIds = new HashSet<>(context.getActiveProfileIds());
Collection<String> deactivatedIds = new HashSet<>(context.getInactiveProfileIds());

List<Profile> activeProfiles = new ArrayList<>(profiles.size());
List<Profile> activePomProfilesByDefault = new ArrayList<>();
boolean activatedPomProfileNotByDefault = false;

Map<String, Profile> activation = earlyInterpolateProfileActivations(profiles, context, problems);

for (Profile profile : profiles) {
if (!deactivatedIds.contains(profile.getId())) {
if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
if (activatedIds.contains(profile.getId())
|| isActive(activation.get(profile.getId()), context, problems)) {
activeProfiles.add(profile);

if (Profile.SOURCE_POM.equals(profile.getSource())) {
Expand All @@ -108,6 +125,44 @@ public List<Profile> getActiveProfiles(
return activeProfiles;
}

private Map<String, Profile> earlyInterpolateProfileActivations(
Collection<Profile> original, ProfileActivationContext context, ModelProblemCollector problems) {
if (original.isEmpty()) {
return Collections.emptyMap();
}
final StringSearchInterpolator xform = new StringSearchInterpolator();
xform.setCacheAnswers(true);
Stream.of(context.getUserProperties(), context.getSystemProperties())
.map(MapBasedValueSource::new)
.forEach(xform::addValueSource);

class ProfileInterpolator extends MavenTransformer implements UnaryOperator<Profile> {
ProfileInterpolator() {
super(s -> {
if (s == null) {
return null;
}
try {
return xform.interpolate(s);
} catch (InterpolationException e) {
problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
.setMessage(e.getMessage())
.setException(e));
}
return s;
});
}

@Override
public Profile apply(Profile p) {
return new Profile(transformProfile(p.getDelegate()));
}
}
return original.stream()
.map(new ProfileInterpolator())
.collect(Collectors.toMap(Profile::getId, Function.identity()));
}

private boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
boolean isActive = false;
for (ProfileActivator activator : activators) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.profile;

import java.util.Collections;
import java.util.Map;

import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.ActivationProperty;
import org.apache.maven.api.model.Profile;
import org.apache.maven.model.profile.activation.PropertyProfileActivator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class DefaultProfileSelectorTest {
private DefaultProfileSelector selector;

@BeforeEach
public void setup() {
selector = new DefaultProfileSelector(Collections.singletonList(new PropertyProfileActivator()));
}

@Test
public void testProfileActivationInterpolation() {
Map<String, String> userProperties = Collections.singletonMap("foo", "bar");

org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile(Profile.newBuilder()
.id("foo")
.activation(Activation.newBuilder()
.property(ActivationProperty.newBuilder()
.name("foo")
.value("${foo}")
.build())
.build())
.build());

assertThat(selector.getActiveProfiles(
Collections.singleton(profile),
new DefaultProfileActivationContext().setUserProperties(userProperties),
p -> {}))
.containsExactly(profile);
}
}

0 comments on commit d42c7e7

Please sign in to comment.