You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2017/05/29 21:13:20 UTC

svn commit: r1796701 - in /sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport: BundlePrerequisite.java FsMountMojo.java

Author: sseifert
Date: Mon May 29 21:13:20 2017
New Revision: 1796701

URL: http://svn.apache.org/viewvc?rev=1796701&view=rev
Log:
SLING-6917 allow to deploy different versions of fsresource based on bundle preconditions

Added:
    sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java   (with props)
Modified:
    sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/FsMountMojo.java

Added: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java?rev=1796701&view=auto
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java (added)
+++ sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java Mon May 29 21:13:20 2017
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+package org.apache.sling.maven.bundlesupport;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * Bundles that have to be installed as prerequisites to execute a goal.
+ * The bundles are only installed if the preconditions are met.
+ */
+public final class BundlePrerequisite {
+    
+    /**
+     * List of bundles that is installed when preconditions are met,
+     * and if these bundles are not installed yet in the given (or a higher) version.
+     */
+    private final List<Bundle> bundles = new ArrayList<>();
+    
+    /**
+     * List of precondition bundles that have to be already present in the given
+     * (or higher) versions to install the bundles.
+     */
+    private final List<Bundle> preconditions = new ArrayList<>();
+    
+    public void addBundle(Bundle bundle) {
+        bundles.add(bundle);
+    }
+    
+    public void addPrecondition(Bundle bundle) {
+        preconditions.add(bundle);
+    }
+    
+    public List<Bundle> getBundles() {
+        return bundles;
+    }
+
+    public List<Bundle> getPreconditions() {
+        return preconditions;
+    }
+
+    /**
+     * Described bundle with symbolic name and bundle version.
+     */
+    public static final class Bundle {
+        
+        private String groupId;
+        private String artifactId;
+        private String version;
+        private String symbolicName;
+        
+        public Bundle() {
+            // empty constructor
+        }
+        
+        public Bundle(String groupId, String artifactId, String version) {
+            this.groupId = groupId;
+            this.artifactId = artifactId;
+            this.version = version;
+        }
+        
+        public String getGroupId() {
+            return groupId;
+        }
+
+        public void setGroupId(String groupId) {
+            this.groupId = groupId;
+        }
+
+        public String getArtifactId() {
+            return artifactId;
+        }
+
+        public void setArtifactId(String artifactId) {
+            this.artifactId = artifactId;
+        }
+
+        public String getSymbolicName() {
+            return StringUtils.defaultString(symbolicName, artifactId);
+        }
+        
+        public void setSymbolicName(String symbolicName) {
+            this.symbolicName = symbolicName;
+        }
+        
+        public String getVersion() {
+            return version;
+        }
+        
+        public void setVersion(String version) {
+            this.version = version;
+        }
+        
+    }
+
+}

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Mon May 29 21:13:20 2017
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundlePrerequisite.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/FsMountMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/FsMountMojo.java?rev=1796701&r1=1796700&r2=1796701&view=diff
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/FsMountMojo.java (original)
+++ sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/FsMountMojo.java Mon May 29 21:13:20 2017
@@ -20,6 +20,8 @@ package org.apache.sling.maven.bundlesup
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.json.JsonArray;
 import javax.json.JsonException;
@@ -40,6 +42,7 @@ import org.apache.maven.plugins.annotati
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.repository.RepositorySystem;
+import org.apache.sling.maven.bundlesupport.BundlePrerequisite.Bundle;
 import org.apache.sling.maven.bundlesupport.deploy.BundleDeploymentMethod;
 import org.apache.sling.maven.bundlesupport.deploy.DeployContext;
 import org.apache.sling.maven.bundlesupport.fsresource.FileVaultXmlMounter;
