ant脚本中的循环和正则表达式的ant-contrib

时间:2013-10-01 11:23:13

标签: regex ant ant-contrib

我有一个使用ant的要求,目标应该提取以逗号分隔的两个参数,这两个参数在传递的类似参数对的长列表中分隔,这些参数是以分号分隔的。目前我正在做这样的事情:

<?xml version="1.0"?>

<project name="" basedir="." default="test" xmlns:ac="antlib:net.sf.antcontrib">
<target name="test" >
<echo message="Hey There I am using What's App" />
<ac:for list="asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu" delimiter=";" param="val">
<ac:sequential>
<ac:propertyregex property="param1"
            input="@{val}"
            regexp="([^\.]*)\,.*"
            select="\1"
            casesensitive="true" />
<ac:propertyregex property="param2"
            input="@{val}"
            regexp=".*,([^\.]*)"
            select="\1"
            casesensitive="true" />
<echo message = "val = ${param1}"/>
<echo message = "value = ${param2}"/>
</ac:sequential>
</ac:for>
</target>
</project>

但我得到的输出为:

Buildfile: /tmp/Manish/build.xml

test:
 [echo] Hey There I am using What's App
 [echo] val = asdfg
 [echo] value = dasfdf
 [echo] val = asdfg
 [echo] value = dasfdf
 [echo] val = asdfg
 [echo] value = dasfdf

所以这是循环3次(正确)但只通过for循环参数传递的第一个值。我正在犯一些明显的错误吗?

谢谢, Manish Joshi

5 个答案:

答案 0 :(得分:2)

尝试使用foreach而不是for,并将propertyregex放入单独的目标中。这是我的蚂蚁脚本的一个例子,它基本上做同样的事情。

<target name="loadTestStatic" depends="setTargetEnv,setPassword">
    <loadfile property="controlFile" srcFile="${projectDir}/test/config/static/controlFile.txt"/>

    <foreach list="${controlFile}" delimiter="${line.separator}" param="descriptor" target="loadConfig"/>
</target>

<target name="loadConfig">
    <if>
        <matches string="${descriptor}" pattern="^camTool:"/>
        <then>
            <propertyregex property="camToolFile"
                           input="${descriptor}"
                           regexp="camTool:(.*)"
                           select="\1"
                           casesensitive="false" />
            <echo message="Got cam tool file ${camToolFile}"/>
            <camTool file="${camToolFile}"/>
        </then>
        <else>
            <!-- todo: add CM Tool, SQL as required -->
            <echo message="Unexpected config ${descriptor} ignored"/>
        </else>
    </if>
</target>

答案 1 :(得分:1)

另一种方法是使用脚本语言,如groovy

  <groovy>
     <arg value="asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu"/>

     args[0].tokenize(";").each {
        def m = it.tokenize(",")

        println "val   = ${m[0]}"
        println "value = ${m[1]}"
     }
  </groovy>

答案 2 :(得分:0)

或者使用Ant插件Flaka,f.e。 :

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

<!-- with cvs property -->
<property name="foobar" value="asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu"/>
 <fl:for var="item" in="split('${foobar}', ';')">
  <fl:let>
   param1 ::= split(item, ',')[0]
   param2 ::= split(item, ',')[1]
  </fl:let>
  <echo>
   $${param1} => ${param1}
   $${param2} => ${param2}
  </echo>
 </fl:for>

 <!-- with list inline -->
 <fl:for var="item" in="split('asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu', ';')">
  <fl:let>
   param1 ::= split(item, ',')[0]
   param2 ::= split(item, ',')[1]
  </fl:let>
  <echo>
   $${param1} => ${param1}
   $${param2} => ${param2}
  </echo>
 </fl:for>

</project>

注意param1中的双重'::'= = split(item,',')[0] 表示覆盖任何(也是userproperties,通过-Dkey = value定义为命令行参数)现有属性 而':='创建一个属性,但如果属性已经存在则不会覆盖。

答案 3 :(得分:0)

<target name="myTarget">
        <ac:propertyregex property="param1"
                input="${myValue}"
                regexp="([^\.]*)\,.*"
                select="\1"
                casesensitive="true" />
        <ac:propertyregex property="param2"
                input="${myValue}"
                regexp=".*,([^\.]*)"
                select="\1"
                casesensitive="true" />
        <echo message = "val = ${param1}"/>
        <echo message = "value = ${param2}"/>
    </target>

    <ac:for list="asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu" delimiter=";" param="val">
    <ac:sequential>
        <antcall target="myTarget">
            <param name="myValue" value="@{val}" />
        </antcall>  
    </ac:sequential>
    </ac:for>

答案 4 :(得分:-1)

Ant中的属性是不可变的。您需要使用ant-contrib中的variable task(尽管不鼓励)取消设置属性:

<ac:for list="asdfg,dasfdf;vxxexqxx,hyyypyly;dksfgsgdgf,abaifuacu" delimiter=";" param="val">
<ac:sequential>
<ac:propertyregex property="param1"
            input="@{val}"
            regexp="([^\.]*)\,.*"
            select="\1"
            casesensitive="true" />
<ac:propertyregex property="param2"
            input="@{val}"
            regexp=".*,([^\.]*)"
            select="\1"
            casesensitive="true" />
<echo message = "val = ${param1}"/>
<echo message = "value = ${param2}"/>
<ac:var name="param1" unset="true"/>
<ac:var name="param2" unset="true"/>
</ac:sequential>
</ac:for>