Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant: How do I set a properties from a comma-separated list passed in on command line?

Tags:

ant

I'm using Ant 1.8.1. If I passed in an argument on the command line ...

-DenableProperties=abc,def,ghi,jkl

How do I set individual properties (to true/false) in my Ant script?

<property name="abc" value="???" />
<property name="def" value="???" />

Note that in the above example, I'd want Ant to have access to a property "${abc}" that is set to true, whereas if it tried to access the property "${mno}" that property would be false, or at least some value other than true.

Thanks, - Dave

like image 725
Dave Avatar asked Jun 16 '11 14:06

Dave


People also ask

How do you set an Ant property?

The <property> task is used to set the Ant properties. The property value is immutable, once the value is set you cannot change it. To set a property to a specific value you use Name/value assignment. To set a property to a location you use Name/location assignment.

How do I override property value in Ant?

Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.

What is Basedir in build XML?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.


2 Answers

How to decide when an item of your property $enableproperties is to be set to false or true ?
Some criteria missin..
From my understanding of your question you might try something like that, my solution is based on Ant plugin Flaka

starting with ant -f demo.xml -Denableproperties=abc#t,def#t,ghi,jkl#t,mno
means all items in the list that should be set to true in your script have to be propertyname#t
others will be set to false

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

    <fl:for var="p" in="split('${enableproperties}', ',')">
      <fl:let>#{split(p, '#')[0]} := #{split(p, '#')[1] == 't' ? 'true' : 'false'}</fl:let>
    </fl:for>

    <echo>
$${abc} = ${abc}
$${def} = ${def}
$${ghi} = ${ghi}
$${jkl} = ${jkl}
$${mno} = ${mno}
    </echo>

</project>

output

 [echo] ${abc} = true
 [echo] ${def} = true
 [echo] ${ghi} = false
 [echo] ${jkl} = true
 [echo] ${mno} = false

Disclosure = i'm participating as committer in the Flaka project

like image 34
Rebse Avatar answered Sep 19 '22 14:09

Rebse


Can't think of a way to do this in core Ant. You could do it with the For task of ant-contrib.

<project default="test">

  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="C:/lib/ant-contrib/ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>

  <target name="test">
    <for list="${enableProperties}" param="prop">
      <sequential>
         <property name="@{prop}" value="true"/>
      </sequential>
    </for>
    <for list="${enableProperties}" param="prop">
      <sequential>
         <echo message="@{prop}=${@{prop}}"/>
      </sequential>
    </for>
  </target>

</project>

Output:

$ ant -DenableProperties=abc,def,ghi,jkl
Buildfile: build.xml

test:
     [echo] abc=true
     [echo] def=true
     [echo] ghi=true
     [echo] jkl=true

BUILD SUCCESSFUL
Total time: 0 seconds
like image 195
sudocode Avatar answered Sep 21 '22 14:09

sudocode