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