如果您可以使用典型的 Ant${...}
语法来表示 CSS 文件中的属性,那么 Ant 的<expandproperties>
FilterReader可能会很有用:
<project name="ant-replace-tokens-with-copy-task" default="run">
<target name="run">
<property name="src-root" location="src"/>
<fileset id="temp_css_files" dir="${src-root}">
<include name="**/*.css"/>
</fileset>
<!-- The <copy> task cannot "self-copy" files. So, for each -->
<!-- matched file we'll have <copy> read the file, replace the -->
<!-- tokens, and write the result to a temporary file. Then, we'll -->
<!-- use the <move> task to replace the original files with the -->
<!-- modified files. -->
<property name="filtered-file.extension" value="*.filtered-file"/>
<copy todir="${src-root}">
<fileset refid="temp_css_files"/>
<globmapper from="*" to="${filtered-file.extension}"/>
<filterchain>
<expandproperties/>
</filterchain>
</copy>
<move todir="${src-root}">
<fileset dir="${src-root}" includes="**"/>
<globmapper from="${filtered-file.extension}" to="*"/>
</move>
</target>
</project>
如果您需要坚持使用{...}
语法,ReplaceTokens FilterReader 会将标记替换为属性文件中定义的属性:
<project name="ant-replace-tokens-with-copy-task" default="run">
<target name="run">
<property name="src-root" location="src"/>
<fileset id="temp_css_files" dir="${src-root}">
<include name="**/*.css"/>
</fileset>
<!-- The <copy> task cannot "self-copy" files. So, for each -->
<!-- matched file we'll have <copy> read the file, replace the -->
<!-- tokens, and write the result to a temporary file. Then, we'll -->
<!-- use the <move> task to replace the original files with the -->
<!-- modified files. -->
<property name="filtered-file.extension" value="*.filtered-file"/>
<copy todir="${src-root}">
<fileset refid="temp_css_files"/>
<globmapper from="*" to="${filtered-file.extension}"/>
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="tokenchar" name="begintoken" value="{"/>
<param type="tokenchar" name="endtoken" value="}"/>
<param type="propertiesfile" value="dev.properties"/>
</filterreader>
</filterchain>
</copy>
<move todir="${src-root}">
<fileset dir="${src-root}" includes="**"/>
<globmapper from="${filtered-file.extension}" to="*"/>
</move>
</target>
</project>