使用 Ant 在文件中写入文件夹名称及其大小
Write folder name and their sizes in a file using Ant
我需要 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
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
build.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>
我需要 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
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
build.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>