【问题标题】:How to run an ant target from Maven2?如何从 Maven2 运行 ant 目标?
【发布时间】:2011-04-27 14:40:48
【问题描述】:

如何从命令行使用 antrun-plugin 运行特定目标?

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>

【问题讨论】:

    标签: ant maven-2 maven-antrun-plugin


    【解决方案1】:

    如何从命令行使用 antrun-plugin 运行特定目标?

    要严格回答这个问题,你不能,也不能。

    你可以做的是:

    1。提供插件级configuration

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

    并且在调用插件的时候会用到这个配置(不管插件是怎么调用的:从cli,生命周期的一部分)。

    2。提供执行级别configuration(这就是您所做的)

    <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),直接从命令行调用的目标可以在 POM 中与其他插件调用分开配置,使用名为 default-cli 的特殊 executionId。也就是说,上述配置只会在命令行调用插件时使用。

    但无论如何,您都不能在configuration 元素内调用特定的Ant target。你可能会弄乱配置文件来实现一些接近的东西,但是,如果你真的想朝这个方向发展,我的建议是使用 Ant。

    参考文献

    【讨论】:

    • 谢谢,@Pascal。不能这样做有点令人沮丧,因为显然直接运行 ant 会产生与在 Maven 中运行不同的结果。
    【解决方案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
    

    将运行 Ant 的 testTarget

    【讨论】:

    • 另一种解决方法是制作 pom: &lt;ant target="${mvnAntTarget}" /&gt; 并删除 trampoline
    【解决方案3】:

    参考示例:http://docs.codehaus.org/display/MAVENUSER/Antrun+Plugin 基本上在常规 build.xml 中编写您的 ant 目标。 然后在配置下定义一个&lt;target&gt;,您可以在其中动态决定什么是 buildFile 名称和 targetName 并执行

    <ant andfile="${buildFile}" target="${targetName}" inheritAll="true" inheritRefs="true"/>
    

    【讨论】:

      【解决方案4】:

      我不太确定这是它不起作用的原因,但您使用的语法已被弃用。你应该有类似的东西:

      <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

      【讨论】:

        猜你喜欢
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多