0

我需要一些关于 Ant 的帮助。我想使用 Ant 创建一个文件,该文件包含一些名称和某些文件夹的大小(以 kb 为单位),还包含其他类型的数据(不是大小)但具有相同的模式。像这样的东西:

build.date=April 01, 2000
folder1=10000
folder2=59093
folder3=646854
folder4=14897123

并且还要计算一些文件夹大小的总和(例如foldersum=folder1+folder2)并将其写入文件中:

build.date=April 01, 2000
folder1=10000
folder2=59093
folder3=646854
folder4=14897123
foldersum=folder1+folder2
4

1 回答 1

1

ANT 不是一种编程语言。最好的方法是嵌入脚本语言,例如groovy

例子

给定以下目录结构

├── build.xml
├── folder1
│   ├── file1.txt
│   ├── file2.txt
│   └── file3.txt
├── folder2
│   ├── file4.txt
│   └── file5.txt
└── folder3
    └── file6.txt

生成一个名为“build-report.txt”的文件

build.date=April 09,2015
folder1=1
folder2=1
folder3=1
foldersum=3

构建.xml

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

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

  <target name="build" depends="init">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

    <dirset id="dirs" dir="." includes="folder*"/>

    <groovy>
      import java.text.SimpleDateFormat

      def sdf = new SimpleDateFormat("MMMM dd,yyyy")

      new File("build-report.txt").withWriter { w ->

        w.println "build.date=${sdf.format(new Date())}"

        def total = 0

        ant.project.references.dirs.each {
          def dir = new File(it.toString())
          def size = dir.directorySize()

          w.println "${dir.name}=${size}"

          total += size
        }

        w.println "foldersum=${total}"
      }
    </groovy>
  </target>

  <target name="init" 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.4.3/groovy-all-2.4.3.jar"/>
    <fail message="Groovy installed run build again"/>
  </target>

</project>
于 2015-04-06T20:47:45.983 回答