繁体   English   中英

更新ini文件时删除ant写的ini文件中的注释

[英]Removing comment in ini file writen by ant when updating the ini file

我有一个 ant 文件,它更新了 ant 文件中的数据,因为这个 ini 文件得到了更新,并且在顶部它有如下评论

#Thu, 07 Jul 2011 06:54:54 -0500

我不想要这个评论,因为我正在使用 parse_ini 通过 php 访问这个文件。 由于此评论,我失败了

Comments starting with '#' are deprecated in build.ini on line 1

那么有什么办法让我不会在ini文件中得到评论。

谢谢。

编辑:

<propertyfile file="build.ini">
  <entry key="build-number" type="int" operation="+" value="1" />
</propertyfile>

这会将我的 ini 文件内部版本号更新 +1

Martin 的评论为您指出了一种使用 replaceregexp 删除评论的方法。 (我正要向您展示一个类似的想法,但使用 move、filterchain 和 striplinecomments。但是 replaceregexp 更紧凑。)

我的另一个建议是,由于您正在编辑 ini 文件,也许您应该使用专门的任务,而不是使用PropertyFile任务。 ant-contrib 中有一个IniFile任务可以完成这项工作。

如果 replaceregexp 对您不起作用,因为您的文件中有其他 # 注释并且您只想删除该顶行,请尝试以下操作:

<target name="test">
  <propertyfile file="test.properties">
    <entry key="key" value="value"/>
  </propertyfile>
  <move file="test.properties" tofile="test.properties.tmp">
    <filterchain>
      <headfilter lines="-1" skip="1"/>
    </filterchain>
  </move>
  <move file="test.properties.tmp" tofile="test.properties"/>
</target>

Output:

$ cat test.properties
one=1
# existing comment

$ ant
Buildfile: C:\tmp\ant\build.xml

test:
[propertyfile] Updating property file: C:\tmp\ant\test.properties
     [move] Moving 1 file to C:\tmp\ant
     [move] Moving 1 file to C:\tmp\ant

BUILD SUCCESSFUL

Total time: 0 seconds

$ cat test.properties
one=1
# existing comment

key=value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM