如何动态地包括蚂蚁contrib.jar在ANT [英] How to include ant-contrib.jar dynamically in ANT

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

问题描述

我在寻找一种方法,包括从ANT文件中一个.jar这样我就可以用它马上并调用其方法在我的目标。
在我的情况下,它是蚂蚁的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.

这可能吗?

谢谢!

推荐答案

最好的办法是把蚂蚁的Contrib里面的jar文件你的项目。例如,假设在的build.xml 是在你的项目的根目录。创建一个名为 ant.lib \\蚂蚁的contrib 您的项目里面,然后把蚂蚁的contrib的* .jar 在此文件夹中。您可以使用此方法用于其他可选的Ant任务,你可能需要(例如,常春藤,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>

因为与任务可选的罐子都包含在这个项目我喜欢做这种方式。如果检查一切到你的版本控制系统,有人可以检出你code和无需下载蚂蚁的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命名空间。这使你的蚂蚁的Contrib任务的preFIX为了避免万一任务名称冲突您使用具有相同的任务名称的其他可选的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声明在&LT;项目&GT; 标题。这将包含的 URI 的,将您的Ant任务的Contrib连接到您的XML命名空间。例如, AC:命名空间是所有蚂蚁的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>

这样做是符合交流的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。这样一来,如果有人想在蚂蚁的Contrib任务的文件,他们知道的网址,其中蚂蚁的Contrib项目的生活。

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命名空间。因此,你必须preFIX与所有蚂蚁的Contrib任务名称AC:。你可以使用 antcontrib 或任何你喜欢。随着 AC:命名空间,你的蚂蚁的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>

如果你跳过了整个空间的事情,你可以简单地使用蚂蚁的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>

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

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