tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-1638 backporting build mojo format support
Date Sat, 17 Oct 2015 16:24:43 GMT
Repository: tomee
Updated Branches:
  refs/heads/tomee-1.7.x 9e27b5e1c -> 7ea84538b


TOMEE-1638 backporting build mojo format support


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/7ea84538
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/7ea84538
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/7ea84538

Branch: refs/heads/tomee-1.7.x
Commit: 7ea84538b677ef7067256fb12efd0094ae18f588
Parents: 9e27b5e
Author: Romain Manni-Bucau <rmannibu@gmail.com>
Authored: Sat Oct 17 18:24:31 2015 +0200
Committer: Romain Manni-Bucau <rmannibu@gmail.com>
Committed: Sat Oct 17 18:24:31 2015 +0200

----------------------------------------------------------------------
 .../openejb/maven/plugin/BuildTomEEMojo.java    | 157 +++++++++++++++++--
 .../apache/openejb/maven/plugin/util/Zips.java  |  75 ---------
 2 files changed, 144 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/7ea84538/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
index 32d865f..745260f 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
@@ -17,65 +17,196 @@
 
 package org.apache.openejb.maven.plugin;
 
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
-import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.loader.IO;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
-
-import static org.apache.openejb.maven.plugin.util.Zips.zip;
+import java.util.Collections;
+import java.util.Map;
+import java.util.zip.GZIPOutputStream;
 
 /**
  * Create but not run a TomEE.
  */
 @Mojo(name = "build", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM)
 public class BuildTomEEMojo extends AbstractTomEEMojo {
+    @Deprecated
     @Parameter(property = "tomee-plugin.zip", defaultValue = "true")
     protected boolean zip;
 
     @Parameter(property = "tomee-plugin.attach", defaultValue = "true")
     protected boolean attach;
 
+    @Deprecated
     @Parameter(property = "tomee-plugin.zip-file", defaultValue = "${project.build.directory}/${project.build.finalName}.zip")
     protected File zipFile;
 
+    @Parameter(property = "tomee-plugin.output-base", defaultValue = "${project.build.directory}/${project.build.finalName}")
+    protected File base;
+
     @Component
     protected MavenProjectHelper projectHelper;
 
-    @Parameter(defaultValue = "${project}", readonly = true)
-    protected MavenProject project;
-
     @Parameter(property = "tomee-plugin.classifier")
     protected String classifier = null;
 
+    /**
+     * Behaves as TomEE 1 Maven plugin ie zip structure will get bin/ conf/ lib/ ... directly
in zip root.
+     */
+    @Parameter(property = "tomee-plugin.no-root", defaultValue = "false")
+    protected boolean skipArchiveRootFolder;
+
+    /**
+     * config looks like:
+     * &gt;formats&lt;
+     *     &gt;zip>${project.build.directory}/${project.build.finalName}.zip&gt;/zip&lt;
+     *     &gt;tar.gz /&lt;
+     * &gt;/formats&lt;
+     *
+     * No value means auto format
+     */
+    @Parameter
+    private Map<String, String> formats;
+
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
         super.execute();
-        if (zip) {
+
+        if (formats == null) {
+            formats = Collections.emptyMap();
+        }
+
+        String prefix = catalinaBase.getParentFile().getAbsolutePath();
+        if (!prefix.endsWith(File.separator)) {
+            prefix += File.separator;
+        }
+        if (skipArchiveRootFolder) {
+            prefix += catalinaBase.getName() + File.separator;
+        }
+
+        if (zip || formats.containsKey("zip")) {
             getLog().info("Zipping Custom TomEE Distribution");
+
+            final String zip = formats.get("zip");
+            final File output = zip != null ? new File(zip) : zipFile;
+            ZipArchiveOutputStream zos = null;
             try {
-                zip(catalinaBase, zipFile);
+                zos =
+                    new ZipArchiveOutputStream(new FileOutputStream(output));
+                for (final String entry : catalinaBase.list()) {
+                    zip(zos, new File(catalinaBase, entry), prefix);
+                }
             } catch (final IOException e) {
                 throw new MojoExecutionException(e.getMessage(), e);
+            } finally {
+                IO.close(zos);
             }
 
-            if (attach) {
-                getLog().info("Attaching Custom TomEE Distribution");
-                if (classifier != null) {
-                    projectHelper.attachArtifact(project, "zip", classifier, zipFile);
+            attach("zip", output);
+        }
+        if (formats != null) {
+            formats.remove("zip"); //handled previously for compatibility
+
+            for (final Map.Entry<String, String> format : formats.entrySet()) {
+                final String key = format.getKey();
+                getLog().info(key + "-ing Custom TomEE Distribution");
+
+                if ("tar.gz".equals(key)) {
+                    final String out = format.getValue();
+                    final File output = out != null ? new File(out) : new File(base.getParentFile(),
base.getName() + "." + key);
+                    Files.mkdirs(output.getParentFile());
+
+                    TarArchiveOutputStream tarGz = null;
+                    try {
+                        tarGz = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(output)));
+                        tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
+                        for (final String entry : catalinaBase.list()) {
+                            tarGz(tarGz, new File(catalinaBase, entry), prefix);
+                        }
+                    } catch (final IOException e) {
+                        throw new MojoExecutionException(e.getMessage(), e);
+                    } finally {
+                        IO.close(tarGz);
+                    }
+
+                    attach(key, output);
                 } else {
-                    projectHelper.attachArtifact(project, "zip", zipFile);
+                    throw new MojoExecutionException(key + " format not supported");
                 }
             }
         }
     }
 
+    private void attach(final String ext, final File output) {
+        if (attach) {
+            getLog().info("Attaching Custom TomEE Distribution (" + ext + ")");
+            if (classifier != null) {
+                projectHelper.attachArtifact(project, ext, classifier, output);
+            } else {
+                projectHelper.attachArtifact(project, ext, output);
+            }
+        }
+    }
+
+    private void tarGz(final TarArchiveOutputStream tarGz, final File f, final String prefix)
throws IOException {
+        final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
+        final TarArchiveEntry archiveEntry = new TarArchiveEntry(f, path);
+        if (isSh(path)) {
+            archiveEntry.setMode(0755);
+        }
+        tarGz.putArchiveEntry(archiveEntry);
+        if (f.isDirectory()) {
+            tarGz.closeArchiveEntry();
+            final File[] files = f.listFiles();
+            if (files != null) {
+                for (final File child : files) {
+                    tarGz(tarGz, child, prefix);
+                }
+            }
+        } else {
+            IO.copy(f, tarGz);
+            tarGz.closeArchiveEntry();
+        }
+    }
+
+    private void zip(final ZipArchiveOutputStream zip, final File f, final String prefix)
throws IOException {
+        final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
+        final ZipArchiveEntry archiveEntry = new ZipArchiveEntry(f, path);
+        if (isSh(path)) {
+            archiveEntry.setUnixMode(0755);
+        }
+        zip.putArchiveEntry(archiveEntry);
+        if (f.isDirectory()) {
+            zip.closeArchiveEntry();
+            final File[] files = f.listFiles();
+            if (files != null) {
+                for (final File child : files) {
+                    zip(zip, child, prefix);
+                }
+            }
+        } else {
+            IO.copy(f, zip);
+            zip.closeArchiveEntry();
+        }
+    }
+
+    private boolean isSh(final String path) {
+        return path.startsWith(catalinaBase.getName() + "/bin/") && path.endsWith(".sh");
+    }
+
     @Override
     protected void run() {
         // don't start

http://git-wip-us.apache.org/repos/asf/tomee/blob/7ea84538/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/util/Zips.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/util/Zips.java
b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/util/Zips.java
deleted file mode 100644
index 69d102f..0000000
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/util/Zips.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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.openejb.maven.plugin.util;
-
-import org.apache.openejb.loader.IO;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
-public final class Zips {
-    private static final int BUFFER_SIZE = 1024;
-
-    private Zips() {
-        // no-op
-    }
-
-    public static void zip(final File dir, final File zipName) throws IOException, IllegalArgumentException
{
-        final String[] entries = dir.list();
-        final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));
-
-        String prefix = dir.getAbsolutePath();
-        if (!prefix.endsWith(File.separator)) {
-            prefix += File.separator;
-        }
-
-        for (final String entry : entries) {
-            final File f = new File(dir, entry);
-            zip(out, f, prefix);
-        }
-        IO.close(out);
-    }
-
-    private static void zip(final ZipOutputStream out, final File f, final String prefix)
throws IOException {
-        if (f.isDirectory()) {
-            final File[] files = f.listFiles();
-            if (files != null) {
-                for (final File child : files) {
-                    zip(out, child, prefix);
-                }
-            }
-        } else {
-            final byte[] buffer = new byte[BUFFER_SIZE];
-            int bytesRead;
-
-            final String path = f.getPath().replace(prefix, "");
-
-            final FileInputStream in = new FileInputStream(f);
-            final ZipEntry entry = new ZipEntry(path);
-            out.putNextEntry(entry);
-            while ((bytesRead = in.read(buffer)) != -1) {
-                out.write(buffer, 0, bytesRead);
-            }
-            IO.close(in);
-        }
-    }
-}


Mime
View raw message