有没有办法使用ant检测文件的属性。 例如:创建日期,修改日期,大小等......? 我无法找到任何可以让我这样做的内置物。 感谢
答案 0 :(得分:1)
正确,没有任何内置。
以下示例使用groovy ant task来调用Java NIO库:
<project name="demo" default="build">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<macrodef name="getMetadata">
<attribute name="file"/>
<sequential>
<groovy>
import java.nio.file.*
import java.nio.file.attribute.*
import java.text.*
def path = Paths.get("@{file}")
def attributes = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes()
def df = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss", Locale.US)
properties.'@{file}_size' = attributes.size()
properties.'@{file}_ctime' = df.format(new Date(attributes.creationTime().toMillis()))
properties.'@{file}_mtime' = df.format(new Date(attributes.lastModifiedTime().toMillis()))
properties.'@{file}_atime' = df.format(new Date(attributes.lastAccessTime().toMillis()))
</groovy>
</sequential>
</macrodef>
<target name="build">
<getMetadata file="src/foo/bar/A.txt"/>
<echo message="File : src/foo/bar/A.txt"/>
<echo message="Size : ${src/foo/bar/A.txt_size}"/>
<echo message="Create time : ${src/foo/bar/A.txt_ctime}"/>
<echo message="Modified time : ${src/foo/bar/A.txt_mtime}"/>
<echo message="Last access time: ${src/foo/bar/A.txt_atime}"/>
</target>
</project>
运行以下命令将groovy任务jar安装到ANT可以使用的位置:
mkdir -p ~/.ant/lib
curl http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.3.7/groovy-all-2.3.7.jar -L -o ~/.ant/lib/groovy-all.jar
此外,我使用的是ANT 1.9.4和Java 1.7.0_25