-->

maven-antrun-plugin skip target if any of two poss

2020-03-25 13:51发布

问题:

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>