Ant:如何对目录中的每个文件执行命令?
我想从 Ant 构建文件中为目录中的每个文件执行命令。
我正在寻找一个独立于平台的解决方案。
我该怎么做?
当然,我可以用某种脚本语言编写脚本, 但这会增加对项目的进一步依赖。
I want to execute a command from an Ant buildfile, for each file in a directory.
I am looking for a platform-independent solution.
How do I do this?
Sure, I could write a script in some scripting language,
but this would add further dependencies to the project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用任务 。
它为每个文件执行一次命令。通过文件集或任何其他资源指定文件。 <申请>是内置的;不需要额外的依赖;无需实现自定义任务。
也可以仅运行该命令一次,一次性将所有文件作为参数附加。使用并行属性来切换行为。
抱歉迟到了一年。
Use the <apply> task.
It executes a command once for each file. Specify the files by means of filesets or any other resource. <apply> is built-in; no additional dependency needed; no custom task implementation needed.
It's also possible to run the command only once, appending all files as arguments in one go. Use the parallel attribute to switch the behaviour.
Sorry for being late a year.
简短回答
使用
和嵌套Foreach 需要 ant-contrib。
最近 ant-contrib 的更新示例:
这将 ant 调用目标“bar”,并使用 ${theFile} 生成当前文件。
Short Answer
Use
<foreach>
with a nested<FileSet>
Foreach requires ant-contrib.
Updated Example for recent ant-contrib:
This will antcall the target "bar" with the ${theFile} resulting in the current file.
Tassilo Horn(原始目标在这里)
基本上,因为没有 (还?)以同样的方式
扩展 < a href="http://ant.apache.org/manual/Tasks/exec.html" rel="noreferrer"> ,他建议使用 < apply> (当然也可以在命令行中运行java程序)
这里有一些例子:
An approach without ant-contrib is suggested by Tassilo Horn (the original target is here)
Basicly, as there is no extension of <java> (yet?) in the same way
that <apply> extends <exec>, he suggests to use <apply> (which can of course also run a java programm in a command line)
Here some examples:
这是使用 javascript 和 ant scriptdef 任务执行此操作的方法,您不需要 ant-contrib 即可使此代码工作,因为 scriptdef 是核心 ant 任务。
Here is way to do this using javascript and the ant scriptdef task, you don't need ant-contrib for this code to work since scriptdef is a core ant task.
ant-contrib 是邪恶的;编写自定义 Ant 任务。
ant-contrib 是邪恶的,因为它试图将 ant 从声明式风格转换为命令式风格。但 xml 是一种糟糕的编程语言。
相比之下,自定义 ant 任务允许您使用真实的 IDE 来使用真实的语言 (Java) 进行编写,您可以在其中编写单元测试以确保您具有所需的行为,然后在构建脚本中做出明确的声明:你想要的行为。
仅当您关心编写可维护的 Ant 脚本时,此咆哮才有意义。如果您不关心可维护性,请尽一切努力。 :)
Jtf
ant-contrib is evil; write a custom ant task.
ant-contrib is evil because it tries to convert ant from a declarative style to an imperative style. But xml makes a crap programming language.
By contrast a custom ant task allows you to write in a real language (Java), with a real IDE, where you can write unit tests to make sure you have the behavior you want, and then make a clean declaration in your build script about the behavior you want.
This rant only matters if you care about writing maintainable ant scripts. If you don't care about maintainability by all means do whatever works. :)
Jtf
我知道这篇文章确实很旧,但现在一段时间和 ant 版本已经过去了,有一种方法可以使用基本的 ant 功能来做到这一点,我想我应该分享它。
它是通过调用嵌套任务的递归宏定义来完成的(甚至可以调用其他宏)。唯一的约定是使用固定的变量名称(此处为元素)。
所需的关键功能:
我没有设法制作分隔符变量,但这可能不是一个主要缺点。
I know this post is realy old but now that some time and ant versions passed there is a way to do this with basic ant features and i thought i should share it.
It's done via a recursive macrodef that calls nested tasks (even other macros may be called). The only convention is to use a fixed variable name (element here).
The key features needed where:
I didnt manage to make the delimiter variabel, but this may not be a major downside.
您可以使用 ant-contrib 任务“for”来迭代由任何分隔符分隔的文件列表,默认分隔符为“,”。
以下是显示这一点的示例文件:
You can use the ant-contrib task "for" to iterate on the list of files separate by any delimeter, default delimeter is ",".
Following is the sample file which shows this:
执行 blak3r 建议的操作并定义您的目标类路径,其中
lib 是您存储 jar 的位置
Do what blak3r suggested and define your targets classpath like so
where lib is where you store your jar's