This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 4c794fe  SLING-9503 : Add control over variable replacement in feature 
models
4c794fe is described below

commit 4c794fef05ee05f2b0f759b89a5888f417bc0fc2
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Fri Jun 5 12:18:41 2020 +0200

    SLING-9503 : Add control over variable replacement in feature models
---
 src/it/attach-metadata-from-pom/pom.xml            |  1 +
 src/it/variables-interpolator/pom.xml              |  1 +
 .../sling/feature/maven/FeatureProjectConfig.java  | 34 ++++++++++++++-
 .../apache/sling/feature/maven/Preprocessor.java   |  6 ++-
 .../apache/sling/feature/maven/ProjectHelper.java  | 10 +++--
 .../apache/sling/feature/maven/Substitution.java   | 50 +++++++++++++++++-----
 .../feature/maven/mojos/AbstractFeatureMojo.java   | 32 +++++++++++++-
 .../sling/feature/maven/SubstitutionTest.java      | 11 ++---
 8 files changed, 123 insertions(+), 22 deletions(-)

diff --git a/src/it/attach-metadata-from-pom/pom.xml 
b/src/it/attach-metadata-from-pom/pom.xml
index 403fffa..b00ad3e 100644
--- a/src/it/attach-metadata-from-pom/pom.xml
+++ b/src/it/attach-metadata-from-pom/pom.xml
@@ -51,6 +51,7 @@
               <goal>aggregate-features</goal>
             </goals>
             <configuration>
+              
<replacePropertyVariables>interpolated_variable,osgi.framework.version,sling.framework.install.incremental,sling.framework.install.startlevel,sling.ignoreSystemProperties</replacePropertyVariables>
               <aggregates>
                <aggregate>
                  <classifier>metadata-test</classifier>
diff --git a/src/it/variables-interpolator/pom.xml 
b/src/it/variables-interpolator/pom.xml
index 75a3790..1822f94 100644
--- a/src/it/variables-interpolator/pom.xml
+++ b/src/it/variables-interpolator/pom.xml
@@ -57,6 +57,7 @@
               <goal>aggregate-features</goal>
             </goals>
             <configuration>
+              
<enableLegacyVariableReplacement>true</enableLegacyVariableReplacement>
               <aggregates>
                <aggregate>
                  <classifier>interpolator-test</classifier>
