java 有没有办法概括 Apache ANT 目标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49900/
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
Is there a way to generalize an Apache ANT target?
提问by Outlaw Programmer
We have an Apache ANT script to build our application, then check in the resulting JAR file into version control (VSS in this case). However, now we have a change that requires us to build 2 JAR files for this project, then check both into VSS.
我们有一个 Apache ANT 脚本来构建我们的应用程序,然后将生成的 JAR 文件签入版本控制(在本例中为 VSS)。但是,现在我们有一个更改,需要我们为此项目构建 2 个 JAR 文件,然后将它们都签入 VSS。
The current target that checks the original JAR file into VSS discovers the name of the JAR file through some property. Is there an easy way to "generalize" this target so that I can reuse it to check in a JAR file with any name? In a normal language this would obviously call for a function parameter but, to my knowledge, there really isn't an equivalent concept in ANT.
将原始 JAR 文件检入 VSS 的当前目标通过某些属性发现 JAR 文件的名称。有没有一种简单的方法来“概括”这个目标,以便我可以重用它来签入任何名称的 JAR 文件?在普通语言中,这显然需要一个函数参数,但据我所知,ANT 中确实没有等效的概念。
回答by Vladimir
I would suggest to work with macrosover subant/antcall because the main advantage I found with macros is that you're in complete control over the properties that are passed to the macro (especially if you want to add new properties).
我建议在 subant/antcall 上使用宏,因为我发现使用宏的主要优点是您可以完全控制传递给宏的属性(特别是如果您想添加新属性时)。
You simply refactor your Ant script starting with your target:
您只需从目标开始重构 Ant 脚本:
<target name="vss.check">
<vssadd localpath="D:\build\build.00012.zip"
comment="Added by automatic build"/>
</target>
creating a macro (notice the copy/paste and replacement with the @{file}):
创建宏(注意复制/粘贴和替换为@{file}):
<macrodef name="private-vssadd">
<attribute name="file"/>
<sequential>
<vssadd localpath="@{file}"
comment="Added by automatic build"/>
</sequential>
</macrodef>
and invoke the macros with your files:
并使用您的文件调用宏:
<target name="vss.check">
<private-vssadd file="D:\build\File1.zip"/>
<private-vssadd file="D:\build\File2.zip"/>
</target>
Refactoring, "the Ant way"
重构,“蚂蚁方式”
回答by Chris Dail
It is generally considered a bad idea to version control your binaries and I do not recommend doing so. But if you absolutely have to, you can use antcall combined with param to pass parameters and call a target.
通常认为对二进制文件进行版本控制是一个坏主意,我不建议这样做。但是如果你绝对必须,你可以使用 antcall 结合 param 来传递参数并调用目标。
<antcall target="reusable">
<param name="some.variable" value="var1"/>
</antcall>
<target name="reusable">
<!-- Do something with ${some.variable} -->
</target>
You can find more information about the antcall task here.
您可以在此处找到有关antcall 任务的更多信息。
回答by Dan Dyer
回答by jodonnell
Also check out the subant task, which lets you call the same target on multiple build files:
还可以查看 subant 任务,它允许您在多个构建文件上调用相同的目标:
<project name="subant" default="subant1">
<property name="build.dir" value="subant.build"/>
<target name="subant1">
<subant target="">
<property name="build.dir" value="subant1.build"/>
<property name="not.overloaded" value="not.overloaded"/>
<fileset dir="." includes="*/build.xml"/>
</subant>
</target>
</project>
回答by Peter Kelley
You can use Gantto script your build with groovyto do what you want or have a look at the groovy ant task.
您可以使用Gant用groovy编写您的构建脚本以执行您想要的操作或查看groovy ant 任务。

