Mkdir in ant does not work. How can I handle this error

ANT build script I have the following:

  • Build on a Windows server and write the binaries
  • Map a network drive with different credentials on a local drive (ex P :) with net use
  • I use <mkdir> to create a directory on a mounted drive (P :)
  • Copy binary files to this drive

Below is my code for mkdir

 <echo>Creating ${buildRequesterUserId} folder at mirroring site starts</echo> <mkdir dir="P:\build_output\${buildRequesterUserId}"/> <echo>Creating ${buildRequesterUserId} folder at mirroring site ends</echo> 

For some time, creating a folder works, and for a while it fails with the error below

creation was not successful for an unknown reason and leads to a build failure

This error occurs randomly. Mkdir has been working for a while. I'm not sure why it fails and not sure that due to lack of network

the directory that I am trying to create may or may not exist. I read that mkdir does nothing if the directory already exists

I checked and there is no failonerror for mkdir. Because of this, I do not want the assembly to fail.

I handled the error in the copy part, but not sure how to handle this mkdir

How can I achieve this? Any help would be appreciated

Hi

Kartik

+7
source share
5 answers

The Apache Ant Mkdir calls the File.mkdirs() method, which is vulnerable to race conditions .

File.mkdirs() not an atomic operation - I assume that it is implemented as a sequence of calls to Mkdir .

In the case of a remote file system, there is a good chance that your host will find out about file system changes in the middle of the File.mkdirs() operation, and it will not work.

Ant seemed to try to fix it as Mkdir code changed from this to 1.8.0

 boolean result = mkdirs(dir); if (!result) { String msg = "Directory " + dir.getAbsolutePath() + " creation was not successful for an unknown reason"; throw new BuildException(msg, getLocation()); } 

to this in 1.8.2

 boolean result = mkdirs(dir); if (!result) { if (dir.exists()) { log("A different process or task has already created " + "dir " + dir.getAbsolutePath(), Project.MSG_VERBOSE); return; } String msg = "Directory " + dir.getAbsolutePath() + " creation was not successful for an unknown reason"; throw new BuildException(msg, getLocation()); } 

Is it possible to upgrade to the latest version of Ant?

If not, some brute force extension Mkdir can be created using a native implementation of the execute() method.

If not, Ant Contrib's Trycatch task will work.

+2
source

For me, I had a similar problem with version 1.9 ant.

I deleted the directory and immediately recreated it:

 <delete dir="${jar.dir}"/> <mkdir dir="${jar.dir}"/> 

Despite the fact that the directory was local (and not a network drive), adding 1 second to sleep between both operations, the problem for me was fixed:

 <delete dir="${jar.dir}"/> <sleep seconds="2"/> <mkdir dir="${jar.dir}"/> 
+1
source

You can use the COPY task to create directories (included subdirectories).

0
source

For me, the problem with Linux on Linux was as simple as for the user sudo

"sudo ant"

0
source

Here is how I solved it:

  • Open the build.properties file (actually open all the properties files) refers to build.xml)
  • Check for any trailing spaces and tabs on any line in the file.
  • Add an additional empty line to the end of the file.
-4
source

All Articles