This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes Ant + Split string and assign to a property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Ant + Split string and assign to a property" Watch "Ant + Split string and assign to a property" New topic

Ant + Split string and assign to a property

Easwara Murthy

Joined: Feb 12, 2009
Posts: 6

I have a collection of strings separated by semicolon say, txt1;txt2;txt3;txt4;txt5 (which is available in a property)

I have to split this group of strings based on semicolon. While reading each string (say, first i get the string txt1), i need to assign the string to a variable (and then invoke a target). How can this be achieved using ant.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

The ant-contrib library has a for loop that you can use to call a target with the split values. It assigns each one to a temporary variable when calling the new target.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Donald Skidmore

Joined: Aug 27, 2002
Posts: 2
<target Name="main">
<property semicolon="txt1;txt2;txt3;txt4;txt"/>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
def str=properties.semicolonstring;
def eachstr= str.split(";");
properties.str1 = eachstr[0];
properties.str2 = eachstr[1];
<antcall target="results"/>
<target name="results">

I agree. Here's the link:
subject: Ant + Split string and assign to a property
It's not a secret anymore!