2013-12-08 102 views
0

你好我有2行csv文件:Ant腳本+解析csv文件

MF1,EG1,EG2,BR1,BR2

MF2,EG2,EG3,BR2,BR3

我想要使​​用ant將每個逗號分隔值存儲在單獨的變量中。

我能夠解析行,但不是單個值,因爲列表不支持嵌套。

下面是我的腳本:

<?xml version="1.0" encoding="UTF-8"?> 

<project name="ForTest" default="getLine" basedir="." 
    xmlns:ac="antlib:net.sf.antcontrib"> 
    <taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml" 
     classpath="C:\Manju\apache-ant-1.8.4\ant-contrib-1.0b3-bin\ant-contrib\ant-contrib-1.0b3.jar" /> 
    <loadfile property="message" srcFile="build_params.csv" /> 
    <target name="getLine"> 
     <ac:for list="${message}" delimiter="${line.separator}" param="val"> 
      <sequential> 
       <echo>@{val}</echo> 
       <property name="var1" [email protected]{val}/> 
      </sequential> 
     </ac:for> 
    </target> 
    <target name="parseLine" depends="getLine"> 
     <for [email protected]{val} delimiter="," param="letter"> 
      <sequential> 
       <echo>@{letter}</echo> 
      </sequential> 
     </for> 
    </target> 



</project> 

目標parseline是給錯誤說的名單期待打開引號。幫助表示讚賞。

回答

1

你有沒有考慮嵌入像groovy這樣的腳本語言?遠比打擊ant-contrib更簡單。

<project name="demo" default="run"> 

    <target name="run"> 
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/> 
    <groovy> 
     new File("build_params.csv").splitEachLine(",") { fields -> 
     println "====================" 
     println "field1: ${fields[0]}" 
     println "field2: ${fields[1]}" 
     println "field3: ${fields[2]}" 
     println "field4: ${fields[3]}" 
     println "field5: ${fields[4]}" 
     println "====================" 
     } 
    </groovy> 
    </target> 

</project> 

您可以添加一個特殊的啓動目標自動安裝常規的jar:

<target name="bootstrap"> 
    <mkdir dir="${user.home}/.ant/lib"/> 
    <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/code 
haus/groovy/groovy-all/2.2.1/groovy-all-2.2.1.jar"/> 
    </target> 
+0

感謝馬克,我用Groovy和能夠解決。 – user3080775

0

一件事,你parseLine目標應該這樣開始:

<for list="@{val}" delimiter="," param="letter"> 

注意引號@{val}