Hei,
what about moving this as exec:shell (when stable) ?
Cheers,
Jerome
---------- Forwarded message ----------
From: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: 12 Jun 2007 23:50:21 -0000
Subject: [mojo-scm] [4408] trunk/mojo/mojo-sandbox: Adding mojo to
execute shell scripts.
To: [EMAIL PROTECTED]
Revision 4408 Author jdcasey Date 2007-06-12 18:50:19 -0500 (Tue, 12 Jun 2007)
Log Message Adding mojo to execute shell scripts.
Added Paths
trunk/mojo/mojo-sandbox/shell-maven-plugin/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/pom.xml
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/ShellExecMojo.java
Diff
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/pom.xml
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/pom.xml
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/pom.xml
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,39 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>test</groupId>
+ <artifactId>echo-bad-IT</artifactId>
+ <version>1.1-SNAPSHOT</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>shell-maven-plugin</artifactId>
+
+ <executions>
+ <execution>
+ <id>exec-echo</id>
+ <phase>initialize</phase>
+
+ <configuration>
+ <debug>true</debug>
+ <chmod>true</chmod>
+ <script><![CDATA[
+#!/bin/bish
+
+echo "Hello, World" > ${project.build.directory}/echo-out.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
+
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-bad/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/pom.xml
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/pom.xml
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/pom.xml
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,49 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>test</groupId>
+ <artifactId>echo-dependency-files-IT</artifactId>
+ <version>1.1-SNAPSHOT</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>shell-maven-plugin</artifactId>
+
+ <executions>
+ <execution>
+ <id>exec-echo</id>
+ <phase>initialize</phase>
+
+ <configuration>
+ <trimScript>true</trimScript>
+ <chmod>true</chmod>
+ <messageLevel>debug</messageLevel>
+ <script><![CDATA[
+#!/bin/bash
+
+jar tvf @pathOf(junit:junit)@ > ${project.build.directory}/dependency-files.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
+
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-dependency-files/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/pom.xml
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/pom.xml
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/pom.xml
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,40 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>test</groupId>
+ <artifactId>echo-out-IT</artifactId>
+ <version>1.1-SNAPSHOT</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>shell-maven-plugin</artifactId>
+
+ <executions>
+ <execution>
+ <id>exec-echo</id>
+ <phase>initialize</phase>
+
+ <configuration>
+ <trimScript>true</trimScript>
+ <chmod>true</chmod>
+ <messageLevel>debug</messageLevel>
+ <script><![CDATA[
+#!/bin/bash
+
+echo "Hello, World" > ${project.build.directory}/echo-out.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
+
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-out/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/pom.xml
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/pom.xml
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/pom.xml
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,43 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>test</groupId>
+ <artifactId>echo-with-var-IT</artifactId>
+ <version>1.1-SNAPSHOT</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>shell-maven-plugin</artifactId>
+
+ <executions>
+ <execution>
+ <id>exec-echo</id>
+ <phase>initialize</phase>
+
+ <configuration>
+ <trimScript>true</trimScript>
+ <chmod>true</chmod>
+ <debug>true</debug>
+ <messageLevel>debug</messageLevel>
+ <script><![CDATA[
+#!/bin/bash
+
+VAR=something
+
+echo "Hello, $${VAR}" > ${project.build.directory}/echo-out.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
+
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/echo-with-var/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/pom.xml
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/pom.xml
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/pom.xml
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,57 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>test</groupId>
+ <artifactId>echo-out-IT</artifactId>
+ <version>1.1-SNAPSHOT</version>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>shell-maven-plugin</artifactId>
+
+ <configuration>
+ <chmod>true</chmod>
+ <messageLevel>info</messageLevel>
+ </configuration>
+
+ <executions>
+ <execution>
+ <id>exec-echo</id>
+ <phase>initialize</phase>
+
+ <configuration>
+ <script><![CDATA[
+#!/bin/bash
+
+echo "Hello, World" > ${project.build.directory}/echo-out.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>exec-echo2</id>
+ <phase>compile</phase>
+
+ <configuration>
+ <script><![CDATA[
+#!/bin/bash
+
+echo "Hello, World (again)" > ${project.build.directory}/echo-out2.txt
+
+ ]]></script>
+ </configuration>
+
+ <goals>
+ <goal>shell</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
\ No newline at end of file
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/integration-tests/multi-echo/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added: trunk/mojo/mojo-sandbox/shell-maven-plugin/pom.xml (0 => 4408)
--- trunk/mojo/mojo-sandbox/shell-maven-plugin/pom.xml (rev 0)
+++ trunk/mojo/mojo-sandbox/shell-maven-plugin/pom.xml 2007-06-12
23:50:19 UTC (rev 4408)
@@ -0,0 +1,40 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <artifactId>mojo</artifactId>
+ <groupId>org.codehaus.mojo</groupId>
+ <version>14</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>shell-maven-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>maven-plugin</packaging>
+ <name>Shell Script Executor Plugin</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <!--
+ <dependency>
+ <groupId>ant</groupId>
+ <artifactId>ant</artifactId>
+ </dependency>
+ -->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-utils</artifactId>
+ <version>1.4.2</version>
+ </dependency>
+ </dependencies>
+</project>
Property changes on: trunk/mojo/mojo-sandbox/shell-maven-plugin/pom.xml
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
Added:
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/ShellExecMojo.java
(0 => 4408) ---
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/ShellExecMojo.java
(rev 0)
+++
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/ShellExecMojo.java
2007-06-12 23:50:19 UTC (rev 4408)
@@ -0,0 +1,307 @@
+package org.codehaus.mojo.shell;
+
+/*
+ * Licensed 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.
+ *
+ */
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.CommandLineUtils;
+import org.codehaus.plexus.util.cli.Commandline;
+import org.codehaus.plexus.util.cli.StreamConsumer;
+import org.codehaus.plexus.util.cli.shell.BourneShell;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Mojo used to embed a shell script inside the POM.
+ * This class handles construction of the command-line and monitoring
of the check file,
+ * if specified. It will also handle chmod'ing the given shell
command, if required.
+ *
+ * @goal shell
+ * @requiresDependencyResolution test
+ */
+public class ShellExecMojo
+ extends AbstractMojo
+{
+
+ /**
+ * Whether to keep the script file generated by this invocation.
+ *
+ * @parameter default-value="false" expression="${shell.keepScriptFile}"
+ */
+ private boolean keepScriptFile;
+
+ /**
+ * MavenProject instance used to resolve property expressions from within Ant.
+ *
+ * @parameter expression="${project}"
+ * @required
+ * @readonly
+ */
+ private MavenProject project;
+
+ /**
+ * The temporary working directory where the project is actually
built. By default, this is
+ * within the '/target' directory.
+ *
+ * @parameter expression="${workDir}"
default-value="${project.build.sourceDirectory}"
+ * @required
+ */
+ private File workDir;
+
+ /**
+ * The Ant messageLevel to use.
+ *
+ * @parameter expression="${chmod}" default-value="false"
+ */
+ private boolean chmod;
+
+ /**
+ * The contents of the shell script.
+ *
+ * @parameter expression="${script}"
+ * @required
+ */
+ private String script;
+
+ /**
+ * Result of search for the configuration script, so we don't have to
re-search.
+ */
+ private File executable;
+
+ /**
+ * Whether to execute String.trim() on the script parameter before using it
+ * to generate the script file. This can be important in environments
like Bash,
+ * where '#!/bin/sh' must be on the first line.
+ *
+ * @parameter expression="${trimScript}" default-value="true"
+ */
+ private boolean trimScript;
+
+ /**
+ * The script-file extension.
+ *
+ * @parameter expression="${extension}" default-value=".sh"
+ */
+ private String extension;
+
+ /**
+ * @parameter expression="${debug}" default-value="false"
+ */
+ private boolean debug;
+
+ /**
+ * @parameter default-value="${project.remoteArtifactRepositories}"
+ * @readonly
+ * @required
+ */
+ private List remoteRepositories;
+
+ /**
+ * @parameter default-value="${project.pluginArtifactRepositories}"
+ * @required
+ * @readonly
+ */
+ private List remotePluginRepositories;
+
+ /**
+ * @parameter default-value="false"
+ */
+ private boolean skipPomProjects;
+
+ /**
+ * 1. Create a temporary file containing the script. This is the executable.
+ *
+ * 3. If chmodUsed == true, then we'll set the executable bit on the
executable file using chmod.
+ *
+ * 4. Construct the Ant Exec task using the supplied command, any
command-line options, an optional
+ * Make target, working directory, and overrides for failure conditions.
+ *
+ * 5. Next, execute the resulting Exec task.
+ */
+ public void execute()
+ throws MojoExecutionException
+ {
+ if ( skipPomProjects && "pom".equals( project.getPackaging() ) )
+ {
+ getLog().info( "Skipping POM project, per configuration." );
+ return;
+ }
+
+ workDir.mkdirs();
+
+ try
+ {
+ createExecutable();
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Failed to create shell script.
Reason: " + e.getMessage(), e );
+ }
+
+ if ( chmod )
+ {
+ // add the task to make the configure script executable
+ chmod();
+ }
+
+ Commandline exec = new Commandline();
+ exec.setShell( new BourneShell( true ) );
+
+ exec.setWorkingDirectory( workDir.getAbsolutePath() );
+
+ exec.setExecutable( executable.getAbsolutePath() );
+
+ try
+ {
+ StreamConsumer consumer = newInfoStreamConsumer();
+
+ int result = CommandLineUtils.executeCommandLine( exec, consumer, consumer );
+
+ if ( result != 0 )
+ {
+ throw new MojoExecutionException( "Script failed to execute (exit
value != 0). Please see output above for more information." );
+ }
+ }
+ catch ( CommandLineException e )
+ {
+ throw new MojoExecutionException( "Failed to execute embedded shell
script. Reason: " + e.getMessage(), e );
+ }
+ }
+
+ protected final void setChmodUsed( boolean chmodUsed )
+ {
+ chmod = chmodUsed;
+ }
+
+ protected final void setProject( MavenProject project )
+ {
+ this.project = project;
+ }
+
+ /**
+ * Generate the script file (to a temp file).
+ * @throws IOException
+ * @throws MojoExecutionException
+ */
+ private void createExecutable()
+ throws IOException, MojoExecutionException
+ {
+ executable = File.createTempFile( "maven-shell-plugin-", extension );
+
+ if ( debug || getLog().isDebugEnabled() || keepScriptFile )
+ {
+ getLog().info( "NOT deleting generated script file: " +
executable.getAbsolutePath() );
+ }
+ else
+ {
+ executable.deleteOnExit();
+ }
+
+ Writer writer = null;
+
+ String output = script;
+
+ if ( trimScript )
+ {
+ output = script.trim();
+ }
+
+ try
+ {
+ writer = new FileWriter( executable );
+
+ List repositories = new ArrayList();
+ repositories.addAll( remoteRepositories );
+ repositories.addAll( remotePluginRepositories );
+
+ getLog().debug( "Resolving functions in script source." );
+
+ writer.write( output );
+ }
+ finally
+ {
+ IOUtil.close( writer );
+ }
+ }
+
+ /**
+ * Add the Ant task used to make the command executable.
+ * @throws MojoExecutionException
+ */
+ private void chmod() throws MojoExecutionException
+ {
+ Commandline chmod = new Commandline();
+ chmod.setExecutable( "chmod" );
+
+ chmod.createArg().setLine( "+x" );
+
+ chmod.createArg().setLine( executable.getAbsolutePath() );
+
+ StreamConsumer consumer = newDebugStreamConsumer();
+
+ try
+ {
+ int result = CommandLineUtils.executeCommandLine( chmod, consumer, consumer );
+
+ if ( result != 0 )
+ {
+ throw new MojoExecutionException( "Failed to chmod script file (exit
value != 0). Please see debug output for more information." );
+ }
+ }
+ catch ( CommandLineException e )
+ {
+ throw new MojoExecutionException( "Failed to chmod +x: " +
executable + ". Reason: " + e.getMessage(), e );
+ }
+ }
+
+
+ public StreamConsumer newDebugStreamConsumer()
+ {
+ return new StreamConsumer()
+ {
+ public void consumeLine( String line )
+ {
+ if ( getLog().isDebugEnabled() )
+ {
+ getLog().debug( line );
+ }
+ }
+ };
+ }
+
+ public StreamConsumer newInfoStreamConsumer()
+ {
+ return new StreamConsumer()
+ {
+ public void consumeLine( String line )
+ {
+ if ( getLog().isInfoEnabled() )
+ {
+ getLog().info( line );
+ }
+ }
+ };
+ }
+
+}
Property changes on:
trunk/mojo/mojo-sandbox/shell-maven-plugin/src/main/java/org/codehaus/mojo/shell/ShellExecMojo.java
___________________________________________________________________
Name: svn:keywords
+ "Author Date Id Revision"
Name: svn:eol-style
+ native
________________________________
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
--
Jerome Lacoste
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email