我想用属性文件中的值替换css文件中的一些替换标记。到目前为止,我所做的是:
<target depends="prepare" name="build_css">
<replaceregexp>
<fileset refid="temp_css_files"/>
<regexp pattern="{(.*)}"/>
<substitution expression="${testprop}"/>
</replaceregexp>
</target>
,它将成功地用testprop属性的值替换匹配的字符串。但是我想做的是,用一个名称为匹配字符串的属性来替换匹配的字符串。
因此,替换标记{myprop}
将被属性myprop
的值所替换。
我试着:
<target depends="prepare" name="build_css">
<replaceregexp>
<fileset refid="temp_css_files"/>
<regexp pattern="{(.*)}"/>
<substitution expression="${1}"/>
</replaceregexp>
</target>
没有成功,因为匹配的字符串随后被字符串${myprop}
替换。
这是可能的吗?或者有更简单的方法来完成我错过的另一项任务?
如果您可以使用典型的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>