是否可以替换Ant的build.xml中属性中的文本?

问题描述 投票:3回答:5

我有一个属性app.version,它设置为1.2.0(当然,总是在变化),需要创建名称为“ something-ver-1_2_0”的zip文件。这可能吗?

java ant
5个回答
7
投票

您可以使用pathconvert任务替换“。”与“ _”并分配给新属性:

<?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
投票

另一种方法是使用正则表达式从文件到属性的版本号filter,如本示例中所示:

<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
投票

由于属性app.version总是在更改我假定,因此您不想将其硬编码到属性文件中,而希望在构建时将其传递给它。除了此answer,您还可以在命令行上尝试以下操作;

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

然后将app.version更改为所需的那个。

编辑:

从反馈中更好地了解您的问题。Unfortunatelyant没有字符串处理任务,您需要为此编写自己的task。这是一个关闭的example


0
投票

可以使用zip任务

<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>

有关压缩任务的更多信息:http://ant.apache.org/manual/Tasks/zip.html


0
投票

属性无法更改,但antContrib变量(http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html可以更改。

这里是一个宏,用于在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>

然后按如下所示调用宏:

<ac:var name="newFileName" value="${app.version}"/>
<current:replaceVarText varName="newFileName" from="." to="_" />
<echo>Filename will be ${newFileName}
© www.soinside.com 2019 - 2024. All rights reserved.