You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/09/24 11:21:33 UTC

svn commit: r578722 - in /incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport: AbstractBundleDeployMojo.java BundleDeployFileMojo.java BundleDeployMojo.java

Author: fmeschbe
Date: Mon Sep 24 02:21:32 2007
New Revision: 578722

URL: http://svn.apache.org/viewvc?rev=578722&view=rev
Log:
Add new plugin comparable to deploy named deploy-file which allows deployment of a bundle without a POM

Added:
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java
Modified:
    incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java

Added: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java?rev=578722&view=auto
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java (added)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java Mon Sep 24 02:21:32 2007
@@ -0,0 +1,187 @@
+/*
+ * 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.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.Deflater;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
+import org.apache.commons.httpclient.methods.multipart.FilePartSource;
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.Part;
+import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+
+abstract class AbstractBundleDeployMojo extends AbstractBundlePostMojo {
+
+    /**
+     * The URL to the OSGi Bundle repository to which the bundle is posted, e.g.
+     * <code>http://obr.sample.com</code>
+     * 
+     * @parameter expression="${obr}"
+     * @required
+     */
+    private String obr;
+
+    /**
+     * Returns the path and name of the jar file containing the bundle to be
+     * uploaded. This method always returns a non-<code>null</code> name but
+     * throws a <code>MojoExecutionException</code> if the name is not known.
+     * 
+     * @return The name of the file to be uploaded, this is never
+     *         <code>null</code>.
+     * @throws MojoExecutionException If the name of the file is not known
+     *             because it might not have been configured.
+     */
+    protected abstract String getJarFileName() throws MojoExecutionException;
+
+    /**
+     * Optionally fixes up the version of the bundle given in the jar File. If
+     * no version fixup is required the <code>jarFile</code> may just be
+     * returned.
+     * 
+     * @param jarFile The file whose bundle version should be fixed
+     * @return The file containing the fixed version or <code>jarFile</code>
+     *         if the version was not fixed.
+     * @throws MojoExecutionException May be thrown in case of any problems
+     */
+    protected abstract File fixBundleVersion(File jarFile)
+            throws MojoExecutionException;
+
+    /**
+     * Execute this Mojo
+     */
+    public void execute() throws MojoExecutionException {
+        // only upload if packaging as an osgi-bundle
+        File jarFile = new File(getJarFileName());
+        String bundleName = getBundleSymbolicName(jarFile);
+        if (bundleName == null) {
+            this.getLog().info(
+                jarFile + " is not an OSGi Bundle, not uploading");
+            return;
+        }
+
+        // optionally fix up the bundle version
+        jarFile = fixBundleVersion(jarFile);
+
+        getLog().info(
+            "Deploying Bundle " + bundleName + "(" + jarFile + ") to " + obr);
+        this.post(this.obr, jarFile);
+    }
+
+    private void post(String targetURL, File file)
+            throws MojoExecutionException {
+        
+        PostMethod filePost = new PostMethod(targetURL);
+        try {
+            Part[] parts = {
+                new FilePart(file.getName(), new FilePartSource(file.getName(),
+                    file)), new StringPart("_noredir_", "_noredir_") };
+            filePost.setRequestEntity(new MultipartRequestEntity(parts,
+                filePost.getParams()));
+            HttpClient client = new HttpClient();
+            client.getHttpConnectionManager().getParams().setConnectionTimeout(
+                5000);
+            int status = client.executeMethod(filePost);
+            if (status == HttpStatus.SC_OK) {
+                getLog().info("Bundle deployed");
+            } else {
+                this.getLog().error(
+                    "Deployment failed, cause: "
+                        + HttpStatus.getStatusText(status));
+            }
+        } catch (Exception ex) {
+            throw new MojoExecutionException("Deployment on " + targetURL
+                + " failed, cause: " + ex.getMessage(), ex);
+        } finally {
+            filePost.releaseConnection();
+        }
+    }
+
+    /**
+     * Change the version in jar
+     * 
+     * @param newVersion
+     * @param file
+     * @return
+     * @throws MojoExecutionException
+     */
+    protected File changeVersion(File file, String oldVersion, String newVersion)
+            throws MojoExecutionException {
+        String fileName = file.getName();
+        int pos = fileName.indexOf(oldVersion);
+        fileName = fileName.substring(0, pos) + newVersion
+            + fileName.substring(pos + oldVersion.length());
+
+        JarInputStream jis = null;
+        JarOutputStream jos;
+        OutputStream out = null;
+        try {
+            // now create a temporary file and update the version
+            final JarFile sourceJar = new JarFile(file);
+            final Manifest manifest = sourceJar.getManifest();
+            manifest.getMainAttributes().putValue("Bundle-Version", newVersion);
+
+            jis = new JarInputStream(new FileInputStream(file));
+            final File destJar = new File(file.getParentFile(), fileName);
+            out = new FileOutputStream(destJar);
+            jos = new JarOutputStream(out, manifest);
+
+            jos.setMethod(JarOutputStream.DEFLATED);
+            jos.setLevel(Deflater.BEST_COMPRESSION);
+
+            JarEntry entryIn = jis.getNextJarEntry();
+            while (entryIn != null) {
+                JarEntry entryOut = new JarEntry(entryIn.getName());
+                entryOut.setTime(entryIn.getTime());
+                entryOut.setComment(entryIn.getComment());
+                jos.putNextEntry(entryOut);
+                if (!entryIn.isDirectory()) {
+                    IOUtils.copy(jis, jos);
+                }
+                jos.closeEntry();
+                jis.closeEntry();
+                entryIn = jis.getNextJarEntry();
+            }
+
+            // close the JAR file now to force writing
+            jos.close();
+            return destJar;
+        } catch (IOException ioe) {
+            throw new MojoExecutionException(
+                "Unable to update version in jar file.", ioe);
+        } finally {
+            IOUtils.closeQuietly(jis);
+            IOUtils.closeQuietly(out);
+        }
+
+    }
+}
\ No newline at end of file

