Asmor
Asmor

Reputation: 5181

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

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

Upvotes: 0

Views: 573

Answers (1)

ewan.chalmers
ewan.chalmers

Reputation: 16235

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

Upvotes: 2

Related Questions