---
title: "ivy中文参考文档(11)-ant任务"
author: "skydream"
date: "2009-07-23T08:21:00Z"
tags: ["Ant", "Apache", "EXT", "SVN", "XML"]
categories: ["编程语言"]
slug: "ivy-11-ant"
draft: false
lastmod: "2009-09-02T16:32:47Z"
id: 437909
url: "https://skydream.iteye.com/blog/437909"
---

1\) ant任务

    使用ivy的主要和最频繁的方式是在ant构建文件中。不过，ivy也可以作为独立的应用被调用。

    如果你使用ant版本1.6.0 或更高，你仅仅需要添加ivy的命名空间到你的项目中(你的project标签的属性)

xmlns:ivy="antlib:org.apache.ivy.ant"

    这样你就可以调用ivy任务了。

    如果你想让你的构建处理ivy.jar，不管是在ant的lib目录还是在本地的lib目录。你可以类似这样使用任务定义

&lt; path  id ="ivy.lib.path" &gt;  
     &lt; fileset  dir ="path/to/dir/with/ivy/jar"  includes ="\*.jar" /&gt;  
&lt;/ path &gt;  
&lt; taskdef  resource ="org/apache/ivy/ant/antlib.xml"  
         uri ="antlib:org.apache.ivy.ant"  classpathref ="ivy.lib.path" /&gt;

    结合项目命名空间中的antlib定义，它将从你的ant类库或者本地目录(在这个例子中试path/to/dir/with/ivy/jar)装载ivy类。

    如果你使用ant1.5.1或者更高，你不得不定义你在你的构建文件中使用的任务。例如：

&lt; taskdef  name ="ivy-configure"  classname ="org.apache.ivy.ant.IvyConfigure" /&gt;  
   &lt; taskdef  name ="ivy-resolve"  classname ="org.apache.ivy.ant.IvyResolve" /&gt;  
   &lt; taskdef  name ="ivy-retrieve"  classname ="org.apache.ivy.ant.IvyRetrieve" /&gt;  
   &lt; taskdef  name ="ivy-deliver"  classname ="org.apache.ivy.ant.IvyDeliver" /&gt;  
   &lt; taskdef  name ="ivy-publish"  classname ="org.apache.ivy.ant.IvyPublish" /&gt;

    注意：上面列出的任务并不是详尽。为了完整的有对应类的任务列表，请查看svn中的或者你使用的版本的antlib.xml

    然后你可以使用任务，但是检查他们的名字，下面的例子假设你使用ivy命名空间(ivy:xxx任务)，不过对于ant1.5你不能使用命名空间，如果你想遵循上面的任务定义可以使用ivy-xxx任务。

    如果你使用低于1.5.1的ant版本，你不能使用ivy任务...你应该作为外部程序来调用ivy。

    2) 从ant中调用ivy: 开始步骤

    一旦你的构建文件可以调用ivy任务，最简单的使用ivy的任务就是不带任何参数的调用ivy的retrieve任务。

&lt; ivy:retrieve  /&gt;

    这将使用默认值调用ivy，在很多项目中是可行的。实际上，它等同于

&lt; target  name ="resolve" &gt;  
     &lt; ivy:configure  /&gt;  
     
     &lt; ivy:resolve  file ="${ivy.dep.file}"  conf ="${ivy.configurations}"   /&gt;  
     
     &lt; ivy:retrieve  pattern ="${ivy.retrieve.pattern}"  conf ="${ivy.configurations}"   /&gt;  
&lt;/ target &gt;

    这三个任务遵循ivy获取依赖过程的3个主要步骤：

    * 首先configure任务通过给出一个xml配置文件的路径来讲述怎样找到依赖。  
    * 然后resolve任务实际解析ivy文件描述的依赖，并将这些依赖放置到ivy缓存(在配置文件中配置的一个目录)  
    * 最后retrieve任务从缓存中复制依赖到任何你想要的文件系统中。随后你可以用这些依赖用标准ant路径来构造你的classpath。

    为了更准确的理解ivy任务的行为，需要要知道在configure被调用的开始ivy装载了一个属性文件。这个属性文件包含以下属性：