Added: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java?rev=578722&view=auto
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java (added)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java Mon Sep 24 02:21:32 2007
@@ -0,0 +1,60 @@
+/*
+ * 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.io.File;
+
+import org.apache.maven.plugin.MojoExecutionException;
+
+/**
+ * Deploy a JAR representing an OSGi Bundle. This method posts the bundle built
+ * by maven to an OSGi Bundle Repository accepting the bundle. The plugin uses
+ * a </em>multipart/format-data</em> POST request to just post the file to
+ * the URL configured in the <code>obr</code> property. 
+ *
+ * @goal deploy-file
+ * @requiresProject false
+ * @description deploy an OSGi bundle jar to the Day OBR
+ */
+public class BundleDeployFileMojo extends AbstractBundleDeployMojo {
+
+    /**
+     * The name of the generated JAR file.
+     *
+     * @parameter expression="${sling.file}"
+     */
+    private String bundleFileName;
+    
+    @Override
+    protected String getJarFileName() throws MojoExecutionException {
+        if (bundleFileName == null) {
+            throw new MojoExecutionException("Missing sling.file parameter");
+        }
+        
+        return bundleFileName;
+    }
+    
+    @Override
+    protected File fixBundleVersion(File jarFile) {
+        // we just upload the file as is (the obr might fix the version, too)
+        return jarFile;
+    }
+
+
+}

Modified: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java?rev=578722&r1=578721&r2=578722&view=diff
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java (original)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java Mon Sep 24 02:21:32 2007
@@ -18,26 +18,7 @@
 package org.apache.sling.maven.bundlesupport;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import java.util.jar.JarOutputStream;
-import java.util.jar.Manifest;
-import java.util.zip.Deflater;
-
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.multipart.FilePart;
-import org.apache.commons.httpclient.methods.multipart.FilePartSource;
-import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
-import org.apache.commons.httpclient.methods.multipart.Part;
-import org.apache.commons.httpclient.methods.multipart.StringPart;
-import org.apache.commons.io.IOUtils;
+
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -53,7 +34,7 @@
  * @phase deploy
  * @description deploy an OSGi bundle jar to the Day OBR
  */
