Search code examples
xmlantant-contrib

Ant XML Customized Folder Name


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?


Solution

  • 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.