如果两个可能的条件中的任何一个成立,则maven-antrun-plugin跳过目标

时间:2013-02-22 10:00:30

标签: maven ant conditional maven-antrun-plugin

我可以通过

将两个属性A和B传递给maven
 mvn test -DA=true

 mvn test -DB=true

如果定义了A或B,我想要跳过目标。我发现只有A才被认为是这样的:

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <id>skiptThisConditionally</id>
        <phase>test</phase>
        <configuration>
          <target name="anytarget" unless="${A}">
             <echo message="This should be skipped if A or B holds" />
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

现在必须考虑B。可以这样做吗?

的Matthias

1 个答案:

答案 0 :(得分:4)

我会使用外部build.xml文件执行此操作,允许您定义多个目标并结合antcall,因此使用一个额外的虚拟目标,只是为了检查第二个条件。

<强>的pom.xml

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>skiptThisConditionally</id>
            <phase>test</phase>
            <configuration>
                <target name="anytarget">
                    <ant antfile="build.xml"/>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

和build.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="SkipIt" default="main">
       <target name="main" unless="${A}">
          <antcall target="secondTarget"></antcall>
       </target>
       <target name="secondTarget" unless="${B}">
          <echo>A is not true and B is not true</echo>
       </target>
     </project>

替代解决方案,如果您只有两个条件:对一个条件(即maven东西)使用<skip>配置属性,对另一个条件使用unless(即ant东西):

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>skiptThisConditionally</id>
            <phase>test</phase>
            <configuration>
                <skip>${A}</skip>
                <target name="anytarget" unless="${B}">
                    <echo>A is not true and B is not true</echo>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>