diff --git 
a/src/main/java/org/apache/sling/feature/maven/FeatureProjectConfig.java 
b/src/main/java/org/apache/sling/feature/maven/FeatureProjectConfig.java
index a3be7d7..2032d14 100644
--- a/src/main/java/org/apache/sling/feature/maven/FeatureProjectConfig.java
+++ b/src/main/java/org/apache/sling/feature/maven/FeatureProjectConfig.java
@@ -50,6 +50,13 @@ public class FeatureProjectConfig {
 
     public static final String DEFAULT_TEST_FEATURE_DIR = "src/test/features";
 
+    public static final String CFG_ENABLE_PROJ_VARS = 
"enableProjectVariableReplacement";
+
+    public static final String CFG_REPLACE_PROP_VARS = 
"replacePropertyVariables";
+
+    public static final String CFG_LEGACY_REPLACE = 
"enableLegacyVariableReplacement";
+
+
     private final String featuresDirName;
 
     private final String includes;
@@ -70,6 +77,12 @@ public class FeatureProjectConfig {
 
     private final boolean validate;
 
+    private final boolean enableProjectVariableReplacement;
+
+    private final String[] replacePropertyVariables;
+
+    private final boolean enableLegacyVariableReplacement;
+
     public static FeatureProjectConfig getMainConfig(final FeatureProjectInfo 
info) {
         return new FeatureProjectConfig(info, false);
     }
@@ -114,7 +127,14 @@ public class FeatureProjectConfig {
         this.skipAddDep = "true".equals(skipCfg.toLowerCase());
         this.jarStartOrder = ProjectHelper.getConfigValue(info.plugin, 
CFG_JAR_START_ORDER, null);
         this.validate = 
"true".equals(ProjectHelper.getConfigValue(info.plugin, CFG_VALIDATE_FEATURES, 
"true"));
-
+        this.enableProjectVariableReplacement = 
"true".equals(ProjectHelper.getConfigValue(info.plugin, CFG_ENABLE_PROJ_VARS, 
"true"));
+        String vars = ProjectHelper.getConfigValue(info.plugin, 
CFG_REPLACE_PROP_VARS, null);
+        if ( vars == null ) {
+            this.replacePropertyVariables = null;
+        } else {
+            this.replacePropertyVariables = vars.split(",");
+        }
+        this.enableLegacyVariableReplacement = 
"true".equals(ProjectHelper.getConfigValue(info.plugin, CFG_LEGACY_REPLACE, 
"false"));
     }
 
     public String getName() {
@@ -156,5 +176,17 @@ public class FeatureProjectConfig {
     public boolean isValidate() {
         return this.validate;
     }
+
+    public boolean isEnableProjectVariableReplacement() {
+        return enableProjectVariableReplacement;
+    }
+
+    public String[] getReplacePropertyVariables() {
+        return replacePropertyVariables;
+    }
+
+    public boolean isEnableLegacyVariableReplacement() {
+        return enableLegacyVariableReplacement;
+    }
 }
 
diff --git a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java 
b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
index 9fa0dd2..913b637 100644
--- a/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
+++ b/src/main/java/org/apache/sling/feature/maven/Preprocessor.java
@@ -298,7 +298,11 @@ public class Preprocessor {
                     suggestedClassifier = null;
                 }
 
-                final String readJson = 
ProjectHelper.readFeatureFile(info.project, file, suggestedClassifier);
+                final String readJson = 
ProjectHelper.readFeatureFile(info.project, file,
+                        suggestedClassifier,
+                        config.isEnableLegacyVariableReplacement(),
+                        config.isEnableProjectVariableReplacement(),
+                        config.getReplacePropertyVariables());
 
                 final String json = preprocessFeature(info.project, 
config.isValidate(),
                         file, readJson);
diff --git a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java 
b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
index f55ef30..3cc6e7c 100644
--- a/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
+++ b/src/main/java/org/apache/sling/feature/maven/ProjectHelper.java
@@ -531,8 +531,12 @@ public abstract class ProjectHelper {
      * @param file The json file
      * @return The read and minified JSON
      */
-    public static String readFeatureFile(final MavenProject project, final 
File file,
-            final String suggestedClassifier) {
+    public static String readFeatureFile(final MavenProject project,
+            final File file,
+            final String suggestedClassifier,
+            final boolean legacyReplace,
+            final boolean enableProjectVars,
+            final String[] additionalVars) {
         final ArtifactId fileId = new ArtifactId(project.getGroupId(),
                 project.getArtifactId(),
                 project.getVersion(),
@@ -541,7 +545,7 @@ public abstract class ProjectHelper {
 
         // replace variables
         try ( final Reader reader = new FileReader(file) ) {
-            return Substitution.replaceMavenVars(project, 
JSONFeatures.read(reader, fileId, file.getAbsolutePath()));
+            return Substitution.replaceMavenVars(project, legacyReplace, 
enableProjectVars, additionalVars, JSONFeatures.read(reader, fileId, 
file.getAbsolutePath()));
         } catch (final IOException e) {
             throw new RuntimeException("Unable to read feature file " + 
file.getAbsolutePath(), e);
         }
diff --git a/src/main/java/org/apache/sling/feature/maven/Substitution.java 
b/src/main/java/org/apache/sling/feature/maven/Substitution.java
index 48ed999..ae6eec2 100644
--- a/src/main/java/org/apache/sling/feature/maven/Substitution.java
+++ b/src/main/java/org/apache/sling/feature/maven/Substitution.java
@@ -18,6 +18,7 @@ package org.apache.sling.feature.maven;
 
 import java.util.Collections;
 import java.util.List;
+import java.util.Properties;
 
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.apache.maven.project.MavenProject;
@@ -28,26 +29,53 @@ import 
org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
 import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
 import org.codehaus.plexus.interpolation.RecursionInterceptor;
 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
+import org.codehaus.plexus.interpolation.SimpleRecursionInterceptor;
 
 import aQute.bnd.version.MavenVersion;
 
 public class Substitution {
-    public static String replaceMavenVars(MavenProject project, String s) {
+
+    public static String replaceMavenVars(MavenProject project,
+            boolean legacyReplace,
+            boolean replaceProjectProps,
+            String[] additionalProperties, String s) {
         RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
-        project.getProperties().setProperty("project.osgiVersion", 
getOSGiVersion(project.getVersion()));
-        interpolator.addValueSource(new 
PropertiesBasedValueSource(System.getProperties()));
-        interpolator.addValueSource(new 
PropertiesBasedValueSource(project.getProperties()));
+        final RecursionInterceptor recursionInterceptor;
+        if ( legacyReplace ) {
+            project.getProperties().setProperty("project.osgiVersion", 
getOSGiVersion(project.getVersion()));
+            interpolator.addValueSource(new 
PropertiesBasedValueSource(System.getProperties()));
+            interpolator.addValueSource(new 
PropertiesBasedValueSource(project.getProperties()));
+
+            List<String> synonymPrefixes = 
Collections.singletonList("project.");
 
-        List<String> synonymPrefixes = Collections.singletonList("project.");
+            PrefixedValueSourceWrapper modelWrapper = new 
PrefixedValueSourceWrapper(
+                    new ObjectBasedValueSource(project),
+                    synonymPrefixes,
+                    true);
+            interpolator.addValueSource( modelWrapper );
 
-        PrefixedValueSourceWrapper modelWrapper = new 
PrefixedValueSourceWrapper(
-                new ObjectBasedValueSource(project),
-                synonymPrefixes,
-                true);
-        interpolator.addValueSource( modelWrapper );
+            recursionInterceptor = new 
PrefixAwareRecursionInterceptor(synonymPrefixes, true);
 
-        RecursionInterceptor recursionInterceptor = new 
PrefixAwareRecursionInterceptor(synonymPrefixes, true);
+        } else {
+            final Properties props = new Properties();
+            if ( replaceProjectProps ) {
+                props.setProperty("project.groupId", project.getGroupId());
+                props.setProperty("project.artifactId", 
project.getArtifactId());
+                props.setProperty("project.version", project.getVersion());
+                props.setProperty("project.osgiVersion", 
getOSGiVersion(project.getVersion()));
+            }
+            if ( additionalProperties != null ) {
+                for(String p : additionalProperties) {
+                    p = p.trim();
+                    if ( project.getProperties().containsKey(p)) {
+                        props.setProperty(p, 
project.getProperties().getProperty(p));
+                    }
+                }
+            }
+            interpolator.addValueSource(new PropertiesBasedValueSource(props));
 
+            recursionInterceptor = new SimpleRecursionInterceptor();
+        }
         try {
             return interpolator.interpolate(s, recursionInterceptor);
         } catch (InterpolationException e) {
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
index ff8ea3d..57494bd 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/AbstractFeatureMojo.java
@@ -165,6 +165,33 @@ public abstract class AbstractFeatureMojo extends 
AbstractMojo {
     @Parameter(name=FeatureProjectConfig.CFG_JAR_START_ORDER)
     private int jarStartOrder;
 
+    /**
+     * Enable the replacement of variables when reading a feature model. The 
supported
+     * variables are "project.groupId", "project.artifactId", 
"project.version" and
+     * "project.osgiVersion".
+     * @since 1.3.6
+     */
+    @Parameter(defaultValue = "true")
+    private boolean enableProjectVariableReplacement;
+
+    /**
+     * A comma separated list of variables which are replaced when a feature 
model
+     * is read. The value of these variables is fetched from the project 
properties.
+     * @since 1.3.6
+     */
+    @Parameter
+    private String replacePropertyVariables;
+
+    /**
+     * Enable old variable replacement in feature model based on the full maven
+     * project including system variables.
+     * If this is enabled, enableProjectVariableReplacement and
+     * replacePropertyVariables have no effect.
+     * @since 1.3.6
+     */
+    @Parameter(defaultValue = "false")
+    private boolean enableLegacyVariableReplacement;
+
     @Parameter(property = "project", readonly = true, required = true)
     protected MavenProject project;
 
@@ -239,7 +266,10 @@ public abstract class AbstractFeatureMojo extends 
AbstractMojo {
             for (final File file : files) {
                 getLog().debug("Reading feature file " + file);
                 try {
-                    final String json = ProjectHelper.readFeatureFile(project, 
file, null);
+                    final String json = ProjectHelper.readFeatureFile(project, 
file, null,
+                            this.enableLegacyVariableReplacement,
+                            this.enableProjectVariableReplacement,
+                            this.replacePropertyVariables != null ? 
this.replacePropertyVariables.split(",") : null);
 
                     try (final Reader reader = new StringReader(json)) {
                         final Feature feature = FeatureJSONReader.read(reader, 
file.getAbsolutePath());
diff --git a/src/test/java/org/apache/sling/feature/maven/SubstitutionTest.java 
b/src/test/java/org/apache/sling/feature/maven/SubstitutionTest.java
index 6602dff..372ed97 100644
--- a/src/test/java/org/apache/sling/feature/maven/SubstitutionTest.java
+++ b/src/test/java/org/apache/sling/feature/maven/SubstitutionTest.java
@@ -16,12 +16,12 @@
  */
 package org.apache.sling.feature.maven;
 
-import org.apache.maven.project.MavenProject;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
 
 import java.util.Properties;
 
-import static org.junit.Assert.assertEquals;
+import org.apache.maven.project.MavenProject;
+import org.junit.Test;
 
 public class SubstitutionTest {
     @Test
@@ -29,7 +29,8 @@ public class SubstitutionTest {
         MavenProject proj = new MavenProject();
         Properties p = proj.getProperties();
         p.put("test", "foo");
-        assertEquals("hellofoogoodbyefoo", Substitution.replaceMavenVars(proj, 
"hello${test}goodbye${test}"));
+        assertEquals("hellofoogoodbyefoo", Substitution.replaceMavenVars(proj, 
false, false, new String[] {"test"}, "hello${test}goodbye${test}"));
+        assertEquals("hello${test}goodbye${test}", 
Substitution.replaceMavenVars(proj, false, false, null, 
"hello${test}goodbye${test}"));
     }
 
     @Test
@@ -44,7 +45,7 @@ public class SubstitutionTest {
 
             System.setProperty("test", "bar");
 
-            assertEquals("hellobargoodbyebar", 
Substitution.replaceMavenVars(proj, "hello${test}goodbye${test}"));
+            assertEquals("hellobargoodbyebar", 
Substitution.replaceMavenVars(proj, true, false, null, 
"hello${test}goodbye${test}"));
         } finally {
             // Restore the system properties
             System.setProperties(storedProps);

Reply via email to