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

rombert pushed a commit to annotated tag maven-sling-plugin-2.0.2-incubator
in repository https://gitbox.apache.org/repos/asf/sling-maven-sling-plugin.git

commit d10b297528f4f912339f5dfbf2099b954a98cf79
Author: Felix Meschberger <[email protected]>
AuthorDate: Mon Sep 24 09:21:32 2007 +0000

    Add new plugin comparable to deploy named deploy-file which allows 
deployment of a bundle without a POM
    
    git-svn-id: 
https://svn.apache.org/repos/asf/incubator/sling/trunk/maven-sling-plugin@578722
 13f79535-47bb-0310-9956-ffa450edef68
---
 ...ployMojo.java => AbstractBundleDeployMojo.java} | 143 ++++++++-----------
 .../maven/bundlesupport/BundleDeployFileMojo.java  |  60 ++++++++
 .../maven/bundlesupport/BundleDeployMojo.java      | 151 +++------------------
 3 files changed, 132 insertions(+), 222 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java 
b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
similarity index 54%
copy from 
src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
copy to 
src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
index f6764e6..1f64609 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
+++ 
b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java
@@ -38,111 +38,73 @@ 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;
-import org.apache.maven.project.MavenProject;
 
-/**
- * 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
- * @phase deploy
- * @description deploy an OSGi bundle jar to the Day OBR
- */
-public class BundleDeployMojo extends AbstractBundlePostMojo {
+abstract class AbstractBundleDeployMojo extends AbstractBundlePostMojo {
 
-       /**
-     * The directory for the generated JAR.
-     *
-     * @parameter expression="${project.build.directory}"
+    /**
+     * 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 buildDirectory;
+    private String obr;
 
     /**
-     * The name of the generated JAR file.
-     *
-     * @parameter alias="jarName" expression="${project.build.finalName}.jar"
-     * @required
+     * 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.
      */
-    private String jarName;
+    protected abstract String getJarFileName() throws MojoExecutionException;
 
     /**
-     * The URL to the OSGi Bundle repository to which the bundle is posted,
-     * e.g. <code>http://obr.sample.com</code>
+     * 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.
      * 
-     * @parameter expression="${obr}"
-     * @required
+     * @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
      */
-    private String obr;
+    protected abstract File fixBundleVersion(File jarFile)
+            throws MojoExecutionException;
 
     /**
-     * The Maven project.
-     *
-     * @parameter expression="${project}"
-     * @required
-     * @readonly
+     * Execute this Mojo
      */
-    private MavenProject project;
-
-       /**
-        * Execute this Mojo
-        */
-       public void execute() throws MojoExecutionException {
+    public void execute() throws MojoExecutionException {
         // only upload if packaging as an osgi-bundle
-        File jarFile = new File(this.buildDirectory, this.jarName);
+        File jarFile = new File(getJarFileName());
         String bundleName = getBundleSymbolicName(jarFile);
         if (bundleName == null) {
-            this.getLog().info(jarFile + " is not an OSGi Bundle, not 
uploading");
+            this.getLog().info(
+                jarFile + " is not an OSGi Bundle, not uploading");
             return;
         }
 
-        // if this is a snapshot, replace "SNAPSHOT" with the date generated
-        // by the maven deploy plugin
-        if ( this.project.getVersion().indexOf("SNAPSHOT") > 0 ) {
-            // create new version string by replacing all '-' with '.'
-            String newVersion = this.project.getArtifact().getVersion();
-            int firstPos = newVersion.indexOf('-') + 1;
-            int pos = 0;
-            while (pos != -1) {
-                pos = newVersion.indexOf('-');
-                if ( pos != -1 ) {
-                    newVersion = newVersion.substring(0, pos ) + '.' + 
newVersion.substring(pos+1);
-                }
-            }
-            // now remove all dots after the third one
-            pos = newVersion.indexOf('.', firstPos);
-            while ( pos != -1 ) {
-                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"!
-            }
-        }
-        
-        getLog().info("Deploying Bundle " + bundleName + "(" + jarFile + ") to 
" + obr);
+        // 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) {
+    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_") };
+            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();
@@ -153,11 +115,12 @@ public class BundleDeployMojo extends 
AbstractBundlePostMojo {
                 getLog().info("Bundle deployed");
             } else {
                 this.getLog().error(
-                    "Deployment failed, cause: " + 
HttpStatus.getStatusText(status));
+                    "Deployment failed, cause: "
+                        + HttpStatus.getStatusText(status));
             }
         } catch (Exception ex) {
-            this.getLog().error(ex.getClass().getName() + " " + 
ex.getMessage());
-            ex.printStackTrace();
+            throw new MojoExecutionException("Deployment on " + targetURL
+                + " failed, cause: " + ex.getMessage(), ex);
         } finally {
             filePost.releaseConnection();
         }
@@ -165,16 +128,18 @@ public class BundleDeployMojo extends 
AbstractBundlePostMojo {
 
     /**
      * Change the version in jar
+     * 
      * @param newVersion
      * @param file
      * @return
      * @throws MojoExecutionException
      */
-    protected File changeVersion(File file, String newVersion)
-    throws MojoExecutionException {
+    protected File changeVersion(File file, String oldVersion, 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());
+        int pos = fileName.indexOf(oldVersion);
+        fileName = fileName.substring(0, pos) + newVersion
+            + fileName.substring(pos + oldVersion.length());
 
         JarInputStream jis = null;
         JarOutputStream jos;
@@ -211,12 +176,12 @@ public class BundleDeployMojo extends 
AbstractBundlePostMojo {
             jos.close();
             return destJar;
         } catch (IOException ioe) {
-            throw new MojoExecutionException("Unable to update version in jar 
file.", 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
diff --git 
a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java 
b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java
new file mode 100644
index 0000000..3284b4d
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployFileMojo.java
@@ -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;
+    }
+
+
+}
diff --git 
a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java 
b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
index f6764e6..8dae336 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
@@ -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 @@ import org.apache.maven.project.MavenProject;
  * @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 @@ public class BundleDeployMojo extends AbstractBundlePostMojo 
{
     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 @@ public class BundleDeployMojo extends 
AbstractBundlePostMojo {
      */
     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 @@ public class BundleDeployMojo extends 
AbstractBundlePostMojo {
                 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);
-        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) {
-            this.getLog().error(ex.getClass().getName() + " " + 
ex.getMessage());
-            ex.printStackTrace();
-        } finally {
-            filePost.releaseConnection();
-        }
-    }
-
-    /**
-     * 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;
+        // if this is a final release append "final"
         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();
+            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);
             }
-
-            // 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);
+        } catch (OverConstrainedVersionException ocve) {
+            // we ignore this and don't append "final"!
         }
-
-
+        
+        // just return the file in case of some issues
+        return jarFile;
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to