@@ -52,10 +55,15 @@ import org.apache.sling.maven.bundlesupp
 @Mojo(name = "fsmount", requiresProject = true)
 public class FsMountMojo extends AbstractFsMountMojo {
     
-    private static final String FS_BUNDLE_GROUP_ID = "org.apache.sling"; 
+    private static final String BUNDLE_GROUP_ID = "org.apache.sling"; 
+
     private static final String FS_BUNDLE_ARTIFACT_ID = "org.apache.sling.fsresource"; 
-    private static final String FS_BUNDLE_SYMBOLIC_NAME = FS_BUNDLE_ARTIFACT_ID; 
+    private static final String FS_BUNDLE_DEFAULT_VERSION = "2.1.2"; 
+    private static final String FS_BUNDLE_LEGACY_DEFAULT_VERSION = "1.4.2"; 
     
+    private static final String RESOURCE_RESOLVER_BUNDLE_ARTIFACT_ID = "org.apache.sling.resourceresolver"; 
+    private static final String RESOURCE_RESOLVER_BUNDLE_MIN_VERSION = "1.5.18"; 
+
     /**
      * Bundle deployment method. One of the following three values are allowed
      * <ol>
@@ -72,20 +80,51 @@ public class FsMountMojo extends Abstrac
      * 
      * This has precedence over the deprecated parameter {@link #usePut}.
      */
-    @Parameter(property="sling.deploy.method", required = true, defaultValue = "WebConsole")
+    @Parameter(property="sling.deploy.method", required = false, defaultValue = "WebConsole")
     private BundleDeploymentMethod deploymentMethod;
     
     /**
      * Deploy <code>org.apache.sling.fsresource</code> to Sling instance bundle when it is not deployed already.
      */
-    @Parameter(required = true, defaultValue = "true")
+    @Parameter(required = false, defaultValue = "true")
     private boolean deployFsResourceBundle;
     
     /**
-     * Minimum version of <code>org.apache.sling.fsresource</code> bundle. If an older version is installed this version is deployed. 
+     * Bundles that have to be installed as prerequisites to execute this goal.
+     * With multiple entries in the list different bundles with different preconditions can be defined.<br/>
+     * <strong>Default value is:</strong>:
+     * <pre>
+     * &lt;deployFsResourceBundlePrerequisites&gt;
+     *   &lt;bundlePrerequisite&gt;
+     *     &lt;bundles&gt;
+     *       &lt;bundle&gt;
+     *         &lt;groupId&gt;org.apache.sling&lt;/groupId&gt;
+     *         &lt;artifactId&gt;org.apache.sling.fsresource&lt;/artifactId&gt;
+     *         &lt;version&gt;2.1.2&lt;/version&gt;
+     *       &lt;/bundle&gt;
+     *     &lt;/bundles&gt;
+     *     &lt;preconditions&gt;
+     *       &lt;bundle&gt;
+     *         &lt;groupId&gt;org.apache.sling&lt;/groupId&gt;
+     *         &lt;artifactId&gt;org.apache.sling.resourceresolver&lt;/artifactId&gt;
+     *         &lt;version&gt;1.5.18&lt;/version&gt;
+     *       &lt;/bundle&gt;
+     *     &lt;/preconditions&gt;
+     *   &lt;/bundlePrerequisite&gt;
+     *   &lt;bundlePrerequisite&gt;
+     *     &lt;bundles&gt;
+     *       &lt;bundle&gt;
+     *         &lt;groupId&gt;org.apache.sling&lt;/groupId&gt;
+     *         &lt;artifactId&gt;org.apache.sling.fsresource&lt;/artifactId&gt;
+     *         &lt;version&gt;1.4.2&lt;/version&gt;
+     *       &lt;/bundle&gt;
+     *     &lt;/bundles&gt;
+     *   &lt;/bundlePrerequisite&gt;
+     * &lt;/deployFsResourceBundlePrerequisites&gt;
+     * </pre>
      */
-    @Parameter(required = true, defaultValue = "2.1.2")
-    private String minimumFsResourceVersion;
+    @Parameter(required = false)
+    private List<BundlePrerequisite> deployFsResourceBundlePrerequisites;
 
     @Component
     private RepositorySystem repository;
@@ -93,6 +132,13 @@ public class FsMountMojo extends Abstrac
     private ArtifactRepository localRepository;
     @Parameter(property = "project.remoteArtifactRepositories", required = true, readonly = true)
     private java.util.List<ArtifactRepository> remoteRepositories;
+    
+    public void addDeployFsResourceBundlePrerequisite(BundlePrerequisite item) {
+        if (this.deployFsResourceBundlePrerequisites == null) {
+            this.deployFsResourceBundlePrerequisites = new ArrayList<>();
+        }
+        this.deployFsResourceBundlePrerequisites.add(item);
+    }
 
     @Override
     protected void configureSlingInitialContent(final String targetUrl, final File bundleFile) throws MojoExecutionException {
@@ -110,29 +156,62 @@ public class FsMountMojo extends Abstrac
             return;
         }
         
-        boolean deployRequired = false;       
-        String fsBundleVersion = getFsResourceBundleInstalledVersion(targetUrl);
-        if (StringUtils.isBlank(fsBundleVersion)) {
-            deployRequired = true;
-        }
-        else {
-            DefaultArtifactVersion deployedVersion = new DefaultArtifactVersion(fsBundleVersion);
-            DefaultArtifactVersion requiredVersion = new DefaultArtifactVersion(minimumFsResourceVersion);
-            deployRequired = (deployedVersion.compareTo(requiredVersion) < 0);
+        if (deployFsResourceBundlePrerequisites == null) {
+            BundlePrerequisite latest = new BundlePrerequisite();
+            latest.addBundle(new Bundle(BUNDLE_GROUP_ID, FS_BUNDLE_ARTIFACT_ID, FS_BUNDLE_DEFAULT_VERSION));
+            latest.addPrecondition(new Bundle(BUNDLE_GROUP_ID, RESOURCE_RESOLVER_BUNDLE_ARTIFACT_ID, RESOURCE_RESOLVER_BUNDLE_MIN_VERSION));
+            addDeployFsResourceBundlePrerequisite(latest);
+            
+            BundlePrerequisite legacy = new BundlePrerequisite();
+            legacy.addBundle(new Bundle(BUNDLE_GROUP_ID, FS_BUNDLE_ARTIFACT_ID, FS_BUNDLE_LEGACY_DEFAULT_VERSION));
+            addDeployFsResourceBundlePrerequisite(legacy);
+        }
+        
+        for (BundlePrerequisite bundlePrerequisite : deployFsResourceBundlePrerequisites) {
+            if (isBundlePrerequisitesPreconditionsMet(bundlePrerequisite, targetUrl)) {
+                for (Bundle bundle : bundlePrerequisite.getBundles()) {
+                    deployBundle(bundle, targetUrl);
+                }
+                break;
+            }
         }
-        if (!deployRequired) {
-            getLog().info("Bundle " + FS_BUNDLE_SYMBOLIC_NAME + " " + fsBundleVersion + " already installed, skipping deployment.");
+    }
+    
+    private void deployBundle(Bundle bundle, String targetUrl) throws MojoExecutionException {
+        if (isBundleInstalled(bundle, targetUrl)) {
+            getLog().debug("Bundle " + bundle.getSymbolicName() + " " + bundle.getVersion() + " (or higher) already installed.");
             return;
         }
         
-        getLog().info("Deploying bundle " + FS_BUNDLE_SYMBOLIC_NAME + " " + minimumFsResourceVersion + " ...");
+        getLog().info("Installing Bundle " + bundle.getSymbolicName() + " " + bundle.getVersion() + " to "
+                    + targetUrl + " via " + deploymentMethod);
         
-        File file = getArtifactFile(FS_BUNDLE_GROUP_ID, FS_BUNDLE_ARTIFACT_ID, minimumFsResourceVersion, "jar");
-        deploymentMethod.execute().deploy(targetUrl, file, FS_BUNDLE_SYMBOLIC_NAME, new DeployContext()
+        File file = getArtifactFile(bundle, "jar");
+        deploymentMethod.execute().deploy(targetUrl, file, bundle.getSymbolicName(), new DeployContext()
                 .log(getLog())
                 .httpClient(getHttpClient())
                 .failOnError(failOnError));
     }
+    
+    private boolean isBundlePrerequisitesPreconditionsMet(BundlePrerequisite bundlePrerequisite, String targetUrl) throws MojoExecutionException {
+        for (Bundle precondition : bundlePrerequisite.getPreconditions()) {
+            if (!isBundleInstalled(precondition, targetUrl)) {
+                getLog().debug("Bundle " + precondition.getSymbolicName() + " " + precondition.getVersion() + " (or higher) is not installed.");
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    private boolean isBundleInstalled(Bundle bundle, String targetUrl) throws MojoExecutionException {
+        String installedVersionString = getBundleInstalledVersion(bundle.getSymbolicName(), targetUrl);
+        if (StringUtils.isBlank(installedVersionString)) {
+            return false;
+        }
+        DefaultArtifactVersion installedVersion = new DefaultArtifactVersion(installedVersionString);
+        DefaultArtifactVersion requiredVersion = new DefaultArtifactVersion(bundle.getVersion());
+        return (installedVersion.compareTo(requiredVersion) >= 0);
+    }
 
     /**
      * Get version of fsresource bundle that is installed in the instance.
@@ -140,8 +219,8 @@ public class FsMountMojo extends Abstrac
      * @return Version number or null if non installed
      * @throws MojoExecutionException
      */
-    public String getFsResourceBundleInstalledVersion(final String targetUrl) throws MojoExecutionException {
-        final String getUrl = targetUrl + "/bundles/" + FS_BUNDLE_SYMBOLIC_NAME + ".json";
+    private String getBundleInstalledVersion(final String bundleSymbolicName, final String targetUrl) throws MojoExecutionException {
+        final String getUrl = targetUrl + "/bundles/" + bundleSymbolicName + ".json";
         final GetMethod get = new GetMethod(getUrl);
 
         try {
@@ -180,9 +259,8 @@ public class FsMountMojo extends Abstrac
         return null;
     }
    
-    private File getArtifactFile(String groupId, String artifactId, String version, String type)
-            throws MojoExecutionException {
-        Artifact artifactObject = repository.createArtifact(groupId, artifactId, version, type);
+    private File getArtifactFile(Bundle bundle, String type) throws MojoExecutionException {
+        Artifact artifactObject = repository.createArtifact(bundle.getGroupId(), bundle.getArtifactId(), bundle.getVersion(), type);
         ArtifactResolutionRequest request = new ArtifactResolutionRequest();
         request.setArtifact(artifactObject);
         request.setLocalRepository(localRepository);