Adam
Adam

Reputation: 36723

How to create temporary directory in ant?

I'd like to create a temporary directory in ant (version 1.6.5) and assign it to a property.

Background: I'm trying to speed up an existing build process which builds inside networked filesystem. The build already copies all the source to a temporary directory, however this is on the same filesystem. I've tested changing this to /tmp/foo and it gives a worthwhile speed increase: 3mins vs 4mins.

Upvotes: 16

Views: 10748

Answers (2)

Zweibieren
Zweibieren

Reputation: 412

The answer above only hints at how to create a temporary directory. The point is that merely returns a string. A more complete answer is

<target name="temptest" description="test making tempdir">
    <tempfile property="mytempdir" destdir="${java.io.tmpdir}"/>
    <tempfile property="mytempfile" destdir="${mytempdir}"/>
    <tstamp>
        <format property="now" pattern="MMMM dd yyyy"/>
    </tstamp>
    <copy tofile="${mytempfile}">
        <string value="today=${now}"/>
    </copy>
    <property file="${mytempfile}"/> 
    <echo message="It it now ${today}"/>
</target>              

Upvotes: -2

ewan.chalmers
ewan.chalmers

Reputation: 16245

You could combine the tempfile task with the java.io.tmpdir system property to get a file path to use to create a temporary dir:

<project default="test">

    <target name="test">
        <echo>${java.io.tmpdir}</echo>
        <tempfile property="temp.file" destDir="${java.io.tmpdir}" prefix="build"/>
        <echo>${temp.file}</echo>
    </target>

</project>

Note that the tempfile task does not create the file (unless you ask it to). It just sets a property which you can use to create a file or dir.

This task sets a property to the name of a temporary file. Unlike java.io.File.createTempFile, this task does not actually create the temporary file, but it does guarantee that the file did not exist when the task was executed.

Output in my environment:

test:
     [echo] C:\Users\sudocode\AppData\Local\Temp\
     [echo] C:\Users\sudocode\AppData\Local\Temp\build1749402932

Upvotes: 25

Related Questions