When using ANT to write to the file system, specify a file

This post is more than 2 years old.

This is my weekly contribution to the "Stupid Developer of the Week" award. I spent the last hour or so trying to diagnose why my ANT script couldn't write to my file system. Apparently there is some Windows 7 bug where folders are marked Read Only and you can't clear it. So despite my ANT script's ability to make a folder I kept getting "Access is denied" errors when it tried to write into the folder. Here is the code I used.

<?xml version="1.0"?> <project name="test" default="getstuff" basedir=".">

&lt;property name="url" value="http://www.raymondcamden.com" /&gt;
&lt;property name="temp.dir" value="c:/temphttpdump/" /&gt;
&lt;target name="getstuff"&gt;
	&lt;echo message="About to get ${url} and copy to ${temp.dir}" /&gt;
	&lt;mkdir dir="${temp.dir}" /&gt;
	&lt;attrib file="${temp.dir}" readonly="false" system="false" verbose="on" /&gt;
	&lt;get src="${url}" dest="${temp.dir}" httpusecaches="false" verbose="on" /&gt;


Spot the issue yet? My get task is being told to save the result of my http get operation to a folder. But I didn't tell it the file to use! And because of this, the task assumed I wanted a file named temphttpdump since people often name files without extensions. To fix it I simply added a file name:

<get src="${url}" dest="${temp.dir}/result.html" httpusecaches="false" verbose="on" />

In my own defense, "Access is denied" as a message has a much different meaning to most folks. Why couldn't it have said "File exists and I can't overwrite" instead? I would have recognized it was trying to save it as a file and saved an hour of my life.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com