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 403e48f  SLING-9095 : Create a mojo that is able to create a feature 
archive
403e48f is described below

commit 403e48f3e62659a8461f4b979e3b1d980431b62d
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Sat Feb 22 09:24:52 2020 +0100

    SLING-9095 : Create a mojo that is able to create a feature archive
---
 .../apache/sling/feature/maven/mojos/Archive.java  | 39 ++++++++++++++++
 ...iveMojo.java => AttachFeatureArchivesMojo.java} | 52 +++++++++++++++-------
 2 files changed, 74 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/Archive.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/Archive.java
new file mode 100644
index 0000000..5daf782
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/Archive.java
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+/**
+ * Several features can be selected to be put into a single archive
+ */
+public class Archive extends FeatureSelectionConfig {
+
+    private String classifier;
+
+    public String getClassifier() {
+        return classifier;
+    }
+
+    public void setClassifier(String classifier) {
+        this.classifier = classifier;
+    }
+
+    @Override
+    public String toString() {
+        return "Archive [classifier=" + classifier + ", getFilesExcludes()=" + 
getFilesExcludes() + ", getSelections()="
+                + getSelections() + "]";
+    }
+}
\ No newline at end of file
diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchiveMojo.java
 
b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchivesMojo.java
similarity index 78%
rename from 
src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchiveMojo.java
rename to 
src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchivesMojo.java
index 9e2b503..2c8b65e 100644
--- 
a/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchiveMojo.java
+++ 
b/src/main/java/org/apache/sling/feature/maven/mojos/AttachFeatureArchivesMojo.java
@@ -22,7 +22,8 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
@@ -33,13 +34,15 @@ 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.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
-import org.apache.sling.feature.builder.ArtifactProvider;
 import org.apache.sling.feature.io.archive.ArchiveWriter;
 import org.apache.sling.feature.maven.ProjectHelper;
 
+import edu.emory.mathcs.backport.java.util.Collections;
+
 /**
  * Create a feature model archive and attach it. An archive is created for each
  * feature of this project
@@ -50,16 +53,31 @@ import org.apache.sling.feature.maven.ProjectHelper;
         requiresDependencyResolution = ResolutionScope.TEST,
         threadSafe = true
     )
-public class AttachFeatureArchiveMojo extends AbstractFeatureMojo {
+public class AttachFeatureArchivesMojo extends AbstractIncludingFeatureMojo {
 
     private static final String EXTENSION = "zip";
 
     private static final String CLASSIFIER = "far";
 
+    @Parameter
+    private List<Archive> archives;
+
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         checkPreconditions();
-        this.attachArchives(ProjectHelper.getFeatures(this.project));
+        if (archives == null || archives.size() == 0) {
+            // by default create an archive of each feature individually
+            this.attachArchives(ProjectHelper.getFeatures(this.project));
+        } else {
+            for (final Archive archive : archives) {
+                if (archive.getClassifier() == null) {
+                    throw new MojoExecutionException("Classifier is missing 
for archive.");
+                }
+                final List<Feature> features = new ArrayList<>();
+                features.addAll(this.getSelectedFeatures(archive).values());
+                this.attachArchives(features, archive.getClassifier());
+            }
+        }
     }
 
     /**
@@ -77,7 +95,9 @@ public class AttachFeatureArchiveMojo extends 
AbstractFeatureMojo {
             }
 
             if (add) {
-                attachArchive(entry.getValue());
+                final String classifier = 
entry.getValue().getId().getClassifier() == null ? CLASSIFIER
+                        : 
entry.getValue().getId().getClassifier().concat(CLASSIFIER);
+                attachArchives(Collections.singletonList(entry.getValue()), 
classifier);
             }
         }
     }
@@ -115,20 +135,20 @@ public class AttachFeatureArchiveMojo extends 
AbstractFeatureMojo {
             mf.getMainAttributes().putValue("Specification-Vendor", 
feature.getVendor());
         }
 
-        mf.getMainAttributes().putValue("Implementation-Vendor-Id", 
feature.getId().getGroupId());
-        if (feature.getTitle() != null) {
+        mf.getMainAttributes().putValue("Implementation-Vendor-Id", 
project.getGroupId());
+        if (feature != null && feature.getTitle() != null) {
             mf.getMainAttributes().putValue("Implementation-Title", 
feature.getTitle());
             mf.getMainAttributes().putValue("Specification-Title", 
feature.getTitle());
+        } else if (project.getName() != null) {
+            mf.getMainAttributes().putValue("Implementation-Title", 
project.getName());
+            mf.getMainAttributes().putValue("Specification-Title", 
project.getName());
         }
 
         return mf;
     }
 
-    private void attachArchive(final Feature feature) throws 
MojoExecutionException {
-        final String classifier = feature.getId().getClassifier() == null ? 
CLASSIFIER
-                : feature.getId().getClassifier().concat(CLASSIFIER);
-
-        final ArtifactId archiveId = 
feature.getId().changeClassifier(classifier).changeType(EXTENSION);
+    private void attachArchives(final List<Feature> features, final String 
classifier) throws MojoExecutionException {
+        final ArtifactId archiveId = 
features.get(0).getId().changeClassifier(classifier).changeType(EXTENSION);
 
         // write the feature model archive
         final File outputFile = new File(
@@ -136,11 +156,9 @@ public class AttachFeatureArchiveMojo extends 
AbstractFeatureMojo {
         outputFile.getParentFile().mkdirs();
 
         try ( final FileOutputStream fos = new FileOutputStream(outputFile)) {
-            final JarOutputStream jos = ArchiveWriter.write(fos, feature, 
createBaseManifest(feature),
-                    new ArtifactProvider() {
+            final JarOutputStream jos = ArchiveWriter.write(fos,
+                    createBaseManifest(features.size() == 1 ? features.get(0) 
: null), id -> {
 
-                @Override
-                public URL provide(final ArtifactId id) {
                     try {
                         return ProjectHelper.getOrResolveArtifact(project, 
mavenSession, artifactHandlerManager,
                                 artifactResolver, 
id).getFile().toURI().toURL();
@@ -150,7 +168,7 @@ public class AttachFeatureArchiveMojo extends 
AbstractFeatureMojo {
                         return null;
                     }
                 }
-            });
+                    , features.toArray(new Feature[features.size()]));
 
             // handle license etc.
             jos.setLevel(Deflater.BEST_COMPRESSION);

Reply via email to