ant - 检查多个属性是否设置并且不为空( Ant )

标签 ant ant-contrib

我遇到的情况涉及运行带有可选参数的 ant 构建,这些参数总是被指定但并不总是被定义,就像这样

ant -DBUILD_ENVIRONMENT=test -Dusername_ext= -Dconf.dir_ext= -Dcgi-dir_ext=

如果参数未在命令行中指定值,则它们将通过加载 .properties 文件来指定。我有以下代码将检查属性是否已设置并且不为空。
<if>
    <bool>
        <and>
            <isset property="username_ext"/>
            <not>
                <equals arg1="${username_ext}" arg2="" />
            </not>
        </and>
    </bool>
    <then>
        <property name="username" value="${username_ext}" />
    </then>
</if>
<property file="${BUILD_ENVIRONMENT}.properties" />

由于有多个属性,似乎我应该编写一个目标,对每个属性执行相同的操作,而不是每次都重复该代码。
<antcall target="checkexists">
    <property name="propname" value="username"/>
    <property name="paramname" value="username_ext"/>
</antcall>
<antcall target="checkexists">
    <property name="propname" value="conf.dir"/>
    <property name="paramname" value="conf.dir_ext"/>
</antcall>

但 AFAIK an antcall 不会设置全局属性。那么我如何编写一个目标,该目标将接受它需要检查的参数名称是否设置为空,然后将其复制到其他目标可以使用的参数中?

最佳答案

您可以使用宏根据另一个属性是否设置为非空值来有条件地设置属性,而不是使用目标。

<macrodef name="set-property">
  <attribute name="name" />
  <attribute name="if-property-isset" />
  <attribute name="value" default="${@{if-property-isset}}" />

  <sequential>
    <condition property="@{name}" value="@{value}">
      <and>
        <isset property="@{if-property-isset}" />
        <not>
          <equals arg1="${@{if-property-isset}}" arg2="" />
        </not>
      </and>
    </condition>
  </sequential>
</macrodef>


<target name="test-macro">
  <set-property name="username" if-property-isset="username_ext" />

  <set-property name="conf.dir" if-property-isset="conf.dir_ext" />

  <property name="conf.dir" value="default conf directory" />

  <echo message="username = ${username}" />
  <echo message="conf.dir = ${conf.dir}" />
</target>

输出
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml

test-macro:
     [echo] username = jsmith
     [echo] conf.dir = default conf directory

BUILD SUCCESSFUL
Total time: 1 second

替代属性值

此宏还允许您将属性设置为与命令行上提供的值不同的值。
<target name="test-macro">
  <set-property name="username" if-property-isset="username_ext"
      value="It worked!" />

  <set-property name="conf.dir" if-property-isset="conf.dir_ext" />

  <property name="conf.dir" value="default conf directory" />

  <echo message="username = ${username}" />
  <echo message="conf.dir = ${conf.dir}" />
</target>

输出
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml

test-macro:
     [echo] username = It worked!
     [echo] conf.dir = default conf directory

BUILD SUCCESSFUL
Total time: 1 second

关于ant - 检查多个属性是否设置并且不为空( Ant ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030623/

相关文章:

java - 使用属性打开/关闭 Proguard

java - 使 JAR 可用于 JDK 和 JRE

java - apache ant 可变文件类型属性

Ant - 如果条件/和-或

java - Ant 程序终止

windows - ANT 构建脚本如何终止 Windows 进程?

ant - 使用普通 ant 并以文件名作为参数为文件集中的每个文件调用一个任务(没有 contrib 任务,没有自己的任务)

并行执行的antcontrib foreach不会引发错误

eclipse - 从 Eclipse 项目创建多个 JAR

ant-contrib - if/then/else 任务