如何在 Ant 目标中使用文件大小 [英] How to use size of file inside Ant target
问题描述
我目前正在用 Ant 构建脚本替换我的自制构建脚本.
I'm currently in the process of replacing my homebrewn build script by an Ant build script.
现在我需要用特定文件的大小替换各种标记.我知道如何通过 <length>
任务获取以字节为单位的大小并将其存储在属性中,但我也需要以千字节和兆字节为单位的大小.
Now I need to replace various tokens by the size of a specific file. I know how to get the size in bytes via the <length>
task and store in in a property, but I need the size in kilobytes and megabytes too.
如何访问其他表示形式(KB、MB)的文件大小或从 Ant 目标中计算这些值并将它们存储在属性中?
How can I access the file size in other representations (KB, MB) or compute these values from within the Ant target and store them in a property?
在我发现 任务后,使用一些 JavaScript 计算其他值并使用
project.setNewProperty("foo", "bar");
.
After I discovered the <script>
task, it was fairly easy to calculate the other values using some JavaScript and add a new property to the project using project.setNewProperty("foo", "bar");
.
推荐答案
我使用 任务,允许使用 JavaScript(或任何其他 Apache BSF 或 JSR 223 支持语言)来自 Ant 目标.
I found a solution that does not require any third-party library or custom tasks using the <script>
task that allows for using JavaScript (or any other Apache BSF or JSR 223 supported language) from within an Ant target.
<target name="insert-filesize">
<length file="${afile}" property="fs.length.bytes" />
<script language="javascript">
<![CDATA[
var length_bytes = project.getProperty("fs.length.bytes");
var length_kbytes = Math.round((length_bytes / 1024) * Math.pow(10,2))
/ Math.pow(10,2);
var length_mbytes = Math.round((length_kbytes / 1024) * Math.pow(10,2))
/ Math.pow(10,2);
project.setNewProperty("fs.length.kb", length_kbytes);
project.setNewProperty("fs.length.mb", length_mbytes);
]]>
</script>
<copy todir="${target.dir}">
<fileset dir="${source.dir}">
<include name="**/*" />
<exclude name="**/*.zip" />
</fileset>
<filterset begintoken="$$$$" endtoken="$$$$">
<filter token="SIZEBYTES" value="${fs.length.bytes}"/>
<filter token="SIZEKILOBYTES" value="${fs.length.kb}"/>
<filter token="SIZEMEGABYTES" value="${fs.length.mb}"/>
</filterset>
</copy>
</target>
这篇关于如何在 Ant 目标中使用文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!