maven-antrun-plugin skip target if any of two possible conditions holds

孤者浪人 提交于 2019-12-19 09:38:50

问题


I can pass two properties A and B to maven via

 mvn test -DA=true

or

 mvn test -DB=true

If either A or B is defined i want a target to be skipped. I found it was possible when only A was considered like this:

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

Now B has to be considered too. Can this be done?

matthias


回答1:


I would do that with an external build.xml file allowing you to define multiple target combined with antcall and so using one additional dummy target, just to check the second condition.

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>

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

Alternative solution if you only have 2 conditions: using the <skip> configuration attribute for one condition (i.e. maven stuff) and the unless (i.e. ant stuff) for the other condition:

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


来源:https://stackoverflow.com/questions/15021439/maven-antrun-plugin-skip-target-if-any-of-two-possible-conditions-holds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!