SLING-11524 Always retrieve bundle file from project's artifacts (#8)

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.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 @@
             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 @@
                 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 @@
         } 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 @@
 
 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 @@
      */
     @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 @@
             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.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 @@
      * @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 @@
      * @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;