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

Regex ant脚本中循环和正则表达式的ant contrib,regex,ant,ant-contrib,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

我有一个使用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>
因此,这将循环3次(正确),但仅通过for循环参数中传递的第一个值。我犯了什么明显的错误吗

谢谢,
Ant中的Manish Joshi属性是不可变的。您将需要使用来自ant contrib(尽管不鼓励使用)来取消设置属性:

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

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


args[0]。标记化(;“”)。每个{
def m=it.tokenize(“,”)
println“val=${m[0]}”
println“值=${m[1]}”
}

或者使用Ant插件,例如:


参数1::=拆分(项,,)[0]
参数2::=拆分(项,,)[1]
$${param1}=>${param1}
$${param2}=>${param2}
参数1::=拆分(项,,)[0]
参数2::=拆分(项,,)[1]
$${param1}=>${param1}
$${param2}=>${param2}
请注意param1::=split(项,,)中的双精度“::”[0] 意味着重写任何现有属性(也是userproperties,通过-Dkey=value定义为命令行参数)
而“:=”创建一个属性,但如果属性已经存在,则不会覆盖该属性。

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

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



ant缺少一个重要方面:它不是一种脚本语言。属性不是变量——它们是不可变的。您的要求与您的工具不匹配。ant中没有参数。ant中有参数。这就是macrodef的用途。我一直觉得有趣的是,现在很少有人使用macrodef,并尝试使用导致过于复杂和无法维护的ant脚本的目标来做任何事情。
  <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>
<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>
<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>
<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>