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/12 13:57:53 UTC

[sling-maven-plugin] 01/01: SLING-11534 allow to parameterize "uninstall" with BSN or file/resource name directly

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

kwin pushed a commit to branch feature/uninstall-via-bsn-parameter
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git

commit 5138eab6707abc4c531daf49658ba06907517c36
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Aug 12 10:14:28 2022 +0200

    SLING-11534 allow to parameterize "uninstall" with BSN or file/resource
    name directly
    
    add ITs for "install-file" and "uninstall"
    fix uninstall with WebDAV
---
 pom.xml                                            |  1 +
 src/it/install-file-test/invoker.properties        | 23 ++++++
 src/it/install-file-test/pom.xml                   | 85 ++++++++++++++++++++++
 .../bundlesupport/AbstractBundleInstallMojo.java   |  7 +-
 .../maven/bundlesupport/BundleUninstallMojo.java   | 45 +++++++++---
 .../sling/maven/bundlesupport/FsUnMountMojo.java   |  2 +-
 .../maven/bundlesupport/deploy/DeployMethod.java   |  7 +-
 .../deploy/method/FelixPostDeployMethod.java       |  4 +-
 .../deploy/method/SlingPostDeployMethod.java       |  4 +-
 .../deploy/method/WebDavPutDeployMethod.java       | 14 ++--
 .../fsresource/SlingInitialContentMounter.java     |  3 +-
 11 files changed, 168 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index b5cda59..8d3d7d2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -56,6 +56,7 @@
         <mavenVersion>3.3.1</mavenVersion><!-- minimum target Maven version this plugin is compatible with -->
         <maven.compiler.target>${sling.java.version}</maven.compiler.target><!-- also set target next to release due to https://issues.apache.org/jira/browse/MPLUGIN-404 -->
         <starter-its.startTimeoutSeconds>60</starter-its.startTimeoutSeconds><!-- the time in seconds to wait for Sling Starter Feature to be started for the ITs -->
+        <project.build.outputTimestamp>10</project.build.outputTimestamp>
     </properties>
 
     <build>
