I need to increment a number in a source file from an Ant build script. I can use the ReplaceRegExp
task to find the number I want to increment, but how do I then increment that number within the replace
attribute?
Heres what I've got so far:
<replaceregexp file="${basedir}/src/path/to/MyFile.java"
match="MY_PROPERTY = ([0-9]{1,});"
replace="MY_PROPERTY = \1;"/>
In the replace attribute, how would I do
replace="MY_PROPERTY = (\1 + 1);"
I can't use the buildnumber
task to store the value in a file since I'm already using that within the same build target. Is there another ant task that will allow me to increment a property?
You can use something like:
<propertyfile file="${version-file}">
<entry key="revision" type="string" operation="=" value="${revision}" />
<entry key="build" type="int" operation="+" value="1" />
so the ant task is propertyfile.
In ant, you've always got the fallback "script" tag for little cases like this that don't quite fit into the mold. Here's a quick (messy) implementation of the above:
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With