如何从Maven2运行一个蚂蚁目标?

时间:2010-10-19 16:58:42

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

如何使用命令行中的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>

4 个答案:

答案 0 :(得分:7)

  

如何使用命令行中的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。为特殊configuration执行标识

提供执行级default-cli
<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中与其他插件调用分开配置。换句话说,上述配置仅在从命令行调用插件时使用。

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

参考

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

你可以偷偷摸摸。

在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

答案 3 :(得分: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

相关问题