如何在Ant中动态包含ant-contrib.jar

时间:2012-10-15 08:33:48

标签: ant fdt

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

2 个答案:

答案 0 :(得分:12)

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

然后,在您的build.xml文件中,您可以执行以下操作:

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

我喜欢这样做,因为带有任务的可选罐子都包含在项目中。如果你检查版本控制系统中的所有内容,有人可以检查你的代码,并在不下载Ant-Contrib并自行安装的情况下进行构建。

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

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

<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可以是任何东西。例如:

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

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

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

如果跳过整个命名空间,可以直接使用Ant-Contrib任务:

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

答案 1 :(得分:4)

最佳解决方案是集成apache ivy依赖管理器。 Ivy可用于管理所有构建类路径Maven风格!

实施例

的ivy.xml

此文件描述了项目的第三方依赖项。 Ivy使用配置将文件逻辑分组在一起。在您的情况下,注意特殊的“构建”配置用于配置构建所需的ANT任务:

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
        <conf name="build"   description="3rd party ANT tasks"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>

        <!-- Build dependencies -->
        <dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" conf="build->default"/>
    </dependencies>

</ivy-module>

注意:

  • 默认情况下从Maven Central检索依赖关系,该关系现在托管大约90%的开源jar

的build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:antcontrib="antlib:net.sf.antcontrib">

    <target name="bootstrap" description="Install ivy">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get src="https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&amp;g=org.apache.ivy&amp;a=ivy&amp;v=LATEST&amp;e=jar"
             dest="${user.home}/.ant/lib/ivy.jar"/>
    </target>

    <target name="init" description="Use ivy to resolve classpaths">
        <ivy:resolve/>

        <ivy:report todir='build/ivy-reports' graph='false' xml='false'/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="runtime.path" conf="runtime"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>
        <ivy:cachepath pathid="build.path"   conf="build"/>
    </target>

    <target name="taskdefs" depends="init" description="Declare 3rd party ANT tasks">
        <taskdef uri="antlib:net.sf.antcontrib" classpathref="build.path"/>
    </target>

    <target name="build" depends="taskdefs" description="Build logic using ant-contrib tasks">
        <echo message="The first five letters of the alphabet are:"/>

        <antcontrib:for list="a,b,c,d,e" param="letter">
            <sequential>
                <echo>Letter @{letter}</echo>
            </sequential>
        </antcontrib:for>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="build"/>
    </target>

    <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
        <ivy:cleancache/>
    </target>

</project>

注意:

  • 有一个特殊的“bootstrap”目标用于启动新安装。不幸的是,常春藤不是以核心ANT分发的。
  • cachepath常春藤任务用于创建包含常春藤下载和缓存的jar的ANT路径。
  • 常春藤报告任务对于理解第三方库的传递依赖性非常有用。
  • antcontrib现在以ANT lib的形式发布,解释了奇怪的命名空间。
相关问题