This is an automated email from the ASF dual-hosted git repository.
davidb 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 47f028c Support for maven property substitution.
47f028c is described below
commit 47f028c9d1da9d7edb714860873cfc31db3e0197
Author: David Bosschaert <[email protected]>
AuthorDate: Mon Sep 24 08:51:49 2018 +0200
Support for maven property substitution.
---
.../sling/feature/maven/mojos/Substitution.java | 20 +++++++++++++++++---
.../feature/maven/mojos/GenerateResourceTest.java | 6 ++++++
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git
a/src/main/java/org/apache/sling/feature/maven/mojos/Substitution.java
b/src/main/java/org/apache/sling/feature/maven/mojos/Substitution.java
index 6d5d8f2..a9a0ef2 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/Substitution.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/Substitution.java
@@ -22,6 +22,7 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
+import java.util.Properties;
class Substitution {
static File substituteMavenVars(MavenProject project, File f, File
processedFeaturesDir) throws IOException {
@@ -43,9 +44,22 @@ class Substitution {
static String replaceMavenVars(MavenProject project, String s) {
// There must be a better way than enumerating all these?
- s = s.replaceAll("\\Q${project.groupId}\\E", project.getGroupId());
- s = s.replaceAll("\\Q${project.artifactId}\\E",
project.getArtifactId());
- s = s.replaceAll("\\Q${project.version}\\E", project.getVersion());
+ s = replaceAll(s, "project.groupId", project.getGroupId());
+ s = replaceAll(s, "project.artifactId", project.getArtifactId());
+ s = replaceAll(s, "project.version", project.getVersion());
+
+
+ Properties props = project.getProperties();
+ if (props != null) {
+ for (String key : props.stringPropertyNames()) {
+ s = replaceAll(s, key, props.getProperty(key));
+ }
+ }
+
return s;
}
+
+ private static String replaceAll(String s, String key, String value) {
+ return s.replaceAll("\\Q${" + key + "}\\E", value);
+ }
}
diff --git
a/src/test/java/org/apache/sling/feature/maven/mojos/GenerateResourceTest.java
b/src/test/java/org/apache/sling/feature/maven/mojos/GenerateResourceTest.java
index 56d44ee..1cfa1ba 100644
---
a/src/test/java/org/apache/sling/feature/maven/mojos/GenerateResourceTest.java
+++
b/src/test/java/org/apache/sling/feature/maven/mojos/GenerateResourceTest.java
@@ -26,6 +26,7 @@ import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
+import java.util.Properties;
import static org.junit.Assert.assertEquals;
@@ -79,14 +80,19 @@ public class GenerateResourceTest {
public void testReplaceVars() {
MavenProject mp = Mockito.mock(MavenProject.class);
+ Properties props = new Properties();
+ props.put("foo", "bar");
+
Mockito.when(mp.getGroupId()).thenReturn("abc");
Mockito.when(mp.getArtifactId()).thenReturn("a.b.c");
Mockito.when(mp.getVersion()).thenReturn("1.2.3-SNAPSHOT");
+ Mockito.when(mp.getProperties()).thenReturn(props);
assertEquals("xxxabcyyy", Substitution.replaceMavenVars(mp,
"xxx${project.groupId}yyy"));
assertEquals("xxxabcyyya.b.c1.2.3-SNAPSHOT",
Substitution.replaceMavenVars(mp,
"xxx${project.groupId}yyy${project.artifactId}${project.version}"));
+ assertEquals("xxxbaryyy", Substitution.replaceMavenVars(mp,
"xxx${foo}yyy"));
}
private void setPrivateField(Object obj, String fieldName, Object value)
throws Exception {