You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2019/06/05 17:44:35 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-8472 : Provide an option to create a reference file with all attached features

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 be07376  SLING-8472 : Provide an option to create a reference file with all attached features
be07376 is described below

commit be07376c412184f695f1bd97075676592af0068d
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Jun 5 19:44:25 2019 +0200

    SLING-8472 : Provide an option to create a reference file with all attached features
---
 .../feature/maven/mojos/AttachFeaturesMojo.java    | 52 ++++++++++++++++++++--
 .../maven/mojos/AttachFeaturesMojoTest.java        |  2 +-
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
index b217c34..2296bda 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojo.java
@@ -20,7 +20,9 @@ import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -29,6 +31,7 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.IOUtils;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
 import org.apache.sling.feature.maven.FeatureConstants;
 import org.apache.sling.feature.maven.ProjectHelper;
@@ -49,6 +52,18 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo {
             defaultValue = "false")
     private boolean attachTestFeatures;
 
+    /**
+     * Create reference file
+     */
+    @Parameter(name = "createReferenceFile", defaultValue = "false")
+    private boolean createReferenceFile;
+
+    /**
+     * Classifier for the reference file (if any)
+     */
+    @Parameter(name = "referenceFileClassifier")
+    private String referenceFileClassifier;
+
     private void attach(final Feature feature,
             final String classifier)
     throws MojoExecutionException {
@@ -76,10 +91,39 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo {
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         ProjectHelper.checkPreprocessorRun(this.project);
-        this.attachClassifierFeatures(ProjectHelper.getFeatures(this.project).values());
+        final List<String> featureUrls = new ArrayList<>();
+        this.attachClassifierFeatures(ProjectHelper.getFeatures(this.project).values(), featureUrls);
 
         if ( this.attachTestFeatures ) {
-        	this.attachClassifierFeatures(ProjectHelper.getTestFeatures(this.project).values());
+            this.attachClassifierFeatures(ProjectHelper.getTestFeatures(this.project).values(), featureUrls);
+        }
+        if (this.createReferenceFile) {
+            this.createReferenceFile(featureUrls);
+        }
+    }
+
+    private void createReferenceFile(final List<String> featureUrls) throws MojoExecutionException {
+        if (featureUrls.isEmpty()) {
+            getLog().warn(
+                    "Create reference file is enabled but no features are attached. Skipping reference file generation.");
+        } else {
+            String fileName = "references";
+            if (this.referenceFileClassifier != null) {
+                fileName = fileName.concat("-").concat(this.referenceFileClassifier);
+            }
+            fileName = fileName.concat(IOUtils.EXTENSION_REF_FILE);
+            final File outputFile = new File(this.getTmpDir(), fileName);
+            outputFile.getParentFile().mkdirs();
+
+            try (final Writer w = new FileWriter(outputFile)) {
+                for (final String url : featureUrls) {
+                    w.write(url);
+                    w.write('\n');
+                }
+            } catch (final IOException e) {
+                throw new MojoExecutionException("Unable to write feature reference file to " + outputFile, e);
+            }
+            projectHelper.attachArtifact(project, "ref", this.referenceFileClassifier, outputFile);
         }
     }
 
@@ -87,9 +131,11 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo {
      * Attach all features
      * @throws MojoExecutionException
      */
-    void attachClassifierFeatures(final Collection<Feature> features) throws MojoExecutionException {
+    void attachClassifierFeatures(final Collection<Feature> features, final List<String> featureUrls)
+            throws MojoExecutionException {
         for (final Feature f : features) {
             attach(f, f.getId().getClassifier());
+            featureUrls.add(f.getId().toMvnUrl());
         }
     }
 }
diff --git a/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java b/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
index 28a621b..b6f8017 100644
--- a/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
+++ b/src/test/java/org/apache/sling/feature/maven/mojos/AttachFeaturesMojoTest.java
@@ -61,7 +61,7 @@ public class AttachFeaturesMojoTest {
         MavenProjectHelper helper = Mockito.mock(MavenProjectHelper.class);
         af.projectHelper = helper;
 
-        af.attachClassifierFeatures(features);
+        af.attachClassifierFeatures(features, new ArrayList<>());
         Mockito.verify(helper).attachArtifact(project, FeatureConstants.PACKAGING_FEATURE, "testa", new File(featuresDir, "slingfeature-tmp" + File.separatorChar + "feature-testa.json"));
         Mockito.verify(helper).attachArtifact(project, FeatureConstants.PACKAGING_FEATURE, "testd", new File(featuresDir, "slingfeature-tmp" + File.separatorChar + "feature-testd.json"));
         Mockito.verifyNoMoreInteractions(helper);