如何从Maven2中运行ant目标?

4

我该如何在命令行中使用antrun插件运行特定的目标?

mvn antrun: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>
4个回答

8
我该如何在命令行上使用antrun插件运行特定目标?
严格来说,你不能这样做。
但是你可以:
1. 提供插件级别的配置
<plugin>
   <artifactId>maven-antrun-plugin</artifactId>
   <configuration>
       ....
   </configuration>
</plugin>

无论插件是从命令行还是生命周期的一部分调用,都会使用此配置。

2. 提供执行级别的配置(这就是您所做的)

<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>

然后调用插件绑定的阶段(在这种情况下是deploy)。

3. 为特殊的default-cli执行Id提供执行级别的configuration

<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>

从 Maven 2.2.0 开始(请参见 MNG-3401),可以使用名为default-cli的特殊 executionId,在 POM 中单独配置从命令行直接调用的目标,与其他插件调用分开。换句话说,以上配置仅在从命令行调用插件时使用。

但是,在任何情况下,您都不能在 configuration 元素内部调用特定的 Ant target。您可能可以通过 messing with profiles 来实现类似的功能,但如果您真的想走这条路,我的建议是使用 Ant。

参考资料


谢谢,@Pascal。有点让人沮丧的是无法这样做,因为直接运行 ant 显然会产生不同于在 Maven 中运行的结果。 - Armand

2
您可以通过一些巧妙的方法来实现。在pom.xml文件中:
...
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <target>
            <ant target="trampoline" />
        </target>
    </configuration>
</plugin>
...

在 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>
....

然后,通过运行:

$ mvn antrun:run -DmvnAntTarget=testTarget

将运行蚂蚁的testTarget


1
另一个解决方法是将pom更改为 <ant target="${mvnAntTarget}" />,然后只需删除“trampoline”。 - rveach

1
请参考以下示例:http://docs.codehaus.org/display/MAVENUSER/Antrun+Plugin 基本上,您需要在常规build.xml中编写ant目标。 然后,在配置下定义一个单独的<target>,在其中动态决定buildFile名称和targetName,并执行。
<ant andfile="${buildFile}" target="${targetName}" inheritAll="true" inheritRefs="true"/>

0

我不太确定这是它不起作用的原因,但你使用的语法已经过时了。你应该有类似这样的东西:

<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>

更多详情请参见: http://maven.apache.org/plugins/maven-antrun-plugin/usage.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接