有没有办法概括 Apache ANT 目标?

发布于 2024-07-04 12:24:17 字数 267 浏览 8 评论 0原文

我们有一个 Apache ANT 脚本来构建我们的应用程序,然后将生成的 JAR 文件签入版本控制(在本例中为 VSS)。 然而,现在我们进行了一项更改,要求我们为此项目构建 2 个 JAR 文件,然后将这两个文件签入 VSS。

将原始 JAR 文件签入 VSS 的当前目标通过某些属性发现 JAR 文件的名称。 有没有一种简单的方法来“概括”这个目标,以便我可以重用它来签入具有任何名称的 JAR 文件? 在普通语言中,这显然需要一个函数参数,但据我所知,ANT 中确实没有等效的概念。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

来世叙缘 2024-07-11 12:24:17

通常认为对二进制文件进行版本控制是一个坏主意,我不建议这样做。 但如果你绝对需要,你可以使用 antcall 结合 param 来传递参数并调用目标。

<antcall target="reusable">
    <param name="some.variable" value="var1"/>
</antcall>

<target name="reusable">
    <!-- Do something with ${some.variable} -->
</target>

您可以在此处找到有关 antcall 任务的更多信息。

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 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.

狼性发作 2024-07-11 12:24:17

看一下 Ant 。 它们允许您为 Ant 构建定义可重用的“例程”。 您可以在此处找到示例(第 15 项)。

Take a look at Ant macros. They allow you to define reusable "routines" for Ant builds. You can find an example here (item 15).

以可爱出名 2024-07-11 12:24:17

另请查看 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>

Also check out the subant task, which lets you call the same target on multiple build files:

<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>
禾厶谷欠 2024-07-11 12:24:17

您可以使用 Gant 通过 groovy 做你想做的事情或者看看 常规蚂蚁任务

You can use Gant to script your build with groovy to do what you want or have a look at the groovy ant task.

明天过后 2024-07-11 12:24:17

我建议使用 而不是 subant/antcall 因为主要优点是宏的一个优点是您可以完全控制传递给宏的属性(特别是如果您想添加新属性)。

您只需从目标开始重构 Ant 脚本:

<target name="vss.check">
    <vssadd localpath="D:\build\build.00012.zip" 
        comment="Added by automatic build"/>
</target>

创建宏(注意复制/粘贴并用 @{file} 替换):

<macrodef name="private-vssadd">
    <attribute name="file"/>
    <sequential>
        <vssadd localpath="@{file}" 
            comment="Added by automatic build"/>
    </sequential>
</macrodef>

并使用文件调用宏:

<target name="vss.check">
    <private-vssadd file="D:\build\File1.zip"/>
    <private-vssadd file="D:\build\File2.zip"/>
</target>

重构,“Ant 方式”

I would suggest to work with macros over 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).

You simply refactor your Ant script starting with your target:

<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}):

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文