Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 666
Armand Avatar asked Dec 22 '22 22:12

Armand


2 Answers

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.

like image 41
Gima Avatar answered Dec 27 '22 09:12

Gima


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

  • Guide to Configuring Default Mojo Executions
like image 84
Pascal Thivent Avatar answered Dec 27 '22 11:12

Pascal Thivent