how do I extract the 1st element from a ant CSV property

房东的猫 提交于 2020-01-13 19:16:22

问题


given a CSV ant property,

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

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

Moreover.. I cannot modify this original "module.list" property to a list or anything else.. although I can create another list,property,etc from this..

Any help is appreciated.. Thanks


回答1:


This is one way to accomplish what you described.

  1. Write the CSV to a temporary file
  2. parse it with replaceregexp
  3. read the contents of the scrubbed file into a new property
  4. Remove the 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>




回答2:


Depending on the actual contents of module.list, you might be able to 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>

That task does a large amount of string manipulation, so if the contents of module.list can contain special path characters, this approach won't work. In that case, I'd go with one of the more generic answers.




回答3:


Ant-Contrib to the rescue.

You can use the propertyregex task from Ant-Contrib to extract the first part of a comma-separated string like this:

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

For your second question: Ant properties are immutable on purpose, so I would generally recommend against designs which rely on changing values of properties. But if that is what you need, the var task from Ant-Contrib allows you to do just that. In addition, some of the property tasks in Ant-Contrib, like propertyregex mentioned above, have an optional override attribute which allow them to change the value of the target property.




回答4:


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

In order to user For Task, don't forget to declare this task def :

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



回答5:


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




回答6:


First question

With a new Ant addon = Flaka you may 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

normally properties are immutable once set in ant, but with Flaka you may overwrite an existing property like that =

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

would overwrite the existing property foo with new value baz.



来源:https://stackoverflow.com/questions/1349959/how-do-i-extract-the-1st-element-from-a-ant-csv-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!