开发者

Is it possible to replace text in properties in Ant's build.xml?

I have a property, app.version, which is set to 1.2.0 (and, of course, always changing) and need to create zip file with name "so开发者_如何学编程mething-ver-1_2_0". Is this possible?


You can use the pathconvert task to replace "." with "_" and assign to a new property:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <property name="app.version" value="1.2.0"/>

    <pathconvert property="app.version.underscore" dirsep="" pathsep="" description="Replace '.' with '_' and assign value to new property">
        <path path="${app.version}" description="Original app version with dot notation" />

        <!--Pathconvert will try to add the root directory to the "path", so replace with empty string -->
        <map from="${basedir}" to="" />

        <filtermapper>
            <replacestring from="." to="_"/>     
        </filtermapper>

    </pathconvert>

    <echo>${app.version} converted to ${app.version.underscore}</echo>
</project>


Another approach is to filter the version number from a file to a property using a regular expression, as suggested in this example:

<loadfile srcfile="${main.path}/Main.java" property="version">
    <filterchain>
        <linecontainsregexp>
            <regexp pattern='^.*String VERSION = ".*";.*$'/>
        </linecontainsregexp>
        <tokenfilter>
            <replaceregex pattern='^.*String VERSION = "(.*)";.*$' replace='\1'/>
        </tokenfilter>
        <striplinebreaks/>
    </filterchain>
</loadfile>


Since the property app.version is always changing i assume you don't want to hard code it into the properties files, rather pass it when you do the build. Further to this answer, you can try the following on the command line;

ant -f build.xml -Dapp.version=1.2.0

changing app.version to the one required then.

Edit:

Understood better your question from the feedback. Unfortunately ant does not have string manipulation tasks, you need to write you own task for this. Here is a close example.


It's possible using the zip task

<zip zipfile="something-ver-${app.version}.zip">
<fileset basedir="${bin.dir}" prefix="bin">
    <include name="**/*" />
</fileset>
<fileset basedir="${doc.dir}" prefix="doc">
    <include name="**/*" />
</fileset></zip>

For more information about the zip task: http://ant.apache.org/manual/Tasks/zip.html


Properties can't be changed but antContrib vars (http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html ) can.

Here is a macro to do a find/replace all on a var:

    <macrodef name="replaceVarText">
        <attribute name="varName" />
        <attribute name="from" />
        <attribute name="to" />
        <sequential>
            <local name="replacedText"/>
            <local name="textToReplace"/>
            <local name="fromProp"/>
            <local name="toProp"/>
            <property name="textToReplace" value = "${@{varName}}"/>
            <property name="fromProp" value = "@{from}"/>
            <property name="toProp" value = "@{to}"/>

            <script language="javascript">
                project.setProperty("replacedText",project.getProperty("textToReplace").split(project.getProperty("fromProp")).join(project.getProperty("toProp")));
            </script>
            <ac:var name="@{varName}" value = "${replacedText}"/>
        </sequential>
    </macrodef>

Then call the macro like:

<ac:var name="newFileName" value="${app.version}"/>
<current:replaceVarText varName="newFileName" from="." to="_" />
<echo>Filename will be ${newFileName}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