Naming a file in Apache Ant based on contents of different file

混江龙づ霸主 提交于 2019-12-25 09:44:50

问题


I've got foo.js, and an ant build process that results in foo.min.js.

foo.js has a header comment that includes:

* $Id: foo.js 12345 2011-10-04 14:35:23Z itoltz $

Where 12345 is the revision of the file when committed to SVN.

I'd like to copy foo.min.js to foo.min.12345.js


回答1:


You can extract the revision number into a property using loadfile and regex. Then you can copy the file using the property.

<project default="rename">

  <target name="rename" depends="get-rev">
    <copy file="foo.min.js" toFile="foo.min.${revision.number}.js"/>
  </target>

  <target name="get-rev">
    <loadfile srcFile="foo.js" property="revision.number">
      <filterchain>
        <linecontainsregexp>
          <regexp pattern="\* \$Id: foo.js"/>
        </linecontainsregexp>
        <tokenfilter>
          <replaceregex pattern="\* \$Id: foo.js (\d+).*" replace="\1"/>
        </tokenfilter>
        <striplinebreaks/>
      </filterchain>
    </loadfile>
    <echo message="revision.number: ${revision.number}"/>
  </target>

</project>

Output:

$ ls
build.xml  foo.js  foo.min.js
$
$ ant
Buildfile: C:\tmp\build.xml

get-rev:
     [echo] revision.number: 12345

rename:
     [copy] Copying 1 file to C:\tmp

BUILD SUCCESSFUL
Total time: 0 seconds
$
$ ls
build.xml  foo.js  foo.min.12345.js  foo.min.js


来源:https://stackoverflow.com/questions/7688240/naming-a-file-in-apache-ant-based-on-contents-of-different-file

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