Check multiple properties are set and not blank (ant)

Craig picture Craig · Aug 20, 2012 · Viewed 9.5k times · Source

I'm in a situation that involves running an ant build with optional parameters that are always specified but but not always defined, like so

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

If the parameters are not given values on the command line they will be by loading a .properties file. I have the following code that will check if the property isset and is not blank.

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

Since there are multiple properties it seems like I should write a target that will do the same actions for each property rather than repeat that code every time.

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

But AFAIK an antcall will not set a global property. How then can I write a target that will accept the name of a parameter it needs to check is set and is not blank, and then copy that in to a parameter that other targets can use?

Answer

Christopher Peisert picture Christopher Peisert · Aug 20, 2012

Rather than using a target you could use a macro to conditionally set properties based on whether or not another property is set to a non-empty value.

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

Output

$ 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


Alternate Property Value

This macro also allows you set the property to a different value than the one provided on the command line.

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

Output

$ 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