如何在 Ant 中动态包含 ant-contrib.jar [英] How to include ant-contrib.jar dynamically in Ant

查看:48
本文介绍了如何在 Ant 中动态包含 ant-contrib.jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 Ant 文件中包含 .jar 的方法,以便我可以立即使用它并在我的目标中调用它的方法.就我而言,它是 ant-contrib-1.0b3.jar.

I'm looking for a way to include a .jar from within an Ant file so I can use it straight away and call its methods in my targets. In my case it's ant-contrib-1.0b3.jar.

推荐答案

最好的方法是将 Ant-Contrib jarfile 放在您的项目中.例如,假设 build.xml 位于项目的根目录中.在您的项目中创建一个名为 ant.lib\ant-contrib 的目录,然后将 ant-contrib*.jar 放在此文件夹中.您可以将此方法用于您可能需要的其他可选 Ant 任务(例如,Ivy、Findbugs、Cobrrtura 等).

The best way is to put the Ant-Contrib jarfile inside you project. For example, let's say the build.xml is in the root of your project. Create a directory called ant.lib\ant-contrib inside your project, then put the ant-contrib*.jar in this folder. You can use this method for other optional Ant tasks that you might need (for example, Ivy, Findbugs, Cobrrtura, etc).

然后,在您的 build.xml 文件中,您可以这样做:

Then, in your build.xml file, you can do this:

<taskdef resource="net/sf/antcontrib/antlib.xml">
     <classpath>
         <fileset dir="${basedir}/ant.lib/ant-contrib"/>
     </classpath>
</taskdef>

我喜欢这样做,因为项目中包含了带有任务的可选 jar.如果您将所有内容都检查到您的版本控制系统中,则有人可以检查您的代码,并且无需下载 Ant-Contrib 并自行安装即可进行构建.

I like doing it this way because the optional jars with the tasks are included with the project. If you check everything into your version control system, someone can checkout your code, and do the build without downloading Ant-Contrib and installing it themselves.

您可以定义一个 XML 命名空间.这为您的 Ant-Contrib 任务提供了一个前缀,以便在您使用具有相同任务名称的其他可选 ant 任务时避免任务名称冲突.另外,它会提醒用户这不是标准的 Ant 任务.

You can define an XML namespaces. This gives your Ant-Contrib tasks a prefix in order to avoid task name collisions in case you use other optional ant tasks that have the same task name. Plus, it alerts users that this is not a standard Ant task.

如果您使用 XML 命名空间,则需要在 标题中放置 XMLNS 声明.这将包含一个 URI,它将您的 Ant Contrib 任务连接到您的 XML 命名空间.例如,ac: 命名空间用于所有 Ant Contrib 任务:

If you use an XML namespace, you need to put a XMLNS declaration in your <project> heading. This will contain a URI that will connect your Ant Contrib tasks to your XML namespace. For example, the ac: namespace is for all Ant Contrib tasks:

<project name="my.project" default="package" basedir="."
    xmlns:ac="http://ant-contrib.sourceforge.net">

 <taskdef resource="net/sf/antcontrib/antlib.xml"
      uri="http://ant-contrib.sourceforge.net">
     <classpath>
         <fileset dir="${basedir}/ant.lib/ant-contrib"/>
     </classpath>
</taskdef>

这样做是将 ac 的 XML 命名空间 (xmlns) 与 URI http://ant-contrib.sourceforge.net 匹配.URI 可以是任何东西.例如:

What this does is match the XML namespace (xmlns) of ac with the URI http://ant-contrib.sourceforge.net. The URI could be anything. For example:

<project name="my.project" default="package" basedir="."
    xmlns:ac="hamburger:with-fries">

 <taskdef resource="net/sf/antcontrib/antlib.xml"
      uri="hamburger:with-fries">
     <classpath>
         <fileset dir="${basedir}/ant.lib/ant-contrib"/>
     </classpath>
</taskdef>

标准是使用类似antlib:net.sf.antcontrib:

<project name="my.project" default="package" basedir="."
    xmlns:ac="antlib:net.sf.antcontrib">

 <taskdef resource="net/sf/antcontrib/antlib.xml"
      uri="antlib:net.sf.antcontrib">
     <classpath>
         <fileset dir="${basedir}/ant.lib/ant-contrib"/>
     </classpath>
</taskdef>

不过,我喜欢使用项目的 URL.这样,如果有人想要有关 Ant-Contrib 任务的文档,他们就会知道 Ant-Contrib 项目所在的 URL.

However, I like using the URL of the project. That way, if someone wants documentation on Ant-Contrib tasks, they know the URL where the Ant-Contrib project lives.

在上述所有三种情况下,我都使用 ac 定义了 XML 命名空间.因此,您必须在所有 Ant-Contrib 任务名称前加上 ac: 前缀.你可以使用 antcontrib 或任何你喜欢的东西.使用 ac: 命名空间,您的 Ant-contrib 任务将如下所示:

In all three cases above, I've defined the XML namespace with ac. Thus, you have to prefix all Ant-Contrib task names with ac:. You could use antcontrib or whatever you like. With the ac: namespace, your Ant-contrib tasks will look like this:

<ac:if>
   <istrue value="${include.debug.code}"/>
   <ac:then>
        [...]
   </ac:then>
   <ac:else>
        [...]
   </ac:else>
<ac:if>

如果你跳过整个命名空间的事情,你可以简单地使用 Ant-Contrib 任务作为文档:

If you skip the whole namespace thing, you can simply use the Ant-Contrib tasks as documented:

<if>
   <istrue value="${include.debug.code}"/>
   <then>
        [...]
   </then>
   <else>
        [...]
   </else>

这篇关于如何在 Ant 中动态包含 ant-contrib.jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