【问题标题】:ANT: Setting property does not work?ANT:设置属性不起作用?
【发布时间】:2012-10-16 13:36:58
【问题描述】:

我尝试将一个 ini 文件解析为可以在我的 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=value;

不知何故,“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}”没有设置。我希望它是“全球性的”。

我尝试使用而不是属性,但没有区别。

【问题讨论】:

  • 在读入 INI 文件后尝试使用&lt;echoproperties&gt; 任务。这将列出所有属性,并且您可以看到在 Ant 中实际设置为属性的内容。这可能会帮助您了解错误在哪里。我从来没有在 Ant 中使用过 INI 文件,所以我不确定它是如何读入的。

标签: ant properties foreach var


【解决方案1】:

这里有一个提示:尝试在&lt;propertyregex&gt; 语句之前添加一个回显部分,以查看各种属性的值。

我添加了这些&lt;echo&gt; 行...

<target name="echoMsg">
    <!-- strip out the section name, variable name and value (if exists on the line) -->
    <echo message="prev line    = ${line}" />
    <echo message="prev section = ${secm}" />
    <echo message="prev name    = ${name}" />
    <echo message="prev value   = ${value}" />
    <echo message="prev new last section: ${prevSection}" />
    <echo message="----" />

现在让我们看看输出:

deploy-staging:
     [echo] Ini file: /Users/david/property.ini
     [echo] ${lib}
     [echo] ${store_dir}
     [echo] /usr/share/ant

echoMsg:
     [echo] prev line    = [global]
     [echo] prev section = ${secm}
     [echo] prev name    = ${name}
     [echo] prev value   = ${value}
     [echo] prev new last section: 
     [echo] ----
     [echo] PREVSECTION IS SET
     [echo] line    = [global]
     [echo] section = global
     [echo] name    = 
     [echo] value   = 
     [echo] new last section: 
     [echo] ----

echoMsg:
     [echo] prev line    = name=foo
     [echo] prev section = ${secm}
     [echo] prev name    = ${name}
     [echo] prev value   = ${value}
     [echo] prev new last section: 
     [echo] ----
     [echo] line    = name=foo
     [echo] section = ${secm}
     [echo] name    = name
     [echo] value   = foo
     [echo] new last section: 
     [echo] ----

echoMsg:
     [echo] prev line    = name2=bar
     [echo] prev section = ${secm}
     [echo] prev name    = ${name}
     [echo] prev value   = ${value}
     [echo] prev new last section: 
     [echo] ----
     [echo] line    = name2=bar
     [echo] section = ${secm}
     [echo] name    = name2
     [echo] value   = bar
     [echo] new last section: 
     [echo] ----

echoMsg:
     [echo] prev line    = [section2]
     [echo] prev section = ${secm}
     [echo] prev name    = ${name}
     [echo] prev value   = ${value}
     [echo] prev new last section: 
     [echo] ----
     [echo] PREVSECTION IS SET
     [echo] line    = [section2]
     [echo] section = section2
     [echo] name    = 
     [echo] value   = 
     [echo] new last section: 
     [echo] ----

echoMsg:
     [echo] prev line    = name=fubar
     [echo] prev section = ${secm}
     [echo] prev name    = ${name}
     [echo] prev value   = ${value}
     [echo] prev new last section: 
     [echo] ----
     [echo] line    = name=fubar
     [echo] section = ${secm}
     [echo] name    = name
     [echo] value   = fubar
     [echo] new last section: 
     [echo] ----
     [echo] HERE: 

BUILD SUCCESSFUL
Total time: 1 second

&lt;echomsg&gt; 的每次调用都会丢失之前在&lt;echomsg&gt; 中设置的值。

我建议你试试&lt;for&gt; 任务。 &lt;for&gt; 任务不会在循环的每次迭代中丢失您的属性值。但是,您必须将override 设置添加到您的&lt;propertyregex&gt; 任务中,并使用&lt;var&gt; 任务而不是&lt;property&gt; 任务才能通过循环的每次迭代来重置您的属性值。

您还必须将您的 &lt;taskdef&gt; 更改为:

<taskdef resource="net/sf/antcontrib/antlib.xml" />

【讨论】:

    【解决方案2】:

    这是对我有用的代码。只是想和万维网分享:

    <project name="DeployScript" default="deploy-staging" basedir=".">
        <taskdef resource="net/sf/antcontrib/antlib.xml" />
    
        <!-- The location of the settings.ini file -->
        <property name="ini-file" location="../settings.ini" />
    
        <!-- load the ini file -->
        <loadfile property="iniConfig" srcFile="${ini-file}"/>  
    
        <!-- when no section is given, the vars are stored in the "default" section -->
        <var name="theSection" value="default" />
    
        <!-- set global properties for this build -->
        <target name="deploy-staging" description="" >
    
            <echo message="Start parsing ini file" level="info" />
    
            <!--
                PARSE INI FILE
                This section parses the ini file and creates the parameters
                which we can use in this script. 
            -->
            <for list="${iniConfig}" param="line" delimiter="${line.separator}" trim="true">
                <sequential>
    
                    <propertyregex property="newSection" input="@{line}" regexp="^\[(.*)\]" select="\1" casesensitive="false" defaultvalue="" override="true" />
                    <propertyregex property="name" input="@{line}" regexp="^([^;][\S]+)\s*=\s*([^;]+)" select="\1" casesensitive="false" defaultValue="" override="true" /> 
                    <propertyregex property="value" input="@{line}" regexp="^([^;][\S]+)\s*=\s*([^;]+)" select="\2" casesensitive="false"  defaultValue="" override="true" />
    
                    <if>
                        <!-- if we recieved a new section -->
                        <isset property="newSection" />
                        <then>
                            <if>
                                <!-- section is not empty -->
                                <not><equals arg1="${newSection}" arg2="" /></not>
                                <then>
                                    <!-- store this as the new section --> 
                                    <var name="theSection" value="${newSection}" />
                                </then>
                            </if>                       
                        </then>
                    </if>
    
                    <!-- only create a new var if we have a var-name -->
                    <if>
                        <isset property="name" />
                        <then>
                            <if>
                                <!-- name is not empty -->
                                <not><equals arg1="${name}" arg2="" /></not>
                                <then>
                                    <!-- store this as the new section --> 
                                    <var name="${theSection}.${name}" value="${value}" /> 
                                    <echo message="${theSection}.${name} = ${value}" level="info" />
                                </then>
                            </if>                       
                        </then>
                    </if>
                </sequential>
            </for>
        </target>
    
    </project>
    

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2014-02-04
      相关资源
      最近更新 更多