0

我想用属性文件中的值替换 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>

没有成功,因为匹配的字符串随后被替换为 String ${myprop}

有可能做到这一点吗?或者有没有更简单的方法来处理我错过的另一项任务?

4

1 回答 1

3

如果您可以使用典型的 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>
于 2013-05-22T15:43:41.083 回答