Ant provides task ‘Tempfile’ to create temporary file, unlike java.io.File.createTempFile
, this task does not actually create a real temporary file in disk system(unless you ask it to), it just sets a property to the name of a temporary file which you can later create a file or dir.
Whats the parameter of Ant task ‘Tempfile’ ?
Attribute | Description | Type | Requirement |
property | Sets the property you wish to assign the temporary file to. | String | Required |
destdir | Sets the destination directory. If not set, the basedir directory is used instead. | File | Optional |
prefix | Sets the optional prefix string for the temp file. | String | |
suffix | Sets the optional suffix string for the temp file. | String | |
deleteonexit | Whether the temp file will be marked for deletion on normal exit of the Java Virtual Machine (even though the file may never be created); default false. Since Apache Ant 1.7 | boolean | |
createfile | Whether the temp file should be created by this task; default false.Since Ant 1.8 | boolean |
How I do – Examples ?
<tempfile property="temporary.file"/>
Use Ant task ‘tempfile’ to create a temporary file.
<tempfile property="temporary.file" suffix=".xml" createfile="true"/>
Create a temporary file with the .xml
suffix, the optional attribute ‘createfile’ is set in this example, unlike the first case that only sets a property to the name, it does create a real file in disk system.
<tempfile property="temporary.file" destDir="build"/>
Create a temporary file in the build subdirectory.
The whole ANT Build example XML.
<project default="createTemp"> <target name="createTemp"> <echo>${java.io.tmpdir}</echo> <tempfile property="temp.file" destDir="${java.io.tmpdir}" prefix="build"/> <echo>${temp.file}</echo> </target> </project>
How to Run this Ant?
1. Create a Java project, open the Ant Build view window of IDE, click ‘add’ button to add the Ant build XML to it, run the target ‘createTemp’.
2. The output per my testing:
createTemp
echo
C:\Users\Jammy\AppData\Local\Temp\
tempfile
echo
C:\Users\Jammy\AppData\Local\Temp\build1098061924Ant build completed successfully in 5s at 23:54:16