专门针对文件的 loadresource 的特化,仅适用于文件,并提供 srcFile 属性以方便使用。除非指定了 encoding,否则将使用当前区域设置的编码。
如果资源内容为空(可能是在处理 filterchain 之后),则不会设置该属性。
| 属性 | 描述 | 必需 |
|---|---|---|
| srcFile | 源文件 | 是 |
| property | 要保存到的属性 | 是 |
| encoding | 加载文件时使用的编码 | 否 |
| failonerror | 是否在失败时停止构建 | 否;默认值 true |
| quiet | 不显示诊断消息(除非 Apache Ant 使用 -verbose 或 -debug 开关调用)或修改退出状态以反映错误。将其设置为 true意味着将 failonerror 设置为 false。自 Ant 1.7.0 起。 |
否;默认值 false |
LoadFile 任务支持嵌套的 FilterChain。
将文件 message.txt 加载到属性 message 中;<echo> 可以打印它。
<loadfile property="message"
srcFile="message.txt"/>
以上与以下相同
<loadresource property="message">
<file file="message.txt"/>
</loadresource>
使用 Latin-1 编码加载文件
<loadfile property="encoded-file"
srcFile="loadfile.xml"
encoding="ISO-8859-1"/>
加载文件,如果文件丢失则不失败(但会打印一条消息)
<loadfile property="optional.value"
srcFile="optional.txt"
failonerror="false"/>
加载一个属性,该属性可以用作另一个任务(在本例中为 mail)的参数,合并行以确保发生这种情况。
<loadfile property="mail.recipients"
srcFile="recipientlist.txt">
<filterchain>
<striplinebreaks/>
</filterchain>
</loadfile>
将 XML 文件加载到属性中,在此过程中扩展文件中声明的所有属性。
<loadfile property="system.configuration.xml"
srcFile="configuration.xml">
<filterchain>
<expandproperties/>
</filterchain>
</loadfile>