Search code examples
antmaven-2maven-antrun-plugin

How to run an ant target from Maven2?


How do i run a specific target with the antrun-plugin from the command line?

mvn antrun:run doesn't make it run.


<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>myExecution</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <ant target="myTarget" inheritRefs="true">
                                    ...
                                </ant>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>

                <dependencies>
                    ...
                </dependencies>
            </plugin>
            ...
        </plugins>
        ...
    </build>
    ...
</project>

Solution

  • How do i run a specific target with the antrun-plugin from the command line?

    To strictly answer this question, you can't, and you don't.

    What you can do is either:

    1. provide a plugin-level configuration

    <plugin>
       <artifactId>maven-antrun-plugin</artifactId>
       <configuration>
           ....
       </configuration>
    </plugin>
    

    And this configuration will be used when invoking the plugin (regardless of how the plugin is invoked: from the cli, a part of the lifecycle).

    2. provide an execution-level configuration (which is what you did)

    <plugin>
       <artifactId>maven-antrun-plugin</artifactId>
       <executions>
           <execution>
               <id>myExecution</id>
               <phase>deploy</phase>
               <goals>
                   <goal>run</goal>
               </goals>
               <configuration>
                   <tasks>
                       <ant target="myTarget" inheritRefs="true">
                           ...
                       </ant>
                   </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    And then invoke the phase to which the plugin is bound (deploy in this case).

    3. provide an execution-level configuration for the special default-cli execution Id

    <plugin>
       <artifactId>maven-antrun-plugin</artifactId>
       <executions>
           <execution>
               <id>default-cli</id>
               <configuration>
                   <tasks>
                       <ant target="myTarget" inheritRefs="true">
                           ...
                       </ant>
                   </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    As of Maven 2.2.0 (see MNG-3401), goals invoked directly from the command line can be configured in the POM separately from other plugin invocations using a special executionId called default-cli. In other words, the above configuration would be only used when invoking the plugin from the command line.

    But in any case, you can't invoke a specific Ant target inside a configuration element. You could maybe mess with profiles to implement something approaching but, if you really want to go this direction, my advice would be to use Ant.

    References