当前位置: 首页 > 知识库问答 >
问题:

ANT:设置属性不起作用?

桂玉石
2023-03-14

我尝试将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。名称=值;

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

我试着用它来代替财产,但没有区别。

共有2个答案

汪坚
2023-03-14

这是适合我的代码。只是想与WWW分享:

<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>
卫烨烁
2023-03-14

这里有一个提示:尝试在<代码>

我添加了这些

<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

每次调用<代码>

我建议您试试<代码>

您还必须更改您的<代码>

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

 类似资料:
  • 问题内容: 我的实体中有一个布尔型属性。这是我的注释: 但是效果并不理想。这是我作为生成表的结果得到的SQL代码: 我究竟做错了什么? 因此,当我尝试将此类的实例保存到数据库时,出现异常: 如果我删除财产: 因此在这种情况下,我可以保存创建的对象。但是仍然 没有设置默认值, 并且我在数据库中此字段的值中得到NULL。 有什么想法吗?如果重要,我会使用MySQL Server 5.1。我将非常感谢您

  • 问题内容: 我正在尝试使用struts2框架设置隐藏的输入值。输入值是一个不变的值,因此我想通过静态引用进行设置,而不用担心可能在Controller中的多个位置进行设置。 这是列表定义: 我意识到这可能超出了问题的范围,但是当我尝试在隐藏的输入中引用该属性时,它将引发异常: org.apache.jasper.JasperException:/WEB- INF/content/user/prof

  • 我在一个android应用程序中工作,正在使用一个DialogFragment来显示一个对话框,我想让那个DialogFragment不可取消。我已经将对话框可取消属性设置为false,但它仍然不受影响。 请查看我的代码,并建议我一个解决方案。

  • el-date-picker defaultTime 动态赋值不生效

  • 问题内容: 自最新版本以来,我经历了Chrome的某些意外行为:在Firefox中,此代码可以正常运行: 它在Chrome中不起作用(只需下载一个名为“下载”的文件),但是以前效果很好。我必须更改一下它又能正常工作吗? 问题答案: 经过研究,我终于找到了您的问题。 下载属性: 如果存在HTTP标头Content-Disposition:,并且提供的文件名与此属性不同,则HTTP标头的优先级高于此属

  • 问题内容: 我需要在CSS中使用属性选择器来更改不同颜色和图像上的链接,但是它不起作用。 我有这个HTML: 而这个CSS: 为什么背景不是红色的? 问题答案: 在href后面使用$。这将使属性值匹配字符串的结尾。