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 2021/12/15 14:18:03 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-11003 : Provide option to store bundle symbolic name and version in metadata

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 3ec210e  SLING-11003 : Provide option to store bundle symbolic name and version in metadata
3ec210e is described below

commit 3ec210e4a5fff38a392d6c8357453d6a9d59f0ff
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Dec 15 15:17:53 2021 +0100

    SLING-11003 : Provide option to store bundle symbolic name and version in metadata
---
 pom.xml                                            | 12 +++--
 .../feature/maven/mojos/AttachFeaturesMojo.java    | 57 +++++++++++++++++++++-
 2 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index b311fc6..ceb2426 100644
--- a/pom.xml
+++ b/pom.xml
@@ -173,7 +173,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.analyser</artifactId>
-            <version>1.4.0</version>
+            <version>1.5.2</version>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -183,13 +183,19 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.feature.extension.apiregions</artifactId>
-            <version>1.4.0</version>
+            <version>1.4.2</version>
         </dependency>
         <!-- aux dependencies for Content-Package check -->
         <dependency>
             <groupId>org.apache.jackrabbit.vault</groupId>
             <artifactId>org.apache.jackrabbit.vault</artifactId>
-            <version>3.2.6</version>
+            <version>3.5.4</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.jackrabbit.vault</groupId>
+            <artifactId>vault-validation</artifactId>
+            <version>3.5.4</version>
             <scope>runtime</scope>
         </dependency>
         <dependency>
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 59cc1c7..12cfa23 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,9 +20,12 @@ import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.jar.JarFile;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -30,10 +33,12 @@ 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.Artifact;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.io.IOUtils;
 import org.apache.sling.feature.maven.FeatureConstants;
 import org.apache.sling.feature.maven.ProjectHelper;
+import org.osgi.framework.Constants;
 
 /**
  * Attach the feature as a project artifact.
@@ -69,11 +74,61 @@ public class AttachFeaturesMojo extends AbstractFeatureMojo {
     @Parameter(name = "referenceFileClassifier")
     private String referenceFileClassifier;
 
+    /**
+     * Include bundle metadata
+     * @since 1.6.0
+     */
+    @Parameter(name = "includeBundleMetadata", defaultValue = "false")
+    private boolean includeBundleMetadata;
+
+    /** Shared metadata cache */
+    private static final Map<String, Map.Entry<String, String>> METADATA_CACHE = new ConcurrentHashMap<>();
+
+    /** Not found entry */
+    private static final Map.Entry<String, String> NOT_FOUND = new AbstractMap.SimpleImmutableEntry<>("NULL", "NULL");
+
     private void attach(final Feature feature)
     throws MojoExecutionException {
         final String classifier = feature.getId().getClassifier();
+
+        boolean changed = false;
+        // check for metadata
+        if ( this.includeBundleMetadata ) {
+            for(final Artifact bundle : feature.getBundles()) {
+                if ( bundle.getMetadata().get(Constants.BUNDLE_SYMBOLICNAME) == null ) {
+                     Map.Entry<String, String> value = METADATA_CACHE.get(bundle.getId().toMvnId());
+                     if ( value == null ) {
+                        final org.apache.maven.artifact.Artifact source = ProjectHelper.getOrResolveArtifact(this.project,
+                             this.mavenSession,
+                            this.artifactHandlerManager,
+                            this.artifactResolver,
+                            bundle.getId());
+        
+                        try (final JarFile jarFile = new JarFile(source.getFile())) {
+                            final String symbolicName = jarFile.getManifest().getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+                            final String version = jarFile.getManifest().getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+                            if ( symbolicName != null && version != null ) {
+                                value = new AbstractMap.SimpleImmutableEntry<>(symbolicName, version);
+                            }
+                        } catch (final IOException e) {
+                            // we ignore this
+                        }
+                        if ( value == null ) {
+                            value = NOT_FOUND;
+                        }
+                        METADATA_CACHE.put(bundle.getId().toMvnId(), value);
+                     }
+                     if ( value != NOT_FOUND ) {
+                         bundle.getMetadata().put(Constants.BUNDLE_SYMBOLICNAME, value.getKey());
+                         bundle.getMetadata().put(Constants.BUNDLE_VERSION, value.getValue());
+                         changed = true;
+                     }
+                }
+            }
+        }
+
         // write the feature
-        final File outputFile = ProjectHelper.createTmpFeatureFile(project, feature);
+        final File outputFile = ProjectHelper.createTmpFeatureFile(project, feature, changed);
 
         // if this project is a feature, it's the main artifact
         if ( project.getPackaging().equals(FeatureConstants.PACKAGING_FEATURE)