【问题标题】:maven-antrun-plugin skip target if any of two possible conditions holdsmaven-antrun-plugin 如果两个可能条件中的任何一个成立,则跳过目标
【发布时间】:2013-02-22 10:00:30
【问题描述】:
我可以通过两个属性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。这可以做到吗?
马蒂亚斯
【问题讨论】:
标签:
maven
ant
conditional
maven-antrun-plugin
【解决方案1】:
我会使用一个外部 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>
如果您只有 2 个条件,则另一种解决方案:将 <skip> 配置属性用于一个条件(即 maven 的东西)和 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>