2011-01-05 6 views
7

배포 환경에 따라 기본 등록 정보 파일과 기본값의 특정 설정을 대체하는 특정 배치 특정 등록 정보 파일이 있습니다. 내 Ant 빌드 스크립트가 두 속성 파일을 병합 (기본 값을 배포 특정 값으로 덮어 씀)하고 결과 속성을 새 파일로 출력하고 싶습니다. Ant를 사용하여 두 개의 다른 등록 정보 파일 병합

나는과 같이 그 일을 시도하지만 실패했습니다 :

<target depends="init" name="configure-target-environment"> 
    <filterset id="application-properties-filterset"> 
     <filtersfile file="${build.config.path}/${target.environment}/application.properties" /> 
    </filterset> 

    <copy todir="${web-inf.path}/conf" file="${build.config.path}/application.properties" overwrite="true" failonerror="true" > 
     <filterset refid="application-properties-filterset" /> 
    </copy> 
</target> 

답변

2

은 아마 당신이 개미의 concat 작업에 보일 것입니다.

2

나는 이것을 알아 낸 것입니다. mail.server.host = @ mail.server.host @ 등 ...

다음이 "템플릿"파일을 지정하면 추가 키 속성 값이 생성되어야합니다. 작업의 "파일"속성에 추가하십시오. 또한 필터 세트에서 가장 중요도가 가장 낮은 것을 먼저 나열한 다음 여러 항목을 지정하십시오.

그래서 그 결과는 다음과 같습니다

<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" > 
    <filterset refid="application-properties-filterset" /> 
</copy> 

+0

마크는 자신의 대답은 올바른 것으로 확신하고있어 일단 디버그 수준으로 변경하는 것이 좋습니다. –

0

개인적으로 사용이 :

<copy todir="${web-inf.path}/conf" filtering="true"> 
    <fileset dir="${build.config.path}" includes="*.properties" /> 
    <filterset> 
    <filtersfile file="application-properties-filterset" /> 
    </filterset> 
</copy> 
3

, 나는 다음과 같이했다 :

<property prefix="app.properties" file="custom.application.properties" /> 
<property prefix="app.properties" file="default.application.properties" /> 
<echoproperties destfile="application.properties"> 
    <propertyset> 
     <propertyref prefix="app.properties"/> 
     <mapper type="glob" from="app.properties.*" to="*"/> 
    </propertyset> 
</echoproperties> 
+0

은 @ tokens @ – Rhubarb

+0

을 요구하기보다는 일반 속성 파일을 사용해야하므로 나에게 가장 좋은 대답 인 것 같습니다. 그러나 이것은 다양한 속성의 값에 이스케이프 토큰을 추가하는 것으로 보입니다. 예 : 저는 aa = D : \ abcd입니다. 이것은 aa = D \ : \\ abcd로 변환됩니다. 이것을 피할 수있는 방법이 있습니까? 개미 연결 작업이 더 잘 작동하는 것을 발견했습니다. – vanval

0

다른 답변 괜찮아, 그러나 나는 이러한 제한없이 하나를 필요 :

  • 모든 속성 @ 토큰 @ (첫 번째 대답)
  • 프로퍼티의 전개와 템플릿으로 지정해야 할 필요 - 예. prop2 = $ {prop1}로 정의 된 속성이 있습니다. 속성을로드하고 에코하는 모든 솔루션에 의해 확장됩니다.
  • EchoProperties (@ user2500146)는 URL 속성 (Ant의 오류가 아님)의 콜론과 같은 문자를 이스케이프 처리합니다. 허용되는 = 대신) CONCAT 기반 솔루션에서
  • 반복 속성 (2 정의가 무시되기 때문에이 작품,하지만 난 리조트했다 결국 반복을

원하지 않았다에 필터 자바 스크립트,하지만 내 솔루션 기본 속성을 가져올 경우에만 및 기본 속성 파일에서 정의되지 않은 경우에만. 그것은 주요 속성을로드하여 작동합니다. wi 첫 번째 단계에서로드 된 기본 속성을 필터링하면서 기본 속성을 concat'ing 대상 모달 접미사, 다음, 복사합니다.

은이 그대로 사용할 수 있지만, 완벽하게 작동하기 때문에 아마도, 로그 문을 꺼내거나

<!-- merge the main.properties.file with the default.properties.file 
    into the output.properties.file (make sure these are defined) --> 
<target name="merge"> 
    <!--Obscure enough prefix to ensure the right props are handled--> 
    <property name="prefix" value="__MY_PREFIX__"/> 
    <!--Load the main properties so we can tell if the default is needed--> 
    <property prefix="${prefix}" file="${main.properties.file}"/> 

    <!--Copy the main properties, then append the defaults selectively--> 
    <copy file="${main.properties.file}" tofile="${output.properties.file}" overwrite="true"/> 
    <concat destfile="${output.properties.file}" append="true"> 
     <fileset file="${default.properties.file}"/> 
     <filterchain> 
      <!--Filter out lines with properties that were already in the main properties --> 
      <scriptfilter language="javascript"> <![CDATA[ 
      var line = self.getToken(); 
      project.log("line: " + line); 
      var skipLine = false; 
      // lines that do not define properties are concatenated 
      if (line.indexOf("=") != -1) { 
       // get the property name from the line 
       var propName = line.substr(0, line.indexOf('=')); 
       project.log("line prop: " + propName); 
       var loadedPropName = "__MY_PREFIX__" + propName; 
       if (project.getProperty(loadedPropName) != null) { 
        project.log("prop has original: " + project.getProperty(loadedPropName)); 
        // skip this line, the property is defined 
        skipLine = true; 
       } 
      } 

      if (skipLine) { 
       project.log("skipping line: " + line); 
       self.setToken(null); 
      } 
      else { 
       // else leave the line in as it was 
       project.log("adding default line: " + line); 
       self.setToken(line); 
      } 

]]> </scriptfilter> 
     </filterchain> 
    </concat> 
</target> 
+0

좀 더 정확하려면 PREFIX 속성을 읽으면 동일성을 확인할 수 있지만 추가 비용이 들지 않습니다. – Rhubarb

관련 문제