You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/07/05 07:42:55 UTC
[sling-slingfeature-maven-plugin] branch master updated: Support
replacement of pom variables in feature model files.
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 eeb4bf2 Support replacement of pom variables in feature model files.
eeb4bf2 is described below
commit eeb4bf2994b68f9c4b9e507a72965a44a513f937
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Thu Jul 5 08:41:32 2018 +0100
Support replacement of pom variables in feature model files.
Variables supported: ${project.groupId}, ${project.artifactId} and
${project.version}
---
.../feature/maven/mojos/GenerateResources.java | 87 ++++++++++++++++++
.../META-INF/m2e/lifecycle-mapping-metadata.xml | 1 +
.../feature/maven/mojos/GenerateResourceTest.java | 102 +++++++++++++++++++++
src/test/resources/generate-resources/test1.json | 1 +
src/test/resources/generate-resources/test1.txt | 1 +
5 files changed, 192 insertions(+)
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/GenerateResources.java b/src/main/java/org/apache/sling/feature/maven/mojos/GenerateResources.java
new file mode 100644
index 0000000..ca78cbd
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/GenerateResources.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sling.feature.maven.mojos;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.MavenProject;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+
+@Mojo(
+ name = "generate-resources",
+ defaultPhase = LifecyclePhase.GENERATE_RESOURCES,
+ requiresDependencyResolution = ResolutionScope.TEST,
+ threadSafe = true)
+public class GenerateResources extends AbstractFeatureMojo {
+ @Parameter(defaultValue="${basedir}/src/main/features")
+ private File featuresDirectory;
+
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ File[] files = featuresDirectory.listFiles();
+ if (files == null)
+ return;
+
+ File processedFeaturesDir = new File(project.getBuild().getDirectory(), "features/processed");
+ processedFeaturesDir.mkdirs();
+
+ for (File f : files) {
+ if (!f.getName().endsWith(".json")) {
+ continue;
+ }
+
+ try {
+ substituteVars(project, f, processedFeaturesDir);
+ } catch (IOException e) {
+ throw new MojoExecutionException("Problem processing feature file " + f.getAbsolutePath(), e);
+ }
+ }
+ }
+
+ private static File substituteVars(MavenProject project, File f, File processedFeaturesDir) throws IOException {
+ File file = new File(processedFeaturesDir, f.getName());
+
+ if (file.exists() && file.lastModified() > f.lastModified()) {
+ // The file already exists, so we don't need to write it again
+ return file;
+ }
+
+ try (FileWriter fw = new FileWriter(file)) {
+ for (String s : Files.readAllLines(f.toPath())) {
+ fw.write(replaceVars(project, s));
+ fw.write(System.getProperty("line.separator"));
+ }
+ }
+ return file;
+ }
+
+ static String replaceVars(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());
+ return s;
+ }
+}
diff --git a/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
index 0f2b325..f316c03 100644
--- a/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
+++ b/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
@@ -21,6 +21,7 @@
<pluginExecution>
<pluginExecutionFilter>
<goals>
+ <goal>process-resources</goal>
<goal>attach-feature</goal>
</goals>
</pluginExecutionFilter>
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
new file mode 100644
index 0000000..0bc7503
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/GenerateResourceTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.sling.feature.maven.mojos;
+
+import org.apache.maven.model.Build;
+import org.apache.maven.project.MavenProject;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Comparator;
+
+import static org.junit.Assert.assertEquals;
+
+public class GenerateResourceTest {
+ @Test
+ public void testExecute() throws Exception {
+ File srcDir = new File(getClass().getResource("/generate-resources/test1.json").toURI()).getParentFile();
+
+ Build build = new Build();
+
+ MavenProject project = new MavenProject();
+ project.setBuild(build);
+ project.setGroupId("gid");
+ project.setArtifactId("aid");
+ project.setVersion("1.2.3-SNAPSHOT");
+
+ GenerateResources gr = new GenerateResources();
+ setPrivateField(gr, "featuresDirectory", srcDir);
+ setPrivateField(gr, "project", project);
+
+ Path tempDir = Files.createTempDirectory("grtest");
+ build.setDirectory(tempDir.toString());
+
+ gr.execute();
+
+ try {
+ File[] files = new File(tempDir.toFile(), "features/processed")
+ .listFiles((d, n) -> n.endsWith(".json"));
+ assertEquals(1, files.length);
+ byte[] bytes = Files.readAllBytes(files[0].toPath());
+ String s = new String(bytes).trim();
+ assertEquals("\"---gid---aid---aid---1.2.3-SNAPSHOT---\"", s);
+ } finally {
+ Files.walk(tempDir)
+ .sorted(Comparator.reverseOrder())
+ .map(Path::toFile)
+ .forEach(File::delete);
+ }
+ }
+
+ @Test
+ public void testExecuteWithoutFeatureFiles() throws Exception {
+ GenerateResources gr = new GenerateResources();
+ setPrivateField(gr, "featuresDirectory", new File("nonexistent"));
+
+ // Should return gracefully
+ gr.execute();
+ }
+
+ @Test
+ public void testReplaceVars() {
+ MavenProject mp = Mockito.mock(MavenProject.class);
+
+ Mockito.when(mp.getGroupId()).thenReturn("abc");
+ Mockito.when(mp.getArtifactId()).thenReturn("a.b.c");
+ Mockito.when(mp.getVersion()).thenReturn("1.2.3-SNAPSHOT");
+
+ assertEquals("xxxabcyyy", GenerateResources.replaceVars(mp,
+ "xxx${project.groupId}yyy"));
+ assertEquals("xxxabcyyya.b.c1.2.3-SNAPSHOT", GenerateResources.replaceVars(mp,
+ "xxx${project.groupId}yyy${project.artifactId}${project.version}"));
+ }
+
+ private void setPrivateField(Object obj, String fieldName, Object value) throws Exception {
+ Field f;
+ try {
+ f = obj.getClass().getDeclaredField(fieldName);
+ } catch (Exception e) {
+ f = obj.getClass().getSuperclass().getDeclaredField(fieldName);
+ }
+ f.setAccessible(true);
+ f.set(obj, value);
+ }
+}
diff --git a/src/test/resources/generate-resources/test1.json b/src/test/resources/generate-resources/test1.json
new file mode 100644
index 0000000..744c8c5
--- /dev/null
+++ b/src/test/resources/generate-resources/test1.json
@@ -0,0 +1 @@
+"---${project.groupId}---${project.artifactId}---${project.artifactId}---${project.version}---"
\ No newline at end of file
diff --git a/src/test/resources/generate-resources/test1.txt b/src/test/resources/generate-resources/test1.txt
new file mode 100644
index 0000000..744c8c5
--- /dev/null
+++ b/src/test/resources/generate-resources/test1.txt
@@ -0,0 +1 @@
+"---${project.groupId}---${project.artifactId}---${project.artifactId}---${project.version}---"
\ No newline at end of file