Removing comment in ini file writen by ant when updating the ini file

南楼画角 提交于 2020-01-05 08:12:12

问题


I have an ant file which updates the data in ant file due this ini file gets updated and at the top it has a comment as follows

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

I don't want this comment as i am accessing this file by php using parse_ini. Due to this comment i get an failure

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

so is there any way so that i will not get the comment in ini file.

Thanks.

EDIT:

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

this updates my ini file build-number by +1


回答1:


Martin's comment points you to a way to remove comments using replaceregexp. (I was about to show you a similar idea but using move, filterchain and striplinecomments. But the replaceregexp is more compact.)

The other suggestion I have is that since you are editing ini files, maybe you should use a task dedicated to that, rather than using the PropertyFile task. There is an IniFile taks in ant-contrib might do the job.

If the replaceregexp doesn't work for you because your file has other # comments in it and you only want to remove that top line, then try this:

<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


来源:https://stackoverflow.com/questions/6610536/removing-comment-in-ini-file-writen-by-ant-when-updating-the-ini-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!