You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2018/11/09 23:33:25 UTC
[sling-slingfeature-maven-plugin] 01/01: SLING-8003 - Interpolate
Maven variables using the Maven Interpolator APIs rather that iterate
string replacing operations
This is an automated email from the ASF dual-hosted git repository.
simonetripodi pushed a commit to branch SLING-8003_rebased
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
commit ba47de6ab8c688afd4f38067d64fb16a87b98149
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Sat Nov 10 00:33:11 2018 +0100
SLING-8003 - Interpolate Maven variables using the Maven Interpolator
APIs rather that iterate string replacing operations
---
src/it/variables-interpolator/invoker.properties | 16 +++++
src/it/variables-interpolator/pom.xml | 82 ++++++++++++++++++++++
.../src/main/features/interpolated-variables.json | 21 ++++++
src/it/variables-interpolator/verify.bsh | 60 ++++++++++++++++
.../apache/sling/feature/maven/Substitution.java | 40 ++++++-----
5 files changed, 203 insertions(+), 16 deletions(-)
diff --git a/src/it/variables-interpolator/invoker.properties b/src/it/variables-interpolator/invoker.properties
new file mode 100644
index 0000000..8151568
--- /dev/null
+++ b/src/it/variables-interpolator/invoker.properties
@@ -0,0 +1,16 @@
+# 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.
+ invoker.goals = clean package
+invoker.debug = true
diff --git a/src/it/variables-interpolator/pom.xml b/src/it/variables-interpolator/pom.xml
new file mode 100644
index 0000000..80bd852
--- /dev/null
+++ b/src/it/variables-interpolator/pom.xml
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.sling</groupId>
+ <artifactId>slingfeature-maven-plugin-test</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0.0-SNAPSHOT</version>
+
+ <name>Apache Sling Features Maven plugin test</name>
+ <description>This is just an Apache Sling Features Maven plugin test to verify variables interpolation</description>
+
+ <organization>
+ <name>The Apache Software Foundation</name>
+ <url>https://www.apache.org/</url>
+ </organization>
+
+ <licenses>
+ <license>
+ <name>Apache License, Version 2.0</name>
+ <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+
+ <properties>
+ <interpolated_variable>true</interpolated_variable>
+ <osgi.framework.version>1.9.0</osgi.framework.version>
+ <sling.framework.install.incremental>true</sling.framework.install.incremental>
+ <sling.framework.install.startlevel>1</sling.framework.install.startlevel>
+ <sling.ignoreSystemProperties>true</sling.ignoreSystemProperties>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>@project.groupId@</groupId>
+ <artifactId>@project.artifactId@</artifactId>
+ <version>@project.version@</version>
+ <extensions>true</extensions>
+ <executions>
+ <execution>
+ <id>aggregate</id>
+ <phase>generate-resources</phase>
+ <goals>
+ <goal>aggregate-features</goal>
+ </goals>
+ <configuration>
+ <aggregateClassifier>interpolator-test</aggregateClassifier>
+ <aggregates>
+ <aggregate>
+ <includes>
+ <include>interpolated-variables.json</include>
+ </includes>
+ </aggregate>
+ </aggregates>
+ </configuration>
+ </execution>
+ <execution>
+ <id>attach</id>
+ <phase>package</phase>
+ <goals>
+ <goal>attach-features</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/src/it/variables-interpolator/src/main/features/interpolated-variables.json b/src/it/variables-interpolator/src/main/features/interpolated-variables.json
new file mode 100644
index 0000000..46e2702
--- /dev/null
+++ b/src/it/variables-interpolator/src/main/features/interpolated-variables.json
@@ -0,0 +1,21 @@
+{
+ "id": "${project.groupId}:${project.artifactId}:slingfeature:slingtest:${project.version}",
+ "title": "${project.name}",
+ "description": "${project.description}",
+ "vendor": "${project.organization.name}",
+ "variables": {
+ "interpolated_variable": "${interpolated_variable}",
+ "osgi_version": "${project.osgiVersion}"
+ },
+ "framework-properties": {
+ "sling.framework.install.incremental": "${sling.framework.install.incremental}",
+ "sling.framework.install.startlevel": "${sling.framework.install.startlevel}",
+ "sling.ignoreSystemProperties": "${sling.ignoreSystemProperties}"
+ },
+ "bundles":[
+ {
+ "id":"org.osgi:org.osgi.framework:${osgi.framework.version}",
+ "start-level":"20"
+ }
+ ]
+}
diff --git a/src/it/variables-interpolator/verify.bsh b/src/it/variables-interpolator/verify.bsh
new file mode 100644
index 0000000..a5a9568
--- /dev/null
+++ b/src/it/variables-interpolator/verify.bsh
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+import java.io.*;
+import java.util.*;
+
+import org.codehaus.plexus.util.*;
+
+ boolean check() {
+ File file = new File(basedir, "target/slingfeature-tmp/feature-slingtest.json");
+ String log = FileUtils.fileRead(file);
+
+ String[] values = {
+ "\"id\":\"org.apache.sling:slingfeature-maven-plugin-test:slingfeature:slingtest:1.0.0-SNAPSHOT\"",
+ "\"title\":\"Apache Sling Features Maven plugin test\"",
+ "\"description\":\"This is just an Apache Sling Features Maven plugin test to verify variables interpolation\"",
+ "\"vendor\":\"The Apache Software Foundation\"",
+ // TODO re-enable verification once license interpolation is fixed
+ // "\"license\":\"Apache License, Version 2.0\"",
+ "\"interpolated_variable\":\"true\"",
+ "\"sling.framework.install.incremental\":\"true\"",
+ "\"sling.framework.install.startlevel\":\"1\"",
+ "\"sling.ignoreSystemProperties\":\"true\"",
+ "\"id\":\"org.osgi:org.osgi.framework:1.9.0\""
+ };
+
+ for (String value : values) {
+ if (log.indexOf(value) < 0) {
+ System.out.println("FAILED!");
+ System.out.println("String '" + value + "' not found in file " + file);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ try {
+ return check();
+ }
+ catch(Throwable t) {
+ t.printStackTrace();
+ return false;
+ }
+
+ return true;
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 6092c1d..0d052bf 100644
--- a/src/main/java/org/apache/sling/feature/maven/Substitution.java
+++ b/src/main/java/org/apache/sling/feature/maven/Substitution.java
@@ -16,34 +16,42 @@
*/
package org.apache.sling.feature.maven;
-import java.util.Properties;
+import java.util.Collections;
+import java.util.List;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.osgi.DefaultMaven2OsgiConverter;
import org.apache.maven.shared.osgi.Maven2OsgiConverter;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
+import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
+import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
+import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
+import org.codehaus.plexus.interpolation.RecursionInterceptor;
+import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
public class Substitution {
public static String replaceMavenVars(MavenProject project, String s) {
- // There must be a better way than enumerating all these?
- s = replaceAll(s, "project.groupId", project.getGroupId());
- s = replaceAll(s, "project.artifactId", project.getArtifactId());
- s = replaceAll(s, "project.version", project.getVersion());
- s = replaceAll(s, "project.osgiVersion", getOSGiVersion(project.getVersion()));
+ RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+ project.getProperties().setProperty("project.osgiVersion", getOSGiVersion(project.getVersion()));
+ interpolator.addValueSource(new PropertiesBasedValueSource(project.getProperties()));
+ List<String> synonymPrefixes = Collections.singletonList("project.");
- Properties props = project.getProperties();
- if (props != null) {
- for (String key : props.stringPropertyNames()) {
- s = replaceAll(s, key, props.getProperty(key));
- }
- }
+ PrefixedValueSourceWrapper modelWrapper = new PrefixedValueSourceWrapper(
+ new ObjectBasedValueSource(project.getModel()),
+ synonymPrefixes,
+ true);
+ interpolator.addValueSource( modelWrapper );
- return s;
- }
+ RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor(synonymPrefixes, true);
- private static String replaceAll(String s, String key, String value) {
- return s.replaceAll("\\Q${" + key + "}\\E", value);
+ try {
+ return interpolator.interpolate(s, recursionInterceptor);
+ } catch (InterpolationException e) {
+ throw new RuntimeException("An error occurred while interpolating variables to JSON:\n" + s, e);
+ }
}
/**