Maven Exec Plugin vs Maven AntRun Plugin for running command-line tool

Maven Exec Plugin supports the following two goals:

  1. exec: to execute external programs (including Java) in a separate process
  2. java: to execute Java programs in the same JVM

Maven AntRun Plugin supports only one goal:

  1. run: to execute the Ant target specified

If you have a requirement to run a command-line tool, e.g. cURL, in an synchronous (blocking) way, you can use the Maven Exec Plugin (exec goal). However, in case you need to run it in an asynchronous (non-blocking) fashion, you can use the Ant exec task (with ‘spawn’ option set as true) with Maven AntRun Plugin. You might want to consider something like this before executing the integration tests which could be dependent on the database being in a clean state. Here is an example:

  • Using Maven Exec Plugin
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <id>drop DB => db_name</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>curl</executable>
        <arguments>
          <argument>-s</argument>
          <argument>-S</argument>
          <argument>-X</argument>
          <argument>DELETE</argument> 
          <argument>http://${db.server}:${db.port}/db_name</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>
  • Using Maven AntRun Plugin
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>drop DB => db_name</id>
      <phase>pre-integration-test</phase>
      <configuration>
        <target>
          <exec executable="curl">
            <arg value="-s" />
            <arg value="-S" />
            <arg value="-X" />
            <arg value="DELETE" />
            <arg value="http://${db.server}:${db.port}/db_name" />
            <spawn value="true">
          </exec>
        </target>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Advertisements

3 responses to “Maven Exec Plugin vs Maven AntRun Plugin for running command-line tool

  1. Pingback: pandroid » PD022 – Maven

  2. Walter January 20, 2015 at 21:27

    Did you mix up your conclusions?
    When I use exec-maven-plugin with
    /bin/sleep

    10

    the second executable is called 10 seconds after the first one.
    I would conclude that Exec is blocking and Antrun scripts can be called non-blocking using the spawn option.

    • Sanchit Bahal January 21, 2015 at 00:06

      Thanks for pointing it out! I have corrected the blog post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: