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 2393a65  SLING-8683 : Slingfeature maven plugin should be usable to 
wrap project artifacts in feature
2393a65 is described below

commit 2393a65ea3dcf16b24369e6246ddacf1e5ad5985
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Thu Sep 12 08:58:36 2019 +0200

    SLING-8683 : Slingfeature maven plugin should be usable to wrap project 
artifacts in feature
---
 ...ttachArtifactMojo.java => IncludeArtifact.java} | 52 +++++++++++++++++-----
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/maven/mojos/AttachArtifactMojo.java 
b/src/main/java/org/apache/sling/feature/maven/mojos/IncludeArtifact.java
similarity index 59%
rename from 
src/main/java/org/apache/sling/feature/maven/mojos/AttachArtifactMojo.java
rename to 
src/main/java/org/apache/sling/feature/maven/mojos/IncludeArtifact.java
index 58e147f..344c14a 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AttachArtifactMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/IncludeArtifact.java
@@ -30,6 +30,10 @@ import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Artifacts;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
 import org.apache.sling.feature.maven.FeatureConstants;
@@ -38,21 +42,28 @@ import org.apache.sling.feature.maven.ProjectHelper;
 /**
  * Attach the feature as a project artifact.
  */
-@Mojo(name = "attach-artifact", defaultPhase = LifecyclePhase.PREPARE_PACKAGE, 
requiresDependencyResolution = ResolutionScope.COMPILE,
+@Mojo(name = "include-artifact", defaultPhase = 
LifecyclePhase.PREPARE_PACKAGE, requiresDependencyResolution = 
ResolutionScope.COMPILE,
       threadSafe = true
     )
-public class AttachArtifactMojo extends AbstractFeatureMojo {
+public class IncludeArtifact extends AbstractFeatureMojo {
 
     /**
-     * Classifier of the feature the current artifact is attached to.
+     * Classifier of the feature the current artifact is included in.
      */
     @Parameter
-    private String attachArtifactClassifier;
+    private String includeArtifactClassifier;
+
+    /**
+     * Name of the extension to include the artifact in. If not specified the
+     * artifact is included as a bundle.
+     */
+    @Parameter
+    private String includeArtifactExtension;
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
-        if (attachArtifactClassifier == null) {
-            throw new MojoExecutionException("attachArtifactClassifier is not 
specified. Check your configuration");
+        if (includeArtifactClassifier == null) {
+            throw new MojoExecutionException("includeArtifactClassifier is not 
specified. Check your configuration");
         }
 
         checkPreconditions();
@@ -61,7 +72,7 @@ public class AttachArtifactMojo extends AbstractFeatureMojo {
         Feature found = null;
         String key = null;
         for (final Map.Entry<String, Feature> entry : featuresMap.entrySet()) {
-            if 
(attachArtifactClassifier.equals(entry.getValue().getId().getClassifier())) {
+            if 
(includeArtifactClassifier.equals(entry.getValue().getId().getClassifier())) {
                 key = entry.getKey();
                 found = entry.getValue();
                 break;
@@ -72,15 +83,16 @@ public class AttachArtifactMojo extends AbstractFeatureMojo 
{
         File file = null;
         if (found == null) {
             found = new Feature(new ArtifactId(this.project.getGroupId(), 
this.project.getArtifactId(),
-                    this.project.getVersion(), attachArtifactClassifier, 
FeatureConstants.PACKAGING_FEATURE));
+                    this.project.getVersion(), includeArtifactClassifier, 
FeatureConstants.PACKAGING_FEATURE));
 
-            file = new File(this.getTmpDir(), "feature-" + 
this.attachArtifactClassifier + ".json");
+            file = new File(this.getTmpDir(), "feature-" + 
this.includeArtifactClassifier + ".json");
             key = file.getAbsolutePath();
             ProjectHelper.getFeatures(this.project).put(key, found);
             ProjectHelper.getAssembledFeatures(this.project).put(key, found);
         }
-        found.getBundles().add(art);
-        
ProjectHelper.getAssembledFeatures(this.project).get(key).getBundles().add(art);
+        includeArtifact(found, includeArtifactExtension, art);
+        
includeArtifact(ProjectHelper.getAssembledFeatures(this.project).get(key), 
includeArtifactExtension,
+                art.copy(art.getId()));
         if (file != null) {
             try ( final Writer writer = new FileWriter(file)) {
                 FeatureJSONWriter.write(writer, found);
@@ -89,4 +101,22 @@ public class AttachArtifactMojo extends AbstractFeatureMojo 
{
             }
         }
     }
+
+    private void includeArtifact(final Feature f, final String extensionName, 
final Artifact art)
+            throws MojoExecutionException {
+        Artifacts container = f.getBundles();
+        if (extensionName != null) {
+            Extension ext = f.getExtensions().getByName(extensionName);
+            if (ext == null) {
+                ext = new Extension(ExtensionType.ARTIFACTS, extensionName, 
ExtensionState.REQUIRED);
+                f.getExtensions().add(ext);
+            }
+            if (ext.getType() != ExtensionType.ARTIFACTS) {
+                throw new MojoExecutionException(
+                        "Wrong extension type for extension " + extensionName 
+ " : " + ext.getType());
+            }
+            container = ext.getArtifacts();
+        }
+        container.add(art);
+    }
 }

Reply via email to