-public class BundleDeployMojo extends AbstractBundlePostMojo {
+public class BundleDeployMojo extends AbstractBundleDeployMojo {
 
 	/**
      * The directory for the generated JAR.
@@ -72,15 +53,6 @@
     private String jarName;
 
     /**
-     * The URL to the OSGi Bundle repository to which the bundle is posted,
-     * e.g. <code>http://obr.sample.com</code>
-     * 
-     * @parameter expression="${obr}"
-     * @required
-     */
-    private String obr;
-
-    /**
      * The Maven project.
      *
      * @parameter expression="${project}"
@@ -89,18 +61,13 @@
      */
     private MavenProject project;
 
-	/**
-	 * Execute this Mojo
-	 */
-	public void execute() throws MojoExecutionException {
-        // only upload if packaging as an osgi-bundle
-        File jarFile = new File(this.buildDirectory, this.jarName);
-        String bundleName = getBundleSymbolicName(jarFile);
-        if (bundleName == null) {
-            this.getLog().info(jarFile + " is not an OSGi Bundle, not uploading");
-            return;
-        }
+    @Override
+    protected String getJarFileName() {
+        return buildDirectory + "/" + jarName;
+    }
 
+    @Override
+    protected File fixBundleVersion(File jarFile) throws MojoExecutionException {
         // if this is a snapshot, replace "SNAPSHOT" with the date generated
         // by the maven deploy plugin
         if ( this.project.getVersion().indexOf("SNAPSHOT") > 0 ) {
@@ -120,103 +87,21 @@
                 newVersion = newVersion.substring(0, pos) + newVersion.substring(pos+1);
                 pos = newVersion.indexOf('.', pos+1);
             }
-            jarFile = this.changeVersion(jarFile, newVersion);
-        } else {
-            // if this is a final release append "final"
-            try {
-                final ArtifactVersion v = this.project.getArtifact().getSelectedVersion();
-                if ( v.getBuildNumber() == 0 && v.getQualifier() == null ) {
-                    final String newVersion = this.project.getArtifact().getVersion() + ".FINAL";
-                    jarFile = this.changeVersion(jarFile, newVersion);
-                }
-            } catch (OverConstrainedVersionException ocve) {
-                // we ignore this and don't append "final"!
-            }
+            return changeVersion(jarFile, project.getVersion(), newVersion);
         }
         
-        getLog().info("Deploying Bundle " + bundleName + "(" + jarFile + ") to " + obr);
-        this.post(this.obr, jarFile);
-	}
-
-	private void post(String targetURL, File file) {
-        PostMethod filePost = new PostMethod(targetURL);
+        // if this is a final release append "final"
         try {
-            Part[] parts = { new FilePart(file.getName(), new FilePartSource(file.getName(), file)),
-                new StringPart("_noredir_", "_noredir_") };
-            filePost.setRequestEntity(new MultipartRequestEntity(parts,
-                filePost.getParams()));
-            HttpClient client = new HttpClient();
-            client.getHttpConnectionManager().getParams().setConnectionTimeout(
-                5000);
-            int status = client.executeMethod(filePost);
-            if (status == HttpStatus.SC_OK) {
-                getLog().info("Bundle deployed");
-            } else {
-                this.getLog().error(
-                    "Deployment failed, cause: " + HttpStatus.getStatusText(status));
+            final ArtifactVersion v = this.project.getArtifact().getSelectedVersion();
+            if ( v.getBuildNumber() == 0 && v.getQualifier() == null ) {
+                final String newVersion = this.project.getArtifact().getVersion() + ".FINAL";
+                return changeVersion(jarFile, project.getVersion(), newVersion);
             }
-        } catch (Exception ex) {
-            this.getLog().error(ex.getClass().getName() + " " + ex.getMessage());
-            ex.printStackTrace();
-        } finally {
-            filePost.releaseConnection();
+        } catch (OverConstrainedVersionException ocve) {
+            // we ignore this and don't append "final"!
         }
-    }
-
-    /**
-     * Change the version in jar
-     * @param newVersion
-     * @param file
-     * @return
-     * @throws MojoExecutionException
-     */
-    protected File changeVersion(File file, String newVersion)
-    throws MojoExecutionException {
-        String fileName = file.getName();
-        int pos = fileName.indexOf(this.project.getVersion());
-        fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + this.project.getVersion().length());
-
-        JarInputStream jis = null;
-        JarOutputStream jos;
-        OutputStream out = null;
-        try {
-            // now create a temporary file and update the version
-            final JarFile sourceJar = new JarFile(file);
-            final Manifest manifest = sourceJar.getManifest();
-            manifest.getMainAttributes().putValue("Bundle-Version", newVersion);
-
-            jis = new JarInputStream(new FileInputStream(file));
-            final File destJar = new File(file.getParentFile(), fileName);
-            out = new FileOutputStream(destJar);
-            jos = new JarOutputStream(out, manifest);
-
-            jos.setMethod(JarOutputStream.DEFLATED);
-            jos.setLevel(Deflater.BEST_COMPRESSION);
-
-            JarEntry entryIn = jis.getNextJarEntry();
-            while (entryIn != null) {
-                JarEntry entryOut = new JarEntry(entryIn.getName());
-                entryOut.setTime(entryIn.getTime());
-                entryOut.setComment(entryIn.getComment());
-                jos.putNextEntry(entryOut);
-                if (!entryIn.isDirectory()) {
-                    IOUtils.copy(jis, jos);
-                }
-                jos.closeEntry();
-                jis.closeEntry();
-                entryIn = jis.getNextJarEntry();
-            }
-
-            // close the JAR file now to force writing
-            jos.close();
-            return destJar;
-        } catch (IOException ioe) {
-            throw new MojoExecutionException("Unable to update version in jar file.", ioe);
-        } finally {
-            IOUtils.closeQuietly(jis);
-            IOUtils.closeQuietly(out);
-        }
-
-
+        
+        // just return the file in case of some issues
+        return jarFile;
     }
 }