在文件中根据正则表达式设置Ant属性



在文件

中有以下内容
version: [0,1,0]

,我想设置一个Ant属性为字符串值0.1.0

正则表达式为

version:[[:space:]][([[:digit:]]),([[:digit:]]),([[:digit:]])]

然后我需要将属性设置为

1.2.3

,

0.1.0

我不能练习如何使用Ant任务来完成这个任务。

我有Ant-contrib,所以可以使用这些任务

基于matt的第二个解决方案,这对我来说适用于任何(文本)文件,无论一行与否。它没有apache-contrib依赖项。

<loadfile property="version" srcfile="version.txt">
  <filterchain>
    <linecontainsregexp>
      <regexp pattern="version:[ t][([0-9]),([0-9]),([0-9])]"/>
    </linecontainsregexp>
    <replaceregex pattern="version:[ t][([0-9]),([0-9]),([0-9])]" replace="1.2.3" />
  </filterchain>
</loadfile>  

用这个解决:

<loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/>
<propertyregex property="burning-boots-js-lib-build.lib-version"
    override="true"
    input="${burning-boots-js-lib-build.lib-version}"
    regexp="version:[ t][([0-9]),([0-9]),([0-9])]"
    select="1.2.3" />

但是看起来有点浪费——它将整个文件加载到一个属性中!

如果有人有更好的建议,请发帖:)

这里有一种不使用反贡献的方法,使用loadpropertiesfilterchain(注意replaceregex是一个"字符串过滤器"-参见tokenfilter文档-而不是replaceregexp任务):

<loadproperties srcFile="version.txt">
  <filterchain>
    <replaceregex pattern="[([0-9]),([0-9]),([0-9])]" replace="1.2.3" />
  </filterchain>
</loadproperties>

注意,正则表达式有点不同,我们将文件视为属性文件。

或者您可以使用loadfilefilterchain,例如,如果您想要加载的文件不是属性格式的。

例如,如果文件内容只是[0,1,0],而您想将version属性设置为0.1.0,您可以这样做:
<loadfile srcFile="version.txt" property="version">
  <filterchain>
    <replaceregex pattern="s+[([0-9]),([0-9]),([0-9])]" replace="1.2.3" />
  </filterchain>
</loadfile>

最新更新