[ 
https://issues.apache.org/jira/browse/MNG-8081?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17830722#comment-17830722
 ] 

ASF GitHub Bot commented on MNG-8081:
-------------------------------------

gnodet commented on code in PR #1446:
URL: https://github.com/apache/maven/pull/1446#discussion_r1538325712


##########
maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java:
##########
@@ -897,69 +904,89 @@ private List<org.apache.maven.api.model.Profile> 
interpolateActivations(
             List<org.apache.maven.api.model.Profile> profiles,
             DefaultProfileActivationContext context,
             DefaultModelProblemCollector problems) {
-        List<org.apache.maven.api.model.Profile> newProfiles = null;
-        for (int index = 0; index < profiles.size(); index++) {
-            org.apache.maven.api.model.Profile profile = profiles.get(index);
-            org.apache.maven.api.model.Activation activation = 
profile.getActivation();
-            if (activation != null) {
-                org.apache.maven.api.model.ActivationFile file = 
activation.getFile();
-                if (file != null) {
-                    String oldExists = file.getExists();
-                    if (isNotEmpty(oldExists)) {
+        if (profiles.stream()
+                .map(org.apache.maven.api.model.Profile::getActivation)
+                .filter(Objects::nonNull)
+                .findFirst()
+                .isEmpty()) {
+            return profiles;
+        }
+        final Interpolator xform = new RegexBasedInterpolator();
+        xform.setCacheAnswers(true);
+        Stream.of(context.getUserProperties(), context.getSystemProperties())
+                .map(MapBasedValueSource::new)
+                .forEach(xform::addValueSource);
+
+        class ProfileInterpolator extends MavenTransformer
+                implements UnaryOperator<org.apache.maven.api.model.Profile> {
+            ProfileInterpolator() {
+                super(s -> {
+                    if (isNotEmpty(s)) {
                         try {
-                            String newExists = interpolate(oldExists, context);
-                            if (!Objects.equals(oldExists, newExists)) {
-                                if (newProfiles == null) {
-                                    newProfiles = new ArrayList<>(profiles);
-                                }
-                                newProfiles.set(
-                                        index, 
profile.withActivation(activation.withFile(file.withExists(newExists))));
-                            }
+                            return xform.interpolate(s);
                         } catch (InterpolationException e) {
-                            addInterpolationProblem(problems, file, oldExists, 
e, "exists");
-                        }
-                    } else {
-                        String oldMissing = file.getMissing();
-                        if (isNotEmpty(oldMissing)) {
-                            try {
-                                String newMissing = interpolate(oldMissing, 
context);
-                                if (!Objects.equals(oldMissing, newMissing)) {
-                                    if (newProfiles == null) {
-                                        newProfiles = new 
ArrayList<>(profiles);
-                                    }
-                                    newProfiles.set(
-                                            index,
-                                            
profile.withActivation(activation.withFile(file.withMissing(newMissing))));
-                                }
-                            } catch (InterpolationException e) {
-                                addInterpolationProblem(problems, file, 
oldMissing, e, "missing");
-                            }
+                            problems.add(new 
ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
+                                    .setMessage(e.getMessage())
+                                    .setException(e));
                         }
                     }
+                    return s;
+                });
+            }
+
+            @Override
+            public org.apache.maven.api.model.Profile 
apply(org.apache.maven.api.model.Profile p) {
+                return org.apache.maven.api.model.Profile.newBuilder(p)
+                        .activation(transformActivation(p.getActivation()))
+                        .build();
+            }
+
+            @Override
+            protected void 
transformActivationFile_Missing(ActivationFile.Builder builder, ActivationFile 
target) {
+                final String path = target.getMissing();
+                final String xformed = transformPath(path, target, "missing");
+                if (xformed != path) {
+                    builder.missing(xformed);
                 }
             }
+
+            @Override
+            protected void 
transformActivationFile_Exists(ActivationFile.Builder builder, ActivationFile 
target) {
+                final String path = target.getExists();
+                final String xformed = transformPath(path, target, "exists");
+                if (xformed != path) {
+                    builder.exists(xformed);
+                }
+            }
+
+            private String transformPath(String path, ActivationFile target, 
String locationKey) {
+                if (isNotEmpty(path)) {
+                    try {
+                        return 
profileActivationFilePathInterpolator.interpolate(path, context);
+                    } catch (InterpolationException e) {
+                        addInterpolationProblem(problems, target, path, e, 
locationKey);
+                    }
+                }
+                return path;
+            }
         }
-        return newProfiles != null ? newProfiles : profiles;
+        return profiles.stream().map(new 
ProfileInterpolator()).collect(Collectors.toList());

Review Comment:
   `.collect(Collectors.toList())` -> `.toList()`





> default profile activation should consider available system and user 
> properties
> -------------------------------------------------------------------------------
>
>                 Key: MNG-8081
>                 URL: https://issues.apache.org/jira/browse/MNG-8081
>             Project: Maven
>          Issue Type: Improvement
>          Components: Profiles
>    Affects Versions: 3.9.6, 4.0.0
>            Reporter: Matthew Jason Benson
>            Priority: Minor
>
> As discussed in my open PR, my use case is to compare between environment 
> variables e.g.:
> {code:java}
> <activation>
>   <property>
>     <name>env.FOO</name>
>     <value>${env.BAR}</value>
>   </property>
> </activation>{code}
> Limiting the interpolation to user/system properties means that there is no 
> mindf*ck resulting from profile activation order, etc., and keeps this 
> request nonthreatening.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to