0

I am new to Ant and XML and i need some help in a problem.

I want to create a root folder whose name would be something like

[number][timestamp][some_strings]_etc

I will show you my first piece of code, where i just read the values from the file.

<target name="create">

   <loadfile srcfile="new.txt" property="fisier" />
   <for param="line" list="${fisier}" delimiter="${line.separator}">
         <sequential>
            <echo>@{line}</echo>
            <propertyregex property="item"
              input="${line}"
              regexp="regexpToMatchSubstring"
              select="\1"
              casesensitive="false" />
         </sequential>
       </for>
   </target>

From the values i read, i need to subtract a string with a regexp. I have something like id=2344 and i need only the number, meaning the string from the right of the equal sign. How can i do that?

3
  • How is this a question related to ANT? Commented Aug 7, 2014 at 20:02
  • I just want to write the build.xml and run it in the command line. How could it not be ant related? Commented Aug 8, 2014 at 7:54
  • 1
    thank you for clarifying your question with an example. Commented Aug 8, 2014 at 18:03

1 Answer 1

1

This kind of requirement is a lot simpler to implement using a general programming language. Your example demonstrates how the ant-contrib library is required to provide the "for" ANT task.

Here's an alternative implementation using groovy:

<groovy>
new File("data.txt").eachLine { line ->
  def num = line =~ /.*=(\d+)/
  println num[0][1]
}
</groovy>

Example

├── build.xml
└── data.txt

Run as follows

build:
   [groovy] 2222
   [groovy] 2223
   [groovy] 2224

data.txt

id=2222
id=2223
id=2224

build.xml

<project name="demo" default="build">

  <available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/>

  <target name="install-groovy" unless="groovy.installed">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.3.6/groovy-all-2.3.6.jar"/>
    <fail message="Groovy installed run the build again"/>
  </target>

  <target name="build" depends="install-groovy">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
    <groovy>
    new File("data.txt").eachLine { line ->
      def num = line =~ /.*=(\d+)/
      println num[0][1]
    }
    </groovy>
  </target>

</project>

Notes:

  • Included an additional target that installs the jar required by the groovy task. Makes the build more portable.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.