ivy.project.dir = ${basedir}  
ivy.lib.dir = ${ivy.project.dir}/lib  
ivy.build.artifacts.dir = ${ivy.project.dir}/build/artifacts  
ivy.distrib.dir = ${ivy.project.dir}/distrib  
     
ivy.resolver.default.check.modified = false  
ivy.default.always.check.exact.revision = true

ivy.configurations = *  
ivy.resolve.default.type.filter = *  
ivy.status = integration  
ivy.dep.file = ivy.xml  
ivy.settings.file = ivysettings.xml  
ivy.retrieve.pattern = ${ivy.lib.dir}/\[artifact]-\[revision].\[ext]  
ivy.deliver.ivy.pattern = ${ivy.distrib.dir}/\[type]s/\[artifact]-\[revision].\[ext]  
ivy.publish.src.artifacts.pattern = ${ivy.distrib.dir}/\[type]s/\[artifact]-\[revision].\[ext]

ivy.report.output.pattern = \[organisation]-\[module]-\[conf].\[ext]

ivy.buildlist.ivyfilepath = ivy.xml

ivy.checksums=sha1,md5

    这些属性的最新版本你可以检查svn版本。

    2.0之后，在调用第一个ivy任务后，属性ivy.version将可用并包含被使用的ivy类库的版本。

    2) ivy任务属性: 概论  
    任务属性的值可能通过不同位置提供。这三个可能的位置是：

   1. 任务属性  
   2. 实例  
   3. 项目属性

    位置被要求使用这种顺序，因此任务在任务属性中设置的值将覆盖在ivy实例中发现的值，例如。

    这里考虑的ivy实例是类Ivy的一个实例，通过调用configure任务建立，并且随后被其他任务重用。因为大部分任务需要ivy实例，他们首先检查是否有实例可用(例如 configure已经被调用)，如果没有一个可用，则调用默认的configure，将得到的ivy实例结果用于后面的任务(除非另外一个configure被调用)。

    通常理解这些不是必须的，但是如果你忘记在其他任务前调用configure而在你的环境中configure步骤是必须的，这将导致一些问题。

    3) 主要任务的通常周期

![](http://ant.apache.org/ivy/history/latest-milestone/images/main-tasks.png)

    4) 示例

    这里有一个使用ivy的更加复杂的构建文件的例子：

&lt; project  xmlns:ivy ="antlib:org.apache.ivy.ant"  name ="sample"  default ="resolve" &gt;

     &lt; target  name ="resolve" &gt;  
         &lt; ivy:configure  file ="../ivysettings.xml"   /&gt;  
          
         &lt; ivy:resolve  file ="my-ivy.xml"  conf ="default, myconf"   /&gt;  
          
     &lt;/ target &gt;  
      
     &lt; target  name ="retrieve-default"  depends ="resolve" &gt;  
         &lt; ivy:retrieve  pattern ="lib/default/\[artifact]-\[revision].\[ext]"  conf ="default"   /&gt;  
     &lt;/ target &gt;

     &lt; target  name ="retrieve-myconf"  depends ="resolve" &gt;  
         &lt; ivy:retrieve  pattern ="lib/myconf/\[artifact]-\[revision].\[ext]"  conf ="myconf"   /&gt;  
     &lt;/ target &gt;

     &lt; target  name ="retrieve-all"  depends ="resolve" &gt;  
         &lt; ivy:retrieve  pattern ="lib/\[conf]/\[artifact]-\[revision].\[ext]"  conf ="\*"   /&gt;  
     &lt;/ target &gt;

     &lt; target  name ="deliver"  depends ="retrieve-all" &gt;  
         &lt; ivy:deliver  deliverpattern ="distrib/\[artifact]-\[revision].\[ext]"  
                     pubrevision ="1.1b4"  pubdate ="20050115123254"  status ="milestone"   /&gt;  
     &lt;/ target &gt;

     &lt; target  name ="publish"  depends ="deliver" &gt;  
         &lt; ivy:publish  resolver ="internal"    
                     artifactspattern ="distrib/\[artifact]-\[revision].\[ext]"    
                     pubrevision ="1.1b4"   /&gt;  
     &lt;/ target &gt;  
&lt;/ project &gt;

    所有的ivy任务在随后的页面中介绍。