How to bind maven antrun plugin to the clean phase

user465374 picture user465374 · Oct 20, 2010 · Viewed 14.8k times · Source

I have just translated an ant project into maven however since maven does not really deal with deployment I introduce some antrun into the build. However when I try to execute it the plugin skips my tasks. for exemple when I run mvn clean antrun:run I get the following message: No ant target defined - SKIPPED. the same happends to the second phase in which I am trying to override the deployment phase to do an actual deploy rather to upload to a repository.

Please find below an extract of my pom.xml (type:pom):

            <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>clean</id>
                    <configuration>
                        <task>
                            <echo>Cleaning deployed website</echo>
                        </task>
                        <tasks>
                            <delete dir="${deployRoot}/mydir/${env}"/>
                        </tasks>
                    </configuration>
                    <phase>clean</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>deployment</id>
                    <configuration>
                        <task>
                            <echo>Deploying website</echo>
                        </task>
                        <tasks>
                            <echo>Copying website artifact to deployment </echo>
                            <mkdir dir="${deployRoot}/mydir/${env}" />
                            <unzip
                                src="${project.basedir}/target/${env}.${project.version}.zip"
                                dest="${deployRoot}/mydir/${env}" />
                            <chmod perm="ugo+rx">
                                <fileset dir="${deployRoot}/mydir/${env}/web-exploded/bin">
                                    <include name="**/*.sh" />
                                    <include name="**/*.bat" />
                                </fileset>
                            </chmod>
                        </tasks>
                    </configuration>
                    <phase>deploy</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Answer

Romain Linsolas picture Romain Linsolas · Oct 20, 2010

In your pom.xml, you define two types of executions:

  • One is linked to the clean phase
  • One is linked to the deploy phase. Note, by the way, that for Maven, deploy does not mean deploy my (web-)application on a server but deploy the artifact on a remote repository. Please read the deploy plugin information for more details.

So if you run the command mvn deploy, when the Maven lifecycle reaches the deploy phase, it will run the plugin execution (the second one in your pom.xml).

However, in your case, you are not running the default Maven lifecycle, as your command is mvn antrun:run (I am not considering the clean goal here as it does not matter for the problem). This can be translated in Maven to run the antrun plugin, with the goal run. The problem with that is that you do not define any configuration (which contains the Ant tasks) for a direct call to your Ant plugin.

So two solutions:

  • Bind the second execution to the install phase, and then run the mvn clean install instead of mvn antrun:run. Note that in this case, you will run the whole Maven lifecycle (i.e. compilation, tests, packaging).
  • Create a configuration of this plugin that is not related to any execution. In XML point of view, simply add (or move) the second <configuration> block to be a child of the <plugin> definition.

If you choose the second solution, you will have a pom.xml like this one:

       <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <!-- For cleaning -->
        <executions>
            <execution>
                <id>clean</id>
                <configuration>
                    <task>
                        <echo>Cleaning deployed website</echo>
                    </task>
                    <tasks>
                        <delete dir="${deployRoot}/mydir/${env}"/>
                    </tasks>
                </configuration>
                <phase>clean</phase>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <tasks>
                <echo>Copying website artifact to deployment </echo>
                ...
            </tasks>
        </configuration>
    </plugin>