Thursday, April 12, 2007

Good temporary files in Ant

I was reading this nice post on unsigning jars with Ant when this line caught my eye:

<tempfile destdir="${java.io.tmpdir}"
          prefix="unsign-"
          property="temp.file"/>

I was struck by an Aha! moment—using ${java.io.tmpdir} is clearly the Right Thing for creating temporary files in Java. Why not also in Ant?

2 comments:

Brian Duff said...

Well... it would be nice if your "clean" target (or whatever) also deleted any temporary files that were created via your build, and that's something that's more easily done if you use a location that you can just mass-delete on clean (which java.io.tmpdir is not).

Brian Oxley said...

Agreed.

What I would likely do is actually make a directory underneath java.io.tmpdir and use that.

Of course, in shell I have mktemp available for that task (or in C/C++ as well) which handles race condition issues, etc.