0

我有一个使用 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 循环参数中传递的第一个值。我犯了一些明显的错误吗?

谢谢,马尼什乔希

4

5 回答 5

2

尝试使用 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>
于 2014-03-12T14:52:55.993 回答
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>
于 2013-10-01T19:39:59.343 回答
0

或者使用 Ant 插件Flaka, fe :

<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] 中的双 '::' 表示覆盖任何(也是用户属性,通过 -Dkey=value 作为命令行参数定义)现有属性,而 ':=' 创建一个属性但如果属性已经存在,则不会覆盖。

于 2013-11-05T12:03:48.227 回答
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>
于 2015-06-19T13:33:19.703 回答
-1

Ant 中的属性是不可变的。您将需要使用 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>
于 2013-10-01T14:05:05.510 回答