How to run an ant target from Maven2?

13,421

Solution 1

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

Solution 2

You can, by being sneaky.

In pom.xml:

...
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <target>
            <ant target="trampoline" />
        </target>
    </configuration>
</plugin>
...

In build.xml:

...
<target name="trampoline">
    <echo message="Executing target '${mvnAntTarget}'"/>
    <antcall target="${mvnAntTarget}" />
</target>

<target name="testTarget">
    <echo message="Yay, I'm a test target.."/>
</target>
....

And then, by running:

$ mvn antrun:run -DmvnAntTarget=testTarget

The Ant's testTarget will be run.

Solution 3

Refer to the example at : http://docs.codehaus.org/display/MAVENUSER/Antrun+Plugin Basically write your ant targets in a regular build.xml. Then define a single <target> under configuration where you dynamically decide what is the buildFile name and targetName and do a

<ant andfile="${buildFile}" target="${targetName}" inheritAll="true" inheritRefs="true"/>
Share:
13,421
Armand
Author by

Armand

Updated on June 11, 2022

Comments

  • Armand
    Armand almost 2 years

    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>