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

前端 未结 5 1927
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 15:41

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?

相关标签:
5条回答
  • 2021-01-06 16:19

    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>
    
    0 讨论(0)
  • 2021-01-06 16:26

    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 讨论(0)
  • 2021-01-06 16:29

    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>
    
    0 讨论(0)
  • 2021-01-06 16:36

    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.

    0 讨论(0)
  • 2021-01-06 16:39

    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

    0 讨论(0)
提交回复
热议问题