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

蓝咒 提交于 2019-12-19 04:14:30

问题


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 "something-ver-1_2_0". Is this possible?


回答1:


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>



回答2:


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>



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/1978866/is-it-possible-to-replace-text-in-properties-in-ants-build-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!