Java Ant 无法删除 Windows 上的某些文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/77436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:01:03  来源:igfitidea点击:

Ant is not able to delete some files on windows

javawindowsantbuild-automation

提问by Honza

I have an ant build that makes directories, calls javac and all the regular stuff. The issue I am having is that when I try to do a clean (delete all the stuff that was generated) the delete task reports that is was unable to delete some files. When I try to delete them manually it works just fine. The files are apparently not open by any other process but ant still does not manage to delete them. What can I do?

我有一个 ant 构建,可以创建目录、调用 javac 和所有常规内容。我遇到的问题是,当我尝试清理(删除所有生成的内容)时,删除任务报告无法删除某些文件。当我尝试手动删除它们时,它工作得很好。这些文件显然没有被任何其他进程打开,但 ant 仍然无法删除它们。我能做什么?

采纳答案by Shimi Bandiel

I encountered this problem once. It was because the file i tried to delete was a part of a classpathfor another task.

我遇到过一次这个问题。这是因为我尝试删除的文件是另一个任务的类路径的一部分。

回答by BQ.

You don't say if your build is run as the currently logged on user. If not, the fact that explorer.exe or other process has the directory shown can cause it to be locked as well. But deleting it in that same explorer.exe process would succeed. Try Unlocker from http://ccollomb.free.fr/unlocker/to see what processes have the files/directories locked.

您没有说明您的构建是否以当前登录的用户身份运行。如果没有,explorer.exe 或其他进程具有显示的目录这一​​事实也可能导致它被锁定。但是在同一个 explorer.exe 进程中删除它会成功。从http://ccollomb.free.fr/unlocker/尝试 Unlocker以查看哪些进程锁定了文件/目录。

回答by John Meagher

Is there something from the Ant process that is holding the files (or directory) open? This would cause the situation where you could delete them after running ant, but not during.

是否有来自 Ant 进程的东西使文件(或目录)保持打开状态?这将导致您可以在运行 ant 后删除它们,但不能在运行期间删除它们。

回答by Craig Trader

It depends ...

这取决于 ...

  • The Ant process doesn't have enough permissions to delete the files (typically because they were created by a different user, perhaps a system user). Try running your Ant script as an administrative user, using Run As.
  • Windows is really bad at cleaning up file locks when processes die or are killed; consequently, Windows thinks the file is locked by a process that died (or was killed). There's nothing you can do in this situation other than reboot.
  • Get better tools to inspect your system state. I recommend downloading the SysInternalstools and using them instead of the default Windows equivalents.
  • Ant 进程没有足够的权限来删除文件(通常是因为它们是由不同的用户创建的,可能是系统用户)。尝试以管理用户身份运行您的 Ant 脚本,使用运行身份。
  • 当进程死亡或被杀死时,Windows 在清理文件锁方面非常糟糕;因此,Windows 认为该文件已被死亡(或被杀死)的进程锁定。在这种情况下,除了重新启动之外,您无能为力。
  • 获得更好的工具来检查您的系统状态。我建议下载SysInternals工具并使用它们而不是默认的 Windows 等效工具。

回答by Sergey

Ant versions before 1.8.0 have a bug which leads to random errors during delete operation. Try using Ant 1.8.0 or newer.

1.8.0 之前的 Ant 版本有一个 bug,它会导致删除操作时出现随机错误。尝试使用 Ant 1.8.0 或更新版本。

You can see the bug details here https://issues.apache.org/bugzilla/show_bug.cgi?id=45960

您可以在此处查看错误详细信息https://issues.apache.org/bugzilla/show_bug.cgi?id=45960

回答by Artem Nakonechny

Using Ant Retry task has helped me. I've just wrapped it around the Delete Task.

使用 Ant Retry 任务对我有帮助。我刚刚将它包裹在删除任务中。

回答by Sarel Botha

I've been having this problem a lot lately and it's random. One time it works, the next time it doesn't work. I'm using NetBeans (in case that matters) and I've added a lot of extra tasks to build.xml. I was having this problem in the -post-jar task. It would happen when I call unjar on the file, then delete. I suspect that NB is trying to scan the jar and this causes the lock on it.

我最近经常遇到这个问题,而且是随机的。一次有效,下一次无效。我正在使用 NetBeans(以防万一)并且我向 build.xml 添加了很多额外的任务。我在 -post-jar 任务中遇到了这个问题。当我在文件上调用 unjar 然后删除时会发生这种情况。我怀疑 NB 正在尝试扫描罐子,这会导致锁定它。

What worked for me is to immediately rename the jar at the start of -post-jar and add a .tmp extension to it. Then I call unjar on the temp file. When I'm done I rename back to the desired jar name.

对我有用的是在 -post-jar 开头立即重命名 jar 并为其添加 .tmp 扩展名。然后我在临时文件上调用 unjar 。完成后,我将重命名回所需的 jar 名称。

回答by Sudheer Palyam

I too had the same problem and was tried of manually deleting the build directories. Finally I solved it by renaming the .jar artifact of my project to a different name from project name itself. For ex: my project was portal and my ant built script use to generate portal.jar, where eclipse ant was not able to delete this portal.jar. When i changed my build.xml to generate my .jar as portalnew.jar, eclipse was able to delete this portalnew.jar next time. Hope this helps.

我也有同样的问题,并尝试手动删除构建目录。最后,我通过将项目的 .jar 工件重命名为与项目名称本身不同的名称来解决它。例如:我的项目是portal,我的ant构建脚本用来生成portal.jar,其中eclipse ant无法删除这个portal.jar。当我更改我的 build.xml 以将我的 .jar 生成为 portalnew.jar 时,eclipse 能够在下次删除这个 portalnew.jar。希望这可以帮助。

回答by Vinila

I faced the same problem.
I didn't have any classpath set to or antivirus running on my machine.
However, the ANT version I was using was 32 bit and the JDK I installed was 64 bit.
I installed a 32 bit JDK and the issue was resolved.

我遇到了同样的问题。
我的机器上没有设置任何类路径或防病毒软件。
但是,我使用的 ANT 版本是 32 位,我安装的 JDK 是 64 位。
我安装了 32 位 JDK,问题解决了。

回答by MikeRoger

In my case my ant clean was failing from Eclipse, unable to remove build files. I see this from time to time. Usually succeeds on a repeat attempt. This time no. Tried running ant clean from command line, failed Unable to delete"unable to delete". It must have been Eclipse holding on to the problem file, when I exited Eclipse, cmd line was able to delete OK.

在我的情况下,我的 ant clean 从 Eclipse 失败,无法删除构建文件。我不时看到这个。通常在重复尝试时会成功。这次没有。尝试从命令行运行 ant clean,失败无法删除“无法删除”。肯定是 Eclipse 保留了问题文件,当我退出 Eclipse 时,cmd 行可以删除 OK。