diff --git a/src/it/install-file-test/invoker.properties b/src/it/install-file-test/invoker.properties
new file mode 100644
index 0000000..44fca57
--- /dev/null
+++ b/src/it/install-file-test/invoker.properties
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals=install -Dsling.urlSuffix=/system/console -Dsling.bundle.name=org.apache.sling.commons.messaging
+invoker.goals.2=install -Dsling.deploy.method=WebDAV -Dsling.urlSuffix=/dav/default/apps/sling-it
+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
diff --git a/src/it/install-file-test/pom.xml b/src/it/install-file-test/pom.xml
new file mode 100644
index 0000000..645157c
--- /dev/null
+++ b/src/it/install-file-test/pom.xml
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership. The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied. See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>example-pom</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+    <packaging>pom</packaging>
+    
+    <properties>
+        <!-- this is either file name or BSN, for BSN overwrite via CLI params -->
+        <sling.bundle.name>org.apache.sling.commons.messaging-1.0.2.jar</sling.bundle.name>
+    </properties>
+    <build>
+        <pluginManagement>
+            <plugins>
+                <plugin>
+                    <artifactId>maven-resources-plugin</artifactId>
+                    <version>3.3.0</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-compiler-plugin</artifactId>
+                    <version>3.10.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-surefire-plugin</artifactId>
+                    <version>2.22.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-install-plugin</artifactId>
+                    <version>3.0.1</version>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+        <plugins>
+            <plugin>
+                <groupId>@project.groupId@</groupId>
+                <artifactId>@project.artifactId@</artifactId>
+                <version>@project.version@</version>
+                <executions>
+                    <execution>
+                        <id>bundle-install</id>
+                        <goals>
+                            <goal>install-file</goal>
+                        </goals>
+                        <phase>install</phase>
+                        <configuration>
+                            <artifactId>org.apache.sling.commons.messaging</artifactId>
+                            <groupId>org.apache.sling</groupId>
+                            <version>1.0.2</version>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>bundle-uninstall</id>
+                        <goals>
+                            <goal>uninstall</goal>
+                        </goals>
+                        <phase>install</phase>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
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 40ea058..cf92954 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleInstallMojo.java
@@ -91,11 +91,12 @@ abstract class AbstractBundleInstallMojo extends AbstractBundleRequestMojo {
     private boolean refreshPackages;
 
     /**
-     * Whether to add the mapping for the
-     * <a href="https://sling.apache.org/documentation/bundles/accessing-filesystem-resources-extensions-fsresource.html">Apache Sling File System Resource Provider</a>.
+     * Whether to add (for install)/remove (for uninstall) the mapping for the
+     * <a href="https://sling.apache.org/documentation/bundles/accessing-filesystem-resources-extensions-fsresource.html">Apache Sling File System Resource Provider</a>
+     * for the <a href="https://sling.apache.org/documentation/bundles/content-loading-jcr-contentloader.html#initial-content-loading">bundle's initial content</a>.
      */
     @Parameter(property="sling.mountByFS", defaultValue = "false", required = true)
-    private boolean mountByFS;
+    boolean mountByFS;
 
     /**
      * The Maven project.
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java
index f550e5d..e27ccbc 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleUninstallMojo.java
@@ -43,11 +43,23 @@ import org.apache.sling.maven.bundlesupport.fsresource.SlingInitialContentMounte
 public class BundleUninstallMojo extends AbstractBundleInstallMojo {
 
     /**
-     * The name of the generated JAR file.
+     * The path of bundle file to uninstall.
+     * The file is only used to determine the file name or bundle symbolic name.
+     * This parameter is only effective if {@link #bundleName} is not set.
+     * @deprecated Use {@link #bundleName} instead
      */
+    @Deprecated
     @Parameter(property = "sling.file", defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
     private File bundleFileName;
 
+    /**
+     * The bundles's file/resource name without path (for all {@link #deploymentMethod}s except for {@code WebConsole}) or
+     * its symbolic name.
+     * If this parameter is set, it takes precedence over {@link #bundleFileName}.
+     */
+    @Parameter(property = "sling.bundle.name")
+    private String bundleName;
+
     @Override
     protected File getBundleFileName() {
         return bundleFileName;
@@ -58,25 +70,36 @@ public class BundleUninstallMojo extends AbstractBundleInstallMojo {
      */
     @Override
     public void execute() throws MojoExecutionException {
-        // only uninstall if packaging as an osgi-bundle
-        final File bundleFile = getBundleFileName();
-        final String bundleName = getBundleSymbolicName(bundleFile);
-        if (bundleName == null) {
-            getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
-            return;
+        final String bundleName;
+        if (this.bundleName == null) {
+            // only uninstall if file is really an OSGi bundle
+            final File bundleFile = getBundleFileName();
+            String bundleSymbolicName = getBundleSymbolicName(bundleFile);
+            if (bundleSymbolicName == null) {
+                getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
+                return;
+            }
+            if (getDeploymentMethod() != BundleDeploymentMethod.WebConsole) {
+                bundleName = bundleFile.getName();
+            } else {
+                bundleName = bundleSymbolicName;
+            }
+        } else {
+            bundleName = this.bundleName;
         }
 
         URI targetURL = getTargetURL();
 
         BundleDeploymentMethod deployMethod = getDeploymentMethod();
 
-
         try (CloseableHttpClient httpClient = getHttpClient()){
-            configure(httpClient, targetURL, bundleFile);
+            if (mountByFS) {
+                configure(httpClient, targetURL, null);
+            }
             getLog().info(
                     "Uninstalling Bundle " + bundleName + " from "
                             + targetURL + " via " + deployMethod + "...");
-            deployMethod.execute().undeploy(targetURL, bundleFile, bundleName, new DeployContext()
+            deployMethod.execute().undeploy(targetURL, bundleName, new DeployContext()
                     .log(getLog())
                     .httpClient(httpClient)
                     .failOnError(failOnError)
@@ -95,7 +118,7 @@ public class BundleUninstallMojo extends AbstractBundleInstallMojo {
 
     @Override
     protected void configure(CloseableHttpClient httpClient, final URI targetURL, final File file) throws MojoExecutionException {
-        new SlingInitialContentMounter(getLog(), httpClient, project).unmount(targetURL, file);
+        new SlingInitialContentMounter(getLog(), httpClient, project).unmount(targetURL);
     }
     
 }
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/FsUnMountMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/FsUnMountMojo.java
index 0e7ee0c..51d1a86 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/FsUnMountMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/FsUnMountMojo.java
@@ -36,7 +36,7 @@ public class FsUnMountMojo extends AbstractFsMountMojo {
 
     @Override
     protected void configureSlingInitialContent(CloseableHttpClient httpClient, final URI targetUrl, final File bundleFile) throws MojoExecutionException {
-        new SlingInitialContentMounter(getLog(), httpClient, project).unmount(targetUrl, bundleFile);
+        new SlingInitialContentMounter(getLog(), httpClient, project).unmount(targetUrl);
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
index 36329ba..bf98ac9 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/DeployMethod.java
@@ -22,6 +22,8 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 
+import org.apache.sling.maven.bundlesupport.deploy.method.FelixPostDeployMethod;
+
 /**
  * Deploys/installs and undeploys/uninstalls bundles on a Sling instance.
  */
@@ -40,11 +42,10 @@ public interface DeployMethod {
     /**
      * Undeploy/uninstall a bundle on a Sling instance.
      * @param targetURL Target URL
-     * @param file Bundle file
-     * @param bundleSymbolicName Bundle symbolic name
+     * @param bundleName Bundle symbolic name or file name (for all methods except for {@link FelixPostDeployMethod})
      * @param context Deploy context parameters
      * @throws IOException in case of failure
      */
-    void undeploy(URI targetURL, File file, String bundleSymbolicName, DeployContext context) throws IOException;
+    void undeploy(URI targetURL, String bundleName, DeployContext context) throws IOException;
     
 }
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/FelixPostDeployMethod.java b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/FelixPostDeployMethod.java
index bda2b89..daf6920 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/FelixPostDeployMethod.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/FelixPostDeployMethod.java
@@ -50,6 +50,7 @@ public class FelixPostDeployMethod implements DeployMethod {
         // append pseudo path after root URL to not get redirected
         // https://github.com/apache/felix-dev/blob/8e35c940a95c91f3fee09c537dbaf9665e5d027e/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java#L338
         URI postUrl = targetURL.resolve("install");
+        context.getLog().debug("Installing via POST to " + postUrl);
         final HttpPost filePost = new HttpPost(postUrl);
 
         // set referrer
@@ -75,8 +76,9 @@ public class FelixPostDeployMethod implements DeployMethod {
     }
 
     @Override
-    public void undeploy(URI targetURL, File file, String bundleSymbolicName, DeployContext context) throws IOException {
+    public void undeploy(URI targetURL, String bundleSymbolicName, DeployContext context) throws IOException {
         URI postUrl = targetURL.resolve("bundles/" + bundleSymbolicName);
+        context.getLog().debug("Uninstalling via POST to " + postUrl);
         final HttpPost post = new HttpPost(postUrl);
         List<NameValuePair> params = new ArrayList<>();
         params.add(new BasicNameValuePair("action", "uninstall"));
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/SlingPostDeployMethod.java b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/SlingPostDeployMethod.java
index d60ed9f..8d14cec 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/SlingPostDeployMethod.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/SlingPostDeployMethod.java
@@ -61,8 +61,8 @@ public class SlingPostDeployMethod implements DeployMethod {
     }
 
     @Override
-    public void undeploy(URI targetURL, File file, String bundleSymbolicName, DeployContext context) throws IOException {
-        final HttpPost post = new HttpPost(getURLWithFilename(targetURL, file.getName()));
+    public void undeploy(URI targetURL, String bundleName, DeployContext context) throws IOException {
+        final HttpPost post = new HttpPost(getURLWithFilename(targetURL, bundleName));
 
         List<NameValuePair> params = new ArrayList<>();
         // Request JSON response from Sling instead of standard HTML
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/WebDavPutDeployMethod.java b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/WebDavPutDeployMethod.java
index 0c82476..0e96a94 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/WebDavPutDeployMethod.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/deploy/method/WebDavPutDeployMethod.java
@@ -58,8 +58,8 @@ public class WebDavPutDeployMethod implements DeployMethod {
     }
 
     @Override
-    public void undeploy(URI targetURL, File file, String bundleSymbolicName, DeployContext context) throws IOException {
-        final HttpDelete delete = new HttpDelete(SlingPostDeployMethod.getURLWithFilename(targetURL, file.getName()));
+    public void undeploy(URI targetURL, String bundleName, DeployContext context) throws IOException {
+        final HttpDelete delete = new HttpDelete(SlingPostDeployMethod.getURLWithFilename(targetURL, bundleName));
         // sanity check on response
         // must answer 204 (no content)
         // https://github.com/apache/jackrabbit/blob/88490006e6bdba0b0ad52d209b1bfa040477c2ec/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/server/AbstractWebdavServlet.java#L763
@@ -83,6 +83,11 @@ public class WebDavPutDeployMethod implements DeployMethod {
         // this never returns a body
     }
 
+    /**
+     * Throws {@link HttpResponseException} for all response codes except for the accepted ones.
+     * Returns the response code otherwise.
+     *
+     */
     private static final class ResponseCodeEnforcingResponseHandler implements HttpClientResponseHandler<Integer> {
 
         private final List<Integer> allowedCodes;
@@ -94,12 +99,11 @@ public class WebDavPutDeployMethod implements DeployMethod {
         public Integer handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
             final HttpEntity entity = response.getEntity();
             EntityUtils.consume(entity);
-            if (allowedCodes.contains(response.getCode())) {
-                throw new HttpResponseException(response.getCode(), response.getReasonPhrase());
+            if (!allowedCodes.contains(response.getCode())) {
+                throw new HttpResponseException(response.getCode(), "Unexpected response code " + response.getCode() + ": " + response.getReasonPhrase());
             }
             return null;
         }
-        
     }
 
     private void performMkCol(URI uri, DeployContext context) throws IOException {
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/fsresource/SlingInitialContentMounter.java b/src/main/java/org/apache/sling/maven/bundlesupport/fsresource/SlingInitialContentMounter.java
index 8963146..4757307 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/fsresource/SlingInitialContentMounter.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/fsresource/SlingInitialContentMounter.java
@@ -164,10 +164,11 @@ public final class SlingInitialContentMounter {
      * @param bundleFile The artifact (bundle)
      * @throws MojoExecutionException Exception
      */
-    public void unmount(final URI targetUrl, final File bundleFile) throws MojoExecutionException {
+    public void unmount(final URI targetUrl) throws MojoExecutionException {
         log.info("Removing file system provider configurations...");
 
         // remove all current configs for this project
+        // TODO: something is weird here, as target URL only contains the base URL but not the actual bundle name
         final Map<String,FsResourceConfiguration> oldConfigs = helper.getCurrentConfigurations(targetUrl);
         helper.removeConfigurations(targetUrl, oldConfigs);
     }