2

给定一个 CSV 蚂蚁属性,

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

我怎样才能得到第一个元素(即这里的“mod1”)?我想执行一个将“mod1”作为参数之一的命令。

此外..我不能将这个原始的“module.list”属性修改为列表或其他任何东西..虽然我可以从中创建另一个列表、属性等..

任何帮助表示赞赏..谢谢

4

6 回答 6

1

如果您想使用所有变量,还可以查看 Ant-Contrib 任务 for 和 foreach。

<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

为了用户任务,不要忘记声明这个任务 def :

<taskdef resource="net/sf/antcontrib/antlib.xml" />
于 2011-11-29T14:53:11.187 回答
1

根据 module.list 的实际内容,您也许可以使用 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>

该任务会进行大量的字符串操作,因此如果 module.list 的内容可以包含特殊的路径字符,那么这种方法将不起作用。在这种情况下,我会选择更通用的答案之一。

于 2009-08-29T21:15:13.257 回答
1

Ant-Contrib进行救援。

您可以使用propertyregexAnt-Contrib 中的任务来提取逗号分隔字符串的第一部分,如下所示:

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

对于您的第二个问题:Ant 属性是故意不可变的,因此我通常建议不要进行依赖于更改属性值的设计。但如果这是您需要的,varAnt-Contrib 的任务允许您这样做。此外,Ant-Contrib 中的一些属性任务,如propertyregex上面提到的,有一个可选override属性,允许它们更改目标属性的值。

于 2009-08-29T22:45:41.937 回答
1

这是完成您所描述的一种方法。

  1. 将 CSV 写入临时文件
  2. 用replaceregexp解析它
  3. 将清理后文件的内容读入新属性
  4. 删除临时文件

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

于 2009-08-29T14:13:03.420 回答
0

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.

于 2009-09-03T20:18:33.607 回答
0

使用脚本任务。您可以使用 Javascript 或 Beanshell 编写脚本,并使用 Ant API 设置您可以从其他 ant 任务访问的属性。

于 2009-08-29T10:17:56.313 回答