---
title: "ivy教程(5)-双重解析器"
author: "skydream"
date: "2009-09-27T20:58:00Z"
tags: ["Ant", "Apache", "OpenSource", "XML", "maven"]
categories: ["编程语言"]
slug: "ivy-5"
draft: false
lastmod: "2009-10-09T21:51:32Z"
id: 483879
url: "https://skydream.iteye.com/blog/483879"
---

在一些情况下，会发生这样的事情:你的模块描述符(ivy文件，maven pom, ...)被放置在一个地方，而模块的制品(jars,...)在另外一个地方。

    双重解析器用于满足这种类型的需求，而这个教程将展示如何使用它。

    1) 项目描述

    让我们看一下你的ivy发行包里面的src/example/dual目录.  
    它包含一个构建文件和3个目录:

    * settings: b包含ivy设置文件  
    * repository: ivy文件的示例仓库  
    * project: 利用ivy双重解析器的项目

    1. dual项目  
    项目非常简单，只包含一个简单的类: example.Hello  
    它依赖两个类库: Apache commons-lang 和 Apache commons-httpclient.  
    这里是项目的内容:

    * build.xml: 项目的ant构建文件  
    * ivy.xml: ivy项目文件  
    * src\\example\\Hello.java: 项目仅有的一个类

    让我们看一下ivy.xml文件:

&lt; ivy-module  version ="1.0" &gt;  
     &lt; info  organisation ="org.apache"  module ="hello-ivy" /&gt;  
     &lt; dependencies &gt;  
         &lt; dependency  org ="commons-httpclient"  name ="commons-httpclient"  rev ="2.0.2" /&gt;  
         &lt; dependency  org ="commons-lang"  name ="commons-lang"  rev ="2.0" /&gt;  
     &lt;/ dependencies &gt;  
&lt;/ ivy-module &gt;

    如你所见，这里没有任何特殊的东西...实际上，这就是ivy的原理: 保持ivy文件独立于依赖解析的方式。

    2. ivy设置

    ivy设置在settings目录中；它只包含一个文件: ivysettings.xml.

&lt; ivysettings &gt;  
   &lt; settings  defaultResolver ="dual-example" /&gt;  
   &lt; resolvers &gt;  
     &lt; dual  name ="dual-example" &gt;  
       &lt; filesystem  name ="ivys" &gt;  
         &lt; ivy  pattern ="${ivy.settings.dir}/../repository/\[module]-ivy-\[revision].xml"   /&gt;  
       &lt;/ filesystem &gt;  
       &lt; ibiblio  name ="ibiblio"  m2compatible ="true"  usepoms ="false"   /&gt;  
     &lt;/ dual &gt;  
   &lt;/ resolvers &gt;  
&lt;/ ivysettings &gt;

    这里我们配置了一个解析器，默认的，它是一个双重解析器。这个双重解析器有两个子解析器：第一个被称为是双重解析器的"ivy"或"metadata"解析器，而第二个被称为"artifact" 解析器。重要的是双重解析器必须严格的拥有两个遵循这个给定顺序的解析器。

    metadata解析器，这里是一个文件解析器，仅仅用于查找模块的描述符，在这个案例中是ivy文件。这个解析器给出的设置阐述说所有ivy文件都在同一个目录中，命名形如\[module]-ivy-\[revision].xml.如果我们检查仓库目录，我们可以确认它包含一个名为commons-httpclient-ivy-2.0.2.xml的文件。它满足给定的模式并将因此被这个解析器找到。

    artifact解析器就是一个ibiblio 解析器，配置为m2compatible 模式以便使用maven2仓库，属性 usepoms="false" 确保它不会使用maven2 metadata。注意，这并不是严格要求，因为在双重解析器中的第二个解析器(artifact解析器)本来就不会被要求查找模块metadata。

    2) walkthrough

    step 1 : 准备  
    打开一个dos或者shell窗口，并进去"dual"目录

    step 2 : 清理  
    在提示符下: ant

    这将清理整个项目目录树(已编译的类和获得的libs)和ivy缓存。  
    你可以再每次你想清理这个例子时做这个事情。

    step 3 : 运行项目  
    进入projuect目录，并简单运行ant

I:\\dual\\project&gt;ant  
Buildfile: src\\example\\dual\\project\\build.xml

