2012-10-26 2 views
0

필자는 개미 스크립트에서 사용할 수있는 속성으로 ini 파일을 구문 분석하려고합니다. 나는 다음과 같습니다개미 : 설정 속성이 작동하지 않습니까?

<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> 

내가 모든 이름 = 값 쌍을 구문 분석과 같은 속성을 넣어 수행하려고 : section.name = 값;

어떻게 든 섹션은 "echoMsg"타겟 내에 기억되지 않습니다. 섹션 이름을 기억하고 싶습니다.

그래서,

[global] 
name=var 
name2=val 

[section2] 
name=var 

이 될해야 : 마지막으로 "$ {prevSection

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] ---- 

당신이 볼 수 있듯이 :

global.name=var 
global.name2=val 
section2.name=var 

이 내 개미 스크립트의 출력 } "설정되지 않았습니다. 나는 그것이 "글로벌"이 될 것으로 기대합니다.

속성 대신 사용하려고했지만 아무런 차이가 없습니다.

+0

에서 [''] (http://ant.apache.org/manual/Tasks/echoproperties.html) 작업을 사용해보십시오 : 그냥 WWW과 공유하고 싶었다 파일. 그러면 모든 속성이 나열되고 실제로 Ant에서 속성으로 설정되는 내용을 볼 수 있습니다. 그러면 오류가 어디에 있는지 알 수 있습니다. 개미에서 INI 파일을 사용한 적은 한번도 없었습니다. 그래서 어떻게 읽는지 모르겠습니다. –

답변

1

힌트 : <propertyregex> 문 앞에 에코 섹션을 추가하여 다양한 속성의 값을 확인해보십시오.

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 

<echomsg>를 호출 할 때마다 이전에 <echomsg>에 설정된 값을 잃게 :

나는이 <echo> 라인 ...
<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="----" /> 

지금의 출력을 살펴 보겠습니다을 추가했다.

<for> 작업을 시도해 보시기 바랍니다. <for> 작업은 루프 반복마다 속성 값을 잃지 않습니다. 그러나 <propertyregex> 작업에 override 설정을 추가하고 <property> 작업 대신 <var> 작업을 사용하여 루프의 각 반복을 통해 속성 값을 다시 설정해야합니다.

당신은 또한 당신의 <taskdef>이로 변경해야합니다 :

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

데이비드에게 감사드립니다. 그것은 완벽하게 작동합니다! – user1351312

1

이것은 나를 위해 작동 코드입니다. 당신은 INI에서 읽은 후

<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>