Skip to content

Maven-exec-plugin

1. What is it, what is it for?

Sometimes in the lifecycle process of our project, we need to do external tasks, which for example we have defined in a shell script and that are included in some specific maven phase.

Maven gives us the flexibility to integrate those external executions, but within the lifecycle of our project with maven.

2. exec-maven-plugin

For this we can execute the exec-maven-plugin plugin that allows us to indicate the script we want to execute and also in which maven phase it has to do it.

In this example we see that it will launch the bootstrap.sh script in the initialize phase, that is at the beginning of the maven execution.

Maven Life Cycle

The maven phases are on the official site https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

<build>
  <plugins>
    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
        <groupId>org.codehaus.mojo</groupId>
        <version>1.6.0</version>
        <executions>
            <execution>
                <id>Download Necessary File before Build</id>
                <phase>initialize</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>${basedir}/bootstrap.sh</executable>
                </configuration>
            </execution>
        </executions>
    </plugin>

Our executable shell script (remember chmod +x) can now do whatever it wants, for example download a file (in this case an image):

#!/bin/bash
echo "Executing previous Native Task Before Maven artifact Building";
echo "Downloading a necessary image"
wget https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Oceanus-es.png/320px-Oceanus-es.png
echo "Succes Donwloaded image"

3. Execute script at the end of execution (INSTALL PHASE)

In this case the doSomeNativeTask.sh script will be executed in the install phase, where our tests and our compilation have already been executed.

    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
        <groupId>org.codehaus.mojo</groupId>
        <executions>
            <execution>
                <id>Do Some Native Task</id>
                <phase>install</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>${basedir}/doSomeNativeTask.sh</executable>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
For example this script, in this case will delete the file that has been downloaded and used for the maven execution
#!/bin/bash
echo "Starting doSomeNativeTask script";
#whoami;  # Prints the current user
#printenv;  # Prints environment variables
rm ./320px-Oceanus-es.png   # Delete the file downloaded by bootstrap.sh
echo "End script";

4. DEMO

Source code of a simple working demo https://github.com/deadveloper666/maven-exec-plugin-demo