Ant XML自定义文件夹名称

时间:2014-08-06 17:29:17

标签: xml ant ant-contrib

我是Ant和XML的新手,我在问题上需要一些帮助。

我想创建一个名称类似于

的根文件夹

[数字] [时间戳] [some_strings] _etc

我将向您展示我的第一段代码,我只是从文件中读取值。

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

根据我读到的值,我需要用正则表达式减去一个字符串。我有像id = 2344的东西,我只需要数字,意思是等号右边的字符串。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用通用编程语言实现这种要求要简单得多。您的示例演示了如何使用ant-contrib库来提供“for”ANT任务。

以下是使用groovy的替代实现:

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

实施例

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

运行如下

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>

注意:

  • 包含一个额外的目标,用于安装groovy任务所需的jar。使构建更具可移植性。