...">

How to extract 1st item from ant CSV property

given the CSV ant property,

<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

      

how can i get the first item (ie "mod1" here)? I want to execute a command that will take "mod1" as one of the arguments.

Also, I cannot change this original module.list property to a list or whatever. Although I can create another list, property, etc. from this.

Any help is appreciated. Thanks to

+2


source to share


6 answers


This is one way to achieve what you described.

  • Writing CSV to a temporary file
  • parse it with replaceregexp
  • read the contents of the cleaned up file in a new property
  • Delete temporary file



<target name="parse" description="Example of how to parse the module.list property to extract the first value in the CSV"> 
    <property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

    <tempfile description="Generate a unique temporary filename for processing"  
    prefix="module.list" suffix="csv" destdir="${basedir}" property="tmpFile" />

    <concat description="Write the value of module.list to the temporary file" 
        destfile="${tmpFile}">${module.list}</concat>

    <replaceregexp description="filter the temporary file using the regex expression to find the first occurance of a , and all characters afer and replace with nothing"
        file="${tmpFile}"
        match=",.*"
        replace=""
        byline="true"/>

    <loadresource description="read the contents of the scrubbed temporary file into the mod1 property"
        property="mod1">
        <file file="${tmpFile}"/>
    </loadresource>

    <delete description="remove the temporary file" 
    file="${tmpFile}" />

    <!--Now you have the parsed value in the mod1 property -->
    <echo message="mod1=${mod1}" />

</target>

      



+1


source


Depending on the actual content of the .list module, you can use pathconvert:

<project>
  <property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

  <pathconvert property="module.1">
    <path path="${module.list}"/>
    <chainedmapper>
      <flattenmapper/>
      <mapper type="regexp" from="(.*?),.*" to="\1"/>
    </chainedmapper>
  </pathconvert>

  <echo>${module.1}</echo>
</project>

      



This task does a lot of string manipulation, so if the contents of module.list can contain special path characters, this approach will not work. In this case, I would go with one of the more general answers.

+1


source


Ant-Contrib for the Rescue.

You can use a propertyregex

task from Ant -Contrib to extract the first part of a comma delimited string like this

<propertyregex property="module.first"
               input="${module.list}"
               regexp="^([^,]*),"
               select="\1"/>

      


For your second question: Ant properties are immutable, so I would recommend against projects that rely on changing property values. But if that's what you want, the var

Ant -Contrib task lets you do it. Additionally, some of the property tasks in Ant -Contrib, such propertyregex

as the ones mentioned above, have an optional attribute override

that allows them to change the value of the target property.

+1


source


You can also take a look at Ant -Contrib tasks for and foreach if you want to use all variables.

<echo message="The first five letters of the alphabet are:"/>
<for list="a,b,c,d,e" param="letter">
  <sequential>
    <echo>Letter @{letter}</echo>
  </sequential>
</for>

      

http://ant-contrib.sourceforge.net/tasks/tasks/index.html

For the For Task user, remember to declare this task as def:

<taskdef resource="net/sf/antcontrib/antlib.xml" />

      

+1


source


Use a script task . You can write a script in Javascript or Beanshell and use the Ant API to set a property that you can access from other Ant tasks.

0


source


First question

With the new Ant addon = Flaka you can use =

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

  <property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>

  <target name="main">   
    <!-- simple echo -->
    <fl:echo>xtractedvalue => #{split('${module.list}',',')[0]}</fl:echo>
    <!-- create property for further processing.. -->
    <fl:let>
      xtractedvalue := split('${module.list}',',')[0]
    </fl:let>
    <echo>$${xtractedvalue} => ${xtractedvalue}</echo> 
  </target> 

</project>

      

Second question

properties are usually immutable once set in ant, but with Flaka you can overwrite an existing property like this =

  <property name="foo" value="bar"/>
  <fl:let>foo ::= 'baz'</fl:let>

      

will replace the existing property foo with the new value baz.

0


source







All Articles