根据不同文件的内容在 Apache Ant 中命名文件 [英] Naming a file in Apache Ant based on contents of different file

查看:21
本文介绍了根据不同文件的内容在 Apache Ant 中命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 foo.js,以及一个生成 foo.min.js 的 ant 构建过程.

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

foo.js 有一个标题注释,包括:

foo.js has a header comment that includes:

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

其中 12345 是提交到 SVN 时文件的修订版.

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

我想把 foo.min.js 复制到 foo.min.12345.js

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

推荐答案

您可以使用 loadfile 和 regex 将修订号提取到属性中.然后您可以使用该属性复制文件.

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>

输出:

$ 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

这篇关于根据不同文件的内容在 Apache Ant 中命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