2011-01-05 55 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

我想出了這一個。需要有一個額外的屬性文件中創建,在下面的格式每個鍵/值:@ 等 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

標記您自己的答案是正確的,因爲它完美的作品。 – 2012-04-23 15:15:04

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

似乎是我的最佳答案,因爲它應該使用常規屬性文件,而不需要@令牌@ – Rhubarb 2016-02-22 10:28:35

+0

但是,這似乎爲各種屬性的值添加了逃逸令牌。例如:我有aa = D:\ abcd。這被轉換爲aa = D \:\\ abcd。有什麼辦法可以避免這種情況?發現螞蟻concat任務效果更好。 – vanval 2016-05-20 14:16:21

0

其他的答案是好的,但我需要一個沒有這些限制:

  • 需要所有的屬性被指定爲以@標記@(第一個答案)
  • 物業擴展模板 - 例如我有屬性定義爲prop2 = $ {prop1}這將擴大任何解決方案加載和回聲屬性
  • EchoProperties(@ user2500146)轉義字符像冒號的URL屬性煩人(不是Ant的錯誤,這是標準的Java屬性,允許:在地方=)的基於CONCAT的解決方案
  • 重複的屬性(這工作,因爲第二個定義被忽略,但我不想重複

最後,我不得不求助於在過濾器的JavaScript,但我的解決方案帶來了默認屬性,當且僅當它們沒有在主要屬性文件中定義。 它通過加載主屬性wi然後將其複製到目標,然後連接默認屬性,而則過濾掉在第一步中加載的任何默認屬性。

您可以使用此逐字但可能要拿出日誌報表或將其更改爲調試級別,一旦你確信

<!-- 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 2016-02-23 10:02:16

相關問題