resolve:  
\[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 :: http://ant.apache.org/ivy/ ::  
\[ivy:retrieve] :: loading settings :: file = C:\\dev\\data\\opensource\_workspace\\ivy\\src\\example\\dual\\config\\ivysettings.xml  
\[ivy:retrieve] :: resolving dependencies :: \[ org.apache | hello-ivy | working@BEN-ScokartG ]  
\[ivy:retrieve]     confs: \[default]  
\[ivy:retrieve]     found \[ commons-httpclient | commons-httpclient | 2.0.2 ] in ivys  
\[ivy:retrieve]     found \[ commons-httpclient | commons-logging | 1.0.4 ] in ibiblio  
\[ivy:retrieve]     found \[ commons-lang | commons-lang | 2.0 ] in ibiblio  
\[ivy:retrieve] downloading http://www.ibiblio.org/maven/commons-httpclient/jars/commons-httpclient-2.0.2.jar ...  
\[ivy:retrieve] ............................ ............................  
\[ivy:retrieve] ............................ ............................  
\[ivy:retrieve] ... ... ... ... (220kB)  
\[ivy:retrieve] .. (0kB)  
\[ivy:retrieve]     \[SUCCESSFUL ] \[ commons-httpclient | commons-httpclient | 2.0.2 ]/commons-httpclient.jar\[jar] (11676ms)  
\[ivy:retrieve] downloading http://www.ibiblio.org/maven/commons-lang/jars/commons-lang-2.0.jar ...  
\[ivy:retrieve] ............................ ............................  
\[ivy:retrieve] ............................ (165kB)  
\[ivy:retrieve] .. (0kB)  
\[ivy:retrieve]     \[SUCCESSFUL ] \[ commons-lang | commons-lang | 2.0 ]/commons-lang.jar\[jar] (7651ms)  
\[ivy:retrieve] downloading http://www.ibiblio.org/maven/commons-logging/jars/commons-logging-1.0.4.jar ...  
\[ivy:retrieve] ............................ (37kB)  
\[ivy:retrieve] .. (0kB)  
\[ivy:retrieve]     \[SUCCESSFUL ] \[ commons-httpclient | commons-logging | 1.0.4 ]/commons-logging.jar\[jar] (9724ms)  
\[ivy:retrieve] :: resolution report ::  
    ---------------------------------------------------------------------  
    |                  |            modules            ||   artifacts   |  
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|  
    ---------------------------------------------------------------------  
    |      default     |   3   |   3   |   0   |   0   ||   3   |   3   |  
    ---------------------------------------------------------------------  
\[ivy:retrieve] :: retrieving :: \[ org.apache | hello-ivy ]  
\[ivy:retrieve]     confs: \[default]  
\[ivy:retrieve]     3 artifacts copied, 0 already retrieved

run:  
    \[mkdir] Created dir: C:\\dev\\data\\opensource\_workspace\\ivy\\src\\example\\dual\\project\\build  
    \[javac] Compiling 1 source file to C:\\dev\\data\\opensource\_workspace\\ivy\\src\\example\\dual\\project\\build  
     \[java] standard message : hello ivy !  
     \[java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy !  
     \[java] head status code with httpclient: 200  
     \[java] now check if httpclient dependency on commons-logging has been realized  
     \[java] found logging class in classpath: interface org.apache.commons.logging.Log

BUILD SUCCESSFUL  
Total time: 37 seconds

    你可以看到，ivy不仅仅下载commons-lang 和commons-httpclient，而且还有commons-logging.实际上，commons-logging是httpclient的依赖，我们可以看到在仓库目录中找到的httpclient 的ivy文件.

&lt; ivy-module  version ="1.0" &gt;  
     &lt; info  
         organisation ="commons-httpclient"  
        module ="commons-httpclient"  
        revision ="2.0.2"  
        status ="release"  
        publication ="20041010174300" /&gt;  
     &lt; dependencies &gt;  
         &lt; dependency  org ="commons-logging"  name ="commons-logging"  rev ="1.0.4"  conf ="default" /&gt;  
     &lt;/ dependencies &gt;  
&lt;/ ivy-module &gt;

    想这样每个东西都工作的很好，ivy文件在仓库目录下被找到而制品从ibiblio下载。

    这种类型的配置在你不想依赖maven2仓库的metadata或者你想为一些或者全部模块完全利用ivy文件时有用。

    有关双重解析器的完整的解析，请查阅对应的参考文档。