Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir in ant fails. How can i handle this error

Tags:

java

ant

mkdirs

The ANT build script I have does the following:

  1. Perform the builds on Windows server & Zip the binaries
  2. Map a network drive with different credentials to a local drive (ex P:) using net use
  3. I am using <mkdir> to create a directory on the mounted drive (P:)
  4. Copy the binaries to that 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>

Some time the creation of folder works and some time it fails with below error

creation was not successful for an unknown reason and makes the build fail

This error happens randomly. The Mkdir works some time. I am not sure why it fails and not sure if its because of network lag

also the directory i am trying to create may or may not exist already. I read that the mkdir does not do anything if directory exists already

I checked and there is no failonerror for mkdir. I don't want the build to fail because of this.

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

How can I achieve this? Any help would be appreciated

Regards

Karthik

like image 342
KK99 Avatar asked Feb 15 '12 09:02

KK99


People also ask

How to check Ant build version?

Check your installation by opening a command line and typing ant -version into the commend line. The system should find the command ant and show the version number of your installed Ant version.

What is Ant command?

It is the most complete Java build and deployment tool available. It is platform neutral and can handle platform specific properties, such as file separators. It can be used to perform platform specific tasks such as modifying the modified time of a file using 'touch' command. Ant scripts are written using plain XML.

What is the purpose of Apache Ant?

Apache Ant is a software tool for automating software build processes which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform.


2 Answers

Apache Ant Mkdir task is calling File.mkdirs() method which is vulnerable to race conditions.

File.mkdirs() is not an atomic operation - I guess it is implemented as a sequence of mkdir calls.

In case of a remote filsystem there's a good chance that your host gets aware of filesystem changes in the middle of File.mkdirs() operation and it fails.

Ant seemed to try to fix it as Mkdir code changed from this in 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());
}

so maybe upgrading to the latest Ant could help?

If not - some brute force Mkdir task extension could be created with your own execute() method implementation.

If not - Trycatch task from Ant Contrib will work.

like image 194
Oleg Mikheev Avatar answered Nov 15 '22 05:11

Oleg Mikheev


For me, I had a similar issue with the 1.9 version of ant.

I was deleting a directory and immediately re-creating it:

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

Even though the directory was local (not a network drive), adding a sleep of 1 second between both operations fixed the issue for me:

<delete dir="${jar.dir}"/>
<sleep seconds="2"/>
<mkdir dir="${jar.dir}"/>
like image 36
Flo Avatar answered Nov 15 '22 05:11

Flo