将replaceregexp替换为属性值,具体取决于匹配的组

时间:2013-05-22 11:54:25

标签: ant replace

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

这可能吗?或者是否有一种更简单的方法可以解决我失踪的另一项任务?

1 个答案:

答案 0 :(得分: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>
相关问题