如何使用maven-antrun-plugin有条件地执行任务?

vau*_*han 7 maven-2 maven-antrun-plugin

我需要执行一些ant命令,具体取决于作为maven build命令的参数传入的环境变量.

目前我有3个任务块,只执行没有条件的任务块.

<tasks name="isProdCheck">
  <condition property="isProd">
    <equals arg1="${environment}" arg2="PROD" />
  </condition>
</tasks>

<tasks if="isProd" depends="isProdCheck">
...
</tasks>

<tasks>
... I am the only block executed
</tasks>
Run Code Online (Sandbox Code Playgroud)

我做错了什么,有更好的方法吗?

Pas*_*ent 11

首先,根据Maven 1.x网站,目前maven 1.x的稳定版本是1.1版本,而不是1.4版本.其次,没有AntRun插件版本1.7,据我所知,这是一个Maven 2插件.第三,你使用的语法看起来非常类似于使用属性,这又是关于Maven 2的.

所以,我可能会遗漏一些东西但是,这非常令人困惑,你应该在你的问题中澄清这些要点.

无论如何,正如你明确提到的Maven 1,我会试着回答.如果我记得很清楚,我会写一个自定义的目标,并使用果冻core:ifcore:when.为此,请提供以下内容maven.xml:

<project xmlns:j="jelly:core" xmlns:ant="jelly:ant">
  <goal name="my-goal">
    <j:if test="${environment == 'PROD'}">
      <ant:xxx .../>
    </j:if>
  </goal>
</project>
Run Code Online (Sandbox Code Playgroud)

我真的不确定语法,所有这些Maven 1的东西都太远了,我没有测试它(我太懒了,不能安装Maven 1).但我想你会的.该脚本参考可以帮助你.

说实话,我真的希望你有充分的理由喜欢Maven 1.x而不是Maven 2.x :)

更新:看来OP实际上正在使用Maven 2,所以我会相应地更新我的问题.要实现所需的行为,您可以使用Ant-contrib的if任务,如下所示:

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                  classpathref="maven.plugin.classpath" />
                <if>
                  <equals arg1="${foo}" arg2="bar" />
                  <then>
                    <echo message="The value of property foo is bar" />
                  </then>
                  <else>
                    <echo message="The value of property foo is not bar" />
                  </else>
                </if>
              </tasks>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>20020829</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
Run Code Online (Sandbox Code Playgroud)

然后打电话mvn compile -Dfoo=bar(这只是一个例子).

但是,这一切并不是做事的"母亲方式".现在我对你想要做的事情了解得更好(但并不完全是因为你没有解释你的最终目标),我认为使用构建配置文件会更合适,并且在阅读了你自己的答案后,我认为你过度复杂的事情(你走错了路).

我知道你是一个Maven初学者,但我建议尝试使用它,而不是回到Ant或你不会得到它的好处.此外,在打开问题时,您应该更好地解答一般问题,而不是要求特定的解决方案.在这里,我无法提供更多指导,因为我不知道你真正想要实现的目标.


Rhu*_*lus 6

您不需要在使用AntContrib之后再使用maven-antrun-plugin 1.5<target>而不必<tasks>根据插件使用情况使用。此标记的作用方式与相同,但在此标记中,您可以添加以下示例中的条件。

<properties>
    <execute.my.target>true</execute.my.target>
</properties>

<build>
    ...
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>config</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <!-- this target will be executed always -->
                        <target>
                            <echo message="Hello there, I'm a simple target" />
                        </target>

                        <!-- 
                            This target will be executed if and only if
                            the property is set to true
                        -->
                        <target name="my-target" if="${execute.my.target}">
                            <echo message="Conditional target..." />
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...
</build>
Run Code Online (Sandbox Code Playgroud)

上面的代码始终执行第一个目标,但是第二个目标取决于属性值。您也可以为父项目和子模块项目配置它,在<pluginsManagement>标签上定义插件并在子模块上调用一些属性,然后调用插件。

  • 在 1.8 中不起作用。如果我在 `&lt;configuration&gt;` 中放了两个 `target`,插件只执行最后一个(并且 Intellij IDEA 在 pom.xml 上将最后一个目标标记为错误)。 (4认同)
  • 正如@Dherik 所说,不能在`&lt;configuration&gt;` 下使用多个目标。但是当您定义多个 `&lt;execution&gt;s`(具有唯一 ID)并且每个 `&lt;execution&gt;` 具有一个 `&lt;target&gt;` 时有效 (2认同)