This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 6af8d68  SLING-11524 Always retrieve bundle file from project's 
artifacts (#8)
6af8d68 is described below

commit 6af8d68cadc59964ceb640077be2ac513402e38d
Author: Konrad Windszus <[email protected]>
AuthorDate: Thu Aug 11 17:21:00 2022 +0200

    SLING-11524 Always retrieve bundle file from project's artifacts (#8)
---
 .../bundlesupport/AbstractBundleRequestMojo.java   | 19 +++---------
 .../maven/bundlesupport/BundleInstallMojo.java     | 35 +++++++++++++++-------
 .../maven/bundlesupport/deploy/DeployMethod.java   |  6 ++--
 3 files changed, 31 insertions(+), 29 deletions(-)

diff --git 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleRequestMojo.java
 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleRequestMojo.java
index d92083f..8203640 100644
--- 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleRequestMojo.java
+++ 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleRequestMojo.java
@@ -41,6 +41,7 @@ import org.apache.hc.core5.util.Timeout;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.osgi.framework.Constants;
 
 abstract class AbstractBundleRequestMojo extends AbstractMojo {
 
@@ -117,9 +118,7 @@ abstract class AbstractBundleRequestMojo extends 
AbstractMojo {
             return null;
         }
 
-        JarFile jaf = null;
-        try {
-            jaf = new JarFile(jarFile);
+        try (JarFile jaf = new JarFile(jarFile)) {
             Manifest manif = jaf.getManifest();
             if (manif == null) {
                 getLog().debug(
@@ -127,8 +126,7 @@ abstract class AbstractBundleRequestMojo extends 
AbstractMojo {
                 return null;
             }
 
-            String symbName = manif.getMainAttributes().getValue(
-                "Bundle-SymbolicName");
+            String symbName = 
manif.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
             if (symbName == null) {
                 getLog().debug(
                     "getBundleSymbolicName: No Bundle-SymbolicName in "
@@ -140,16 +138,7 @@ abstract class AbstractBundleRequestMojo extends 
AbstractMojo {
         } catch (IOException ioe) {
             getLog().warn("getBundleSymbolicName: Problem checking " + jarFile,
                 ioe);
-        } finally {
-            if (jaf != null) {
-                try {
-                    jaf.close();
-                } catch (IOException ignore) {
-                    // don't care
-                }
-            }
-        }
-
+        } 
         // fall back to not being a bundle
         return null;
     }
diff --git 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
index 6ecc098..2a27d41 100644
--- 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
+++ 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
@@ -19,14 +19,17 @@ package org.apache.sling.maven.bundlesupport;
 
 import java.io.File;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 
 /**
- * Install a local OSGi bundle file to a running Sling instance.
+ * Install the project's artifact to a running Sling instance (in case it is 
an OSGi bundle).
+ * It uses the first valid OSGi bundle file for deployment from the primary 
artifact and all secondary ones.
  * For details refer to <a href="bundle-installation.html">Bundle 
Installation</a>.
+ * To install an arbitrary bundle not attached to the current Maven project 
use goal <a href="install-file-mojo.html">install-file</a>.
  */
 @Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL)
 public class BundleInstallMojo extends AbstractBundleInstallMojo {
@@ -37,12 +40,6 @@ public class BundleInstallMojo extends 
AbstractBundleInstallMojo {
      */
     @Parameter(property = "sling.install.skip", defaultValue = "false", 
required = true)
     private boolean skip;
-    
-    /**
-     * The path of the bundle file to install.
-     */
-    @Parameter(property = "sling.file", defaultValue = 
"${project.build.directory}/${project.build.finalName}.jar", required = true)
-    private File bundleFileName;
 
     @Override
     public void execute() throws MojoExecutionException {
@@ -51,12 +48,30 @@ public class BundleInstallMojo extends 
AbstractBundleInstallMojo {
             getLog().debug("Skipping bundle installation as instructed");
             return;
         }
-
         super.execute();
     }
-    
+
     @Override
     protected File getBundleFileName() {
-        return bundleFileName;
+        File file = project.getArtifact().getFile();
+        if (isBundleFile(file)) {
+            return file;
+        } else {
+            getLog().debug("No bundle found in primary artifact " + file + ", 
checking secondary ones...");
+            for (Artifact artifact : project.getAttachedArtifacts()) {
+                if (isBundleFile(artifact.getFile())) {
+                    return file;
+                }
+                getLog().debug("No bundle found in secondary artifact " + 
file);
+            }
+        }
+        return null;
+    }
+
+    private boolean isBundleFile(File file) {
+        if (file == null) {
+            return false;
+        }
+        return getBundleSymbolicName(file) != null;
     }
 }
\ No newline at end of file
diff --git 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
index 31a8689..36329ba 100644
--- 
a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
+++ 
b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
@@ -22,8 +22,6 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 
-import org.apache.maven.plugin.MojoExecutionException;
-
 /**
  * Deploys/installs and undeploys/uninstalls bundles on a Sling instance.
  */
@@ -35,7 +33,7 @@ public interface DeployMethod {
      * @param file Bundle file
      * @param bundleSymbolicName Bundle symbolic name
      * @param context Deploy context parameters
-     * @throws MojoExecutionException Mojo execution execution
+     * @throws IOException in case of failure
      */
     void deploy(URI targetURL, File file, String bundleSymbolicName, 
DeployContext context) throws IOException;
     
@@ -45,7 +43,7 @@ public interface DeployMethod {
      * @param file Bundle file
      * @param bundleSymbolicName Bundle symbolic name
      * @param context Deploy context parameters
-     * @throws MojoExecutionException Mojo execution execution
+     * @throws IOException in case of failure
      */
     void undeploy(URI targetURL, File file, String bundleSymbolicName, 
DeployContext context) throws IOException;
     

Reply via email to