You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2022/08/16 13:07:22 UTC

[sling-maven-plugin] branch master updated: SLING-11543 improve exception handling for invalid bundle files (#16)

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 dc72d15  SLING-11543 improve exception handling for invalid bundle files (#16)
dc72d15 is described below

commit dc72d15de408ea1a48b8aee1de3bdaad73787167
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue Aug 16 15:07:18 2022 +0200

    SLING-11543 improve exception handling for invalid bundle files (#16)
    
    add ITs for standalone goal usage of "install" and "install-file"
    "file-install": change order of precedence for determining the bundle
    the default bundle path is the target file generated by the current
    project
---
 src/it/install-file-test/invoker.properties        |  4 +++-
 src/it/install-file-test/pom.xml                   |  2 +-
 src/it/install-test/invoker.properties             |  5 ++++-
 .../bundlesupport/AbstractBundleInstallMojo.java   |  7 +++----
 .../maven/bundlesupport/BundleInstallFileMojo.java | 23 ++++++++++++----------
 .../maven/bundlesupport/BundleInstallMojo.java     |  4 ++--
 6 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/it/install-file-test/invoker.properties b/src/it/install-file-test/invoker.properties
index 44fca57..4c74892 100644
--- a/src/it/install-file-test/invoker.properties
+++ b/src/it/install-file-test/invoker.properties
@@ -20,4 +20,6 @@ invoker.goals.2=install -Dsling.deploy.method=WebDAV -Dsling.urlSuffix=/dav/defa
 invoker.goals.3=install -Dsling.deploy.method=SlingPostServlet -Dsling.urlSuffix=/apps/sling-it
 # invalid URL must lead to build failure
 invoker.goals.4=install -Dsling.urlSuffix=/system/console2
-invoker.buildResult.4=failure
\ No newline at end of file
+invoker.buildResult.4=failure
+# call goal alone
+invoker.goals.5=sling:install-file -Dsling.urlSuffix=/system/console -Dsling.artifact=org.apache.sling:org.apache.sling.commons.messaging:1.0.2
diff --git a/src/it/install-file-test/pom.xml b/src/it/install-file-test/pom.xml
index 645157c..0ce29b1 100644
--- a/src/it/install-file-test/pom.xml
+++ b/src/it/install-file-test/pom.xml
@@ -65,8 +65,8 @@
                         </goals>
                         <phase>install</phase>
                         <configuration>
-                            <artifactId>org.apache.sling.commons.messaging</artifactId>
                             <groupId>org.apache.sling</groupId>
+                            <artifactId>org.apache.sling.commons.messaging</artifactId>
                             <version>1.0.2</version>
                         </configuration>
                     </execution>
diff --git a/src/it/install-test/invoker.properties b/src/it/install-test/invoker.properties
index 3c0aaa5..2c9f0c7 100644
--- a/src/it/install-test/invoker.properties
+++ b/src/it/install-test/invoker.properties
@@ -20,4 +20,7 @@ invoker.goals.2=install -Dsling.deploy.method=WebDAV -Dsling.urlSuffix=/dav/defa
 invoker.goals.3=install -Dsling.deploy.method=SlingPostServlet -Dsling.urlSuffix=/apps/sling-it
 # invalid URL must lead to build failure
 invoker.goals.4=install -Dsling.urlSuffix=/system/console2
-invoker.buildResult.4=failure
\ No newline at end of file
+invoker.buildResult.4=failure
+# call goal alone
+invoker.goals.5=sling:install -Dsling.urlSuffix=/system/console
+invoker.buildResult.5=failure
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
index 9354fc3..41a99f4 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
@@ -24,6 +24,7 @@ import java.net.URI;
 
 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.apache.sling.maven.bundlesupport.deploy.BundleDeploymentMethod;
@@ -121,14 +122,12 @@ abstract class AbstractBundleInstallMojo extends AbstractBundleRequestMojo {
 
         // only upload if packaging as an osgi-bundle
         if (!bundleFile.exists()) {
-            getLog().info(bundleFile + " does not exist, not uploading");
-            return;
+            throw new MojoExecutionException("The given bundle file " + bundleFile + " does not exist!");
         }
 
         String bundleName = getBundleSymbolicName(bundleFile);
         if (bundleName == null) {
-            getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
-            return;
+            throw new MojoExecutionException("The given file " + bundleFile + " is no OSGi bundle");
         }
 
         URI targetURL = getTargetURL();
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
index 2b97cd7..4a81fc9 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
@@ -29,9 +29,9 @@ import org.eclipse.aether.artifact.DefaultArtifact;
  * Install an OSGi bundle from a given file path or Maven coordinates (resolved from the repository) to a running Sling instance.
  * One of the following parameter sets must be set to determine the bundle to install:
  * <ul>
- * <li>{@link #bundleFileName}</li>
  * <li>{@link #groupId}, {@link #artifactId}, {@link #version}, {@link #packaging} and optionally {@link #classifier}</li>
  * <li>{@link #artifact}</li>
+ * <li>{@link #bundleFileName}</li>
  * </ul>
  * 
  * To install a bundle which has been built from the current Maven project rather use goal <a href="install-mojo.html">install</a>.
@@ -42,24 +42,25 @@ public class BundleInstallFileMojo extends AbstractBundleInstallMojo {
 
     /**
      * The path of the bundle file to install.
+     * Is only effective if artifact is not determined via some other way (Maven coordinates).
      */
-    @Parameter(property="sling.file")
+    @Parameter(property="sling.file", defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
     private File bundleFileName;
 
     /**
-     * The groupId of the artifact to install
+     * The groupId of the artifact to install. Takes precedence over {@link #artifact} and {@link #bundleFileName}.
      */
     @Parameter(property="sling.groupId")
     private String groupId;
 
     /**
-     * The artifactId of the artifact to install
+     * The artifactId of the artifact to install. Takes precedence over {@link #artifact} and {@link #bundleFileName}.
      */
     @Parameter(property="sling.artifactId")
     private String artifactId;
 
     /**
-     * The version of the artifact to install
+     * The version of the artifact to install. Takes precedence over {@link #artifact} and {@link #bundleFileName}.
      */
     @Parameter(property="sling.version")
     private String version;
@@ -78,23 +79,25 @@ public class BundleInstallFileMojo extends AbstractBundleInstallMojo {
 
     /**
      * A string of the form {@code groupId:artifactId:version[:packaging[:classifier]]}.
+     * Takes precedence over {@link #bundleFileName}.
      */
     @Parameter(property="sling.artifact")
     private String artifact;
 
     @Override
     protected File getBundleFileName() throws MojoExecutionException {
-        File fileName = bundleFileName;
+        File fileName = resolveBundleFileFromArtifact();
         if (fileName == null) {
-            fileName = resolveBundleFileFromArtifact();
-            if (fileName == null) {
-                throw new MojoExecutionException("Must provide either sling.file or sling.artifact parameters");
-            }
+            fileName = bundleFileName;
+        } else {
             if (mountByFS) {
                 getLog().warn("The parameter 'mountByFS' is only supported with files outside the Maven repository and therefore ignored in this context!");
                 mountByFS = false;
             }
         }
+        if (fileName == null) {
+            throw new MojoExecutionException("Must provide either sling.file, sling.artifact or sling.groupId/sling.artifactId/sling.version parameters");
+        }
 
         return fileName;
     }
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
index 256879e..9e3488c 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
@@ -52,7 +52,7 @@ public class BundleInstallMojo extends AbstractBundleInstallMojo {
     }
 
     @Override
-    protected File getBundleFileName() {
+    protected File getBundleFileName() throws MojoExecutionException {
         File file = project.getArtifact().getFile();
         if (isBundleFile(file)) {
             return file;
@@ -65,7 +65,7 @@ public class BundleInstallMojo extends AbstractBundleInstallMojo {
                 getLog().debug("No bundle found in secondary artifact " + file);
             }
         }
-        return null;
+        throw new MojoExecutionException("No attached bundle found for this Maven project, for standalone usage use goal 'install-file' instead!");
     }
 
     private boolean isBundleFile(File file) {