Ant: Trimming Properties via Inline Javascript

February 8, 2012

I have had to trim spaces as well as perform other logic (such as parsing the sections of a x.y.z build number) on properties within Ant. Unfortunately, this is not built into Ant. The Ant-contrib library has several methods to doing this, but I had the restriction of not using a third party library. The only other example I found was writing to a temporary file and then reading the property file back in. That just seems like overkill for such a simple task. I actually found an easier, more powerful way by using inline javascript. Ant natively supports the various JVM-based scriping languages, including Javascript.

You can basically do any type of string manipulation within the javascript (including splits, trim, concat, replace, substring, etc) and then set a new property on the project. Remember that properties are immutable in Ant, so you have to set a different property name. Note that this is based on Ant 1.7+.

Leave a Reply