mkdir in ant fails. How can i handle this error

最后都变了- 提交于 2019-12-05 05:26:00

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.

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}"/>

You can use the COPY Task for creating directories (inclusive subdirectories).

For me fixing the issue in linux was as simple as running as sudo user

"sudo ant"

Here's how I solved it:

  1. Open your build.properties file (Actually open all properties files' referenced from build.xml)
  2. Check for any trailing spaces and tabs on any line of the file.
  3. Add an additional blank line to the end of file.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!