如何在Ant" ReplaceRegExp"任务中执行数学函数?

时间:2020-03-06 14:43:06  来源:igfitidea点击:

我需要从Ant构建脚本中增加源文件中的数字。我可以使用ReplaceRegExp任务来查找我要增加的数字,但是如何在replace属性中增加该数字呢?

这是到目前为止我得到的:

<replaceregexp file="${basedir}/src/path/to/MyFile.java"
    match="MY_PROPERTY = ([0-9]{1,});"
    replace="MY_PROPERTY = ;"/>

在replace属性中,我该怎么办

replace="MY_PROPERTY = ( + 1);"

我不能使用buildnumber任务将值存储在文件中,因为我已经在同一构建目标中使用了它。还有另一个允许我增加属性的蚂蚁任务吗?

解决方案

我们可以使用类似:

&lt;propertyfile file =" $ {version-file}"> &lt;&lt; entry key =" revision" type =" string" operation =" =" value =" $ {revision}" /> &lt;entry key =" build" type =" int" operation =" +" value =" 1" />

因此,ant任务是propertyfile。

很好的问题,可以在perl中完成类似的工作,但是我认为在ant,.NET和其他领域是不可能的。 '在Perl中使用过很多次,而在我们提到的情况下,我确实可以使用过。

在ant中,对于像这样的不太适合模具的小情况,我们总会得到后备的" script"标签。这是上述内容的快速(混乱)实现:

<property name="propertiesFile" location="test-file.txt"/>

    <script language="javascript">
        regex = /.*MY_PROPERTY = (\d+).*/;

        t = java.io.File.createTempFile('test-file', 'txt');
        w = new java.io.PrintWriter(t);
        f = new java.io.File(propertiesFile);
        r = new java.io.BufferedReader(new java.io.FileReader(f));
        line = r.readLine();
        while (line != null) {
            m = regex.exec(line);
            if (m) {
                val = parseInt(m[1]) + 1;
                line = 'MY_PROPERTY = ' + val;
            }
            w.println(line);
            line = r.readLine();
        }
        r.close();
        w.close();

        f.delete();
        t.renameTo(f);
    </script>