Check multiple properties are set and not blank (ant)
Asked Answered
G

1

5

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?

Goldcrest answered 19/8, 2012 at 22:55 Comment(1)
Just to be sure that your targets are called. If they are there is vars in ant-contrib extension. It's not property it's variables that could be modified during executionOstosis
A
9

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
Autotransformer answered 20/8, 2012 at 20:21 Comment(3)
Close but my original command includes the second parameter with no value ("ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=") and this macro sets ${conf.der} to an empty stringGoldcrest
@Craig: Answer updated. The error was not using the interpolated value in the <not> condition in the macro. See updated output which successfully sets conf.dir after calling the macro with -Dconf.dir_ext= on the command line.Autotransformer
Nice work, and thanks for doing the full cycle with tests and showing your output.Goldcrest

© 2022 - 2024 — McMap. All rights reserved.