我嘗試解析ini文件到了我可以在我的Ant腳本使用的屬性。我有以下幾點:ANT:設置屬性不起作用?
<project name="DeployScript" default="deploy-staging" basedir=".">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<!-- The location of the settings.ini file -->
<property name="ini-file" location="../settings.ini" />
<loadfile property="iniConfig" srcFile="${ini-file}"/>
<target name="deploy-staging"
description="Deploy project to staging environment" >
<echo message="Ini file: ${ini-file}" />
<echo message="${lib}" />
<echo message="${store_dir}" />
<echo message="${ant.home}" />
<!--- walk the ini file's lines -->
<foreach list="${iniConfig}"
target="echoMsg"
param="line"
delimiter="${line.separator}" />
<echo message="HERE: ${prevSection}" />
</target>
<property name="prevSection" value="" />
<!-- this is executed for every line in the ini file. -->
<target name="echoMsg">
<!-- strip out the section name, variable name and value (if exists on the line) -->
<propertyregex property="secm"
input="${line}"
regexp="^\[(.*)\]"
select="\1"
casesensitive="false" />
<propertyregex property="name"
input="${line}"
regexp="^([\S]+)\s*=\s*([^;]+)"
select="\1"
casesensitive="false"
defaultValue="" />
<propertyregex property="value"
input="${line}"
regexp="^([\S]+)\s*=\s*([^;]+)"
select="\2"
casesensitive="false"
defaultValue="" />
<!-- overwrite the previous section if we have found a new one. -->
<if>
<isset property="secm" />
<then>
<echo message="PREVSECTION IS SET" />
<property name="prevSection" value="${secm}" />
</then>
</if>
<!-- display the information about the found data -->
<echo message="line = ${line}" />
<echo message="section = ${secm}" />
<echo message="name = ${name}" />
<echo message="value = ${value}" />
<echo message="new last section: ${prevSection}" />
<echo message="----" />
</target>
</project>
我嘗試做的是分析所有的name = value對並把它們的屬性,如:section.name =值;
不知何故,部分不是「echoMsg」目標範圍內記住。我想要記住部分的名字。
所以,
[global]
name=var
name2=val
[section2]
name=var
應該改爲:
global.name=var
global.name2=val
section2.name=var
這是我的ant腳本的輸出:
echoMsg:
[echo] PREVSECTION IS SET
[echo] line = [global]
[echo] section = global
[echo] name =
[echo] value =
[echo] new last section: global
[echo] ----
echoMsg:
[echo] line = servername = my-server.local ; Server name
[echo] section = ${secm}
[echo] name = servername
[echo] value = mac-mini-van-Peter.local7
[echo] new last section: ${prevSection}
[echo] ----
正如你所看到的,最後一個「$ {prevSection }「未設置。我希望它是「全球」。
我試圖用的,而不是財產,但沒有差別。
嘗試使用[''](http://ant.apache.org/manual/Tasks/echoproperties.html)任務,你在讀INI後文件。這將列出所有屬性,並且您可以看到實際上在Ant中設置了哪些屬性。這可能會幫助您查看錯誤的位置。我從來沒有使用Ant中INI文件,所以我不知道它是如何在。 –