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&l开发者_运维问答t;/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>
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
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.
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"/>
I'm not too sure that's the reason it doesn't work but the syntax you're using is deprecated. You should have something like:
<configuration>
<target name="myTarget">
<!--
Place any Ant task here. You can add anything
you can add between <target> and </target> in a
build.xml.
-->
</target>
<configuration>
More details here: http://maven.apache.org/plugins/maven-antrun-plugin/usage.html
精彩评论