java 删除ant中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2206299/
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-10-29 20:02:30  来源:igfitidea点击:

Deleting a file in ant

javaant

提问by MHarris

For ease of access, I have a couple of config files in the parent of a series of project folders. When building the projects, they need copying into one of the projects source folders until after the build is finished, whereupon I'd like them to be deleted. At the moment, I have this:

为了便于访问,我在一系列项目文件夹的父级中有几个配置文件。构建项目时,他们需要复制到项目源文件夹之一,直到构建完成,然后我希望将它们删除。目前,我有这个:

<target name="build-java">
  <copy file="config.properties" todir="project/src" />
  <!-- Build other projects -->
  <delete file="project/src/config.properties" />
</target>

Which does the job if the projects build. Alas for my pride, they don't always. Ideally, I'd like the equivalent of the following Java:

如果项目建立,哪个工作。唉,我的骄傲,他们并不总是如此。理想情况下,我想要相当于以下 Java:

File src = new File("config.properties");
File dst = FileUtils.copyFile(src, "project/src");
dst.deleteOnExit();
// Carry on with the rest of the build, content in the knowledge that whatever happens, the file will die.

But neither the Copynor the Deleteant tasks seem up to the job. This doesn't seem like a particularly obscure need?

但是CopyDeleteant 任务似乎都不能胜任这项工作。这似乎不是一个特别隐晦的需求?

采纳答案by Koobz

You could try ant-contrib's trycatchto run the delete task even if the build fails.

即使构建失败,您也可以尝试使用ant-contrib 的 trycatch来运行删除任务。

There's other goodies in ant-contrib like the foreach task. If you're able to install it.

ant-contrib 中还有其他好东西,比如 foreach 任务。如果你能安装它。

回答by Thilo

How about defining a "clean" task that deletes the leftover files, and you can call that after each build (even after failed builds)?

如何定义一个删除剩余文件的“干净”任务,并且您可以在每次构建之后调用它(即使在构建失败之后)?

I think Ant is not very good with these kind of conditional flow-control things. There may be a way to make that work, but that could get messy,

我认为 Ant 不太擅长这些条件流控制的东西。可能有一种方法可以使它起作用,但这可能会变得混乱,