You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/03/20 12:34:15 UTC

svn commit: r1579597 - in /tomee/tomee/trunk/maven/tomee-maven-plugin: ./ src/main/java/org/apache/openejb/maven/plugin/ src/main/java/org/apache/openejb/maven/plugin/runner/

Author: rmannibucau
Date: Thu Mar 20 11:34:14 2014
New Revision: 1579597

URL: http://svn.apache.org/r1579597
Log:
TOMEE-1144 exec goal for tomee

Added:
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
Modified:
    tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml?rev=1579597&r1=1579596&r2=1579597&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml Thu Mar 20 11:34:14 2014
@@ -62,6 +62,16 @@
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-archiver</artifactId>
+      <version>2.4.1</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-compress</artifactId>
+      <version>1.5</version>
+    </dependency>
 
     <dependency>
       <groupId>org.apache.openejb</groupId>

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java?rev=1579597&r1=1579596&r2=1579597&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/BuildTomEEMojo.java Thu Mar 20 11:34:14 2014
@@ -43,13 +43,13 @@ public class BuildTomEEMojo extends Abst
     protected boolean attach;
 
     @Parameter(property = "tomee-plugin.zip-file", defaultValue = "${project.build.directory}/${project.build.finalName}.zip")
-    private File zipFile;
+    protected File zipFile;
 
     @Component
-    private MavenProjectHelper projectHelper;
+    protected MavenProjectHelper projectHelper;
 
     @Parameter(defaultValue = "${project}", readonly = true)
-    private MavenProject project;
+    protected MavenProject project;
 
     @Parameter(property = "tomee-plugin.classifier")
     protected String classifier = null;

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java?rev=1579597&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/ExecMojo.java Thu Mar 20 11:34:14 2014
@@ -0,0 +1,140 @@
+/*
+ * 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;
+
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
+import org.apache.commons.compress.utils.IOUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.openejb.maven.plugin.runner.ExecRunner;
+import org.codehaus.plexus.archiver.jar.Manifest;
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.StringWriter;
+import java.util.Properties;
+
+import static org.apache.openejb.loader.Files.mkdirs;
+
+@Mojo(name = "exec", requiresDependencyResolution = ResolutionScope.RUNTIME_PLUS_SYSTEM)
+public class ExecMojo extends BuildTomEEMojo {
+    @Parameter(property = "tomee-plugin.exec-file", defaultValue = "${project.build.directory}/${project.build.finalName}-exec.jar")
+    protected File execFile;
+
+    @Parameter(property = "tomee-plugin.runner-class", defaultValue = "org.apache.openejb.maven.plugin.runner.ExecRunner")
+    private String runnerClass;
+
+    @Parameter(property = "tomee-plugin.distribution-name", defaultValue = "tomee.zip")
+    private String distributionName;
+
+    @Parameter(property = "tomee-plugin.runtime-working-dir", defaultValue = ".distribution")
+    private String runtimeWorkingDir;
+
+    @Parameter(property = "tomee-plugin.script", defaultValue = "bin/catalina[.sh|.bat]")
+    private String script;
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        final boolean realAttach = attach;
+
+        attach = false;
+        zip = true;
+        super.execute();
+
+        try {
+            createExecutableJar();
+        } catch (final Exception e) {
+            throw new MojoExecutionException(e.getMessage(), e);
+        }
+
+        if (realAttach) {
+            getLog().info("Attaching Exec TomEE binary");
+            if (classifier != null) {
+                projectHelper.attachArtifact(project, "jar", classifier, execFile);
+            } else {
+                projectHelper.attachArtifact(project, "jar", execFile);
+            }
+        }
+    }
+
+    private void createExecutableJar() throws Exception {
+        mkdirs(execFile.getParentFile());
+
+        final Properties config = new Properties();
+        config.put("distribution", distributionName);
+        config.put("workingDir", runtimeWorkingDir);
+        config.put("command", script);
+
+        // create an executable jar with main runner and zipFile
+        final FileOutputStream fileOutputStream = new FileOutputStream(execFile);
+        final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, fileOutputStream);
+
+        { // distrib
+            os.putArchiveEntry(new JarArchiveEntry(distributionName));
+            final FileInputStream in = new FileInputStream(zipFile);
+            try {
+                IOUtils.copy(in, os);
+                os.closeArchiveEntry();
+            } finally {
+                IOUtil.close(in);
+            }
+        }
+
+        { // config
+            os.putArchiveEntry(new JarArchiveEntry("configuration.properties"));
+            final StringWriter writer = new StringWriter();
+            config.store(writer, "");
+            IOUtils.copy(new ByteArrayInputStream(writer.toString().getBytes()), os);
+            os.closeArchiveEntry();
+        }
+
+        { // Manifest
+            final Manifest manifest = new Manifest();
+
+            final Manifest.Attribute mainClassAtt = new Manifest.Attribute();
+            mainClassAtt.setName("Main-Class");
+            mainClassAtt.setValue(runnerClass);
+            manifest.addConfiguredAttribute(mainClassAtt);
+
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
+            manifest.write(baos);
+
+            os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF"));
+            IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), os);
+            os.closeArchiveEntry();
+        }
+
+        { // Main
+            final String name = ExecRunner.class.getName().replace('.', '/') + ".class";
+            os.putArchiveEntry(new JarArchiveEntry(name));
+            IOUtils.copy(getClass().getResourceAsStream('/' + name), os);
+            os.closeArchiveEntry();
+        }
+
+        IOUtil.close(os);
+        IOUtil.close(fileOutputStream);
+    }
+}

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java?rev=1579597&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java Thu Mar 20 11:34:14 2014
@@ -0,0 +1,169 @@
+/*
+ * 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.runner;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Locale;
+import java.util.Properties;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import static java.util.Arrays.asList;
+
+public class ExecRunner implements Runnable {
+    private final InputStream in;
+    private final OutputStream out;
+
+    private ExecRunner(final InputStream in, final OutputStream out) {
+        this.in = in;
+        this.out = out;
+    }
+
+    @Override
+    public void run() {
+        try {
+            int i;
+            final byte[] buf = new byte[1];
+            while ((i = in.read(buf)) != -1) {
+                out.write(buf, 0, i);
+            }
+        } catch (final Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static final String SH_BAT_AUTO = "[.sh|.bat]";
+
+    public static void main(final String[] args) throws Exception {
+        final Properties config = new Properties();
+
+        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+        final InputStream is = contextClassLoader.getResourceAsStream("configuration.properties");
+        if (is != null) {
+            config.load(is);
+            is.close();
+        } else {
+            throw new IllegalArgumentException("Config not found");
+        }
+
+        final String distrib = config.getProperty("distribution");
+        final String workingDir = config.getProperty("workingDir");
+        final InputStream distribIs = contextClassLoader.getResourceAsStream(distrib);
+        File distribOutput = new File(workingDir);
+        extract(distribIs, distribOutput);
+
+        final File[] extracted = distribOutput.listFiles();
+        if (extracted != null && extracted.length == 1) {
+            distribOutput = extracted[0];
+        }
+
+        String cmd = config.getProperty("command");
+        if (cmd.endsWith(SH_BAT_AUTO)) {
+            final int lastSlash = cmd.lastIndexOf('/');
+            if (lastSlash > 0) {
+                final String dir = cmd.substring(0, lastSlash);
+                final String script = cmd.substring(lastSlash + 1, cmd.length() - SH_BAT_AUTO.length())
+                        + (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win") ? ".bat" : ".sh");
+                cmd = dir + '/' + script;
+                final File scriptFile = new File(distribOutput, cmd);
+                if (!scriptFile.exists()) {
+                    throw new IllegalArgumentException("Can't find  " + cmd);
+                }
+                if (cmd.endsWith(".sh")) {
+                    scriptFile.setExecutable(true);
+                }
+            }
+        }
+
+        final Collection<String> params = new ArrayList<String>();
+        params.add(cmd);
+        if (args != null) {
+            params.addAll(asList(args));
+        }
+
+        final Process process = Runtime.getRuntime().exec(params.toArray(new String[params.size()]), null, distribOutput);
+        pipe("exec-runner-out", process.getInputStream(), System.out);
+        pipe("exec-runner-err", process.getErrorStream(), System.err);
+        process.waitFor();
+        System.out.flush();
+        System.err.flush();
+        System.out.println("Exit status: " + process.exitValue());
+    }
+
+    private static void pipe(final String name, final InputStream errorStream, final PrintStream err) {
+        final Thread thread = new Thread(new ExecRunner(errorStream, err));
+        thread.setName(name);
+        thread.setDaemon(true);
+        thread.start();
+    }
+
+    // duplicated to avoid deps, if this class has any dep then it can't be run
+    private static void extract(final InputStream distrib, final File output) throws IOException {
+        mkdirs(output);
+
+        final ZipInputStream in = new ZipInputStream(distrib);
+
+        ZipEntry entry;
+        while ((entry = in.getNextEntry()) != null) {
+            final String path = entry.getName();
+            final File file = new File(output, path);
+
+            if (entry.isDirectory()) {
+                mkdirs(file);
+                continue;
+            }
+
+            mkdirs(file.getParentFile());
+            copy(in, file);
+
+            final long lastModified = entry.getTime();
+            if (lastModified > 0) {
+                file.setLastModified(lastModified);
+            }
+
+        }
+
+        in.close();
+    }
+
+    private static void copy(final ZipInputStream in, final File file) throws IOException {
+        final FileOutputStream to = new FileOutputStream(file);
+        try {
+            final byte[] buffer = new byte[1024];
+            int length;
+            while ((length = in.read(buffer)) != -1) {
+                to.write(buffer, 0, length);
+            }
+            to.flush();
+        } finally {
+            to.close();
+        }
+    }
+
+    private static void mkdirs(File output) {
+        if (!output.exists() && !output.mkdirs()) {
+            throw new IllegalArgumentException("Can't create " + output.getAbsolutePath());
+        }
+    }
+}