在某些情况下,您的模块描述(即Ivy文件、Maven的pom文件)与模块工件(即jar)分开放置。那你该怎么办呢?
让我们看看您的Ivy发行版中的src/example/dual目录。它包含一个生成文件和3个目录:
项目非常简单,只包含一个简单类:example.HelloIvy。它依赖于两个库:Apache commons-lang和Apache commons-httpclient。
项目内容如下:
让我们看一下ivy.xml文件:
<ivy-module version="1.0">
<info organisation="org.apache" module="hello-ivy"/>
<dependencies>
<dependency org="commons-httpclient" name="commons-httpclient" rev="2.0.2"/>
<dependency org="commons-lang" name="commons-lang" rev="2.6"/>
</dependencies>
</ivy-module>
As you can see, nothing special here… Indeed, Ivy’s philosophy is to keep Ivy files independent of the way dependencies are resolved.(正如你所看到的,这里没有什么特别的…事实上,Ivy的理念是保持Ivy文件独立于解决依赖关系的方式。)
The Ivy settings are defined in the ivysettings.xml file located in the settings directory. Here is what it contains, followed by an explanation.(The ivy settings在ivysettings.xml文件,该文件位于settings目录中。下面是它所包含的内容,然后是一个解释。)
<ivysettings>
<settings defaultResolver="dual-example"/>
<resolvers>
<dual name="dual-example">
<filesystem name="ivys">
<ivy pattern="${ivy.settings.dir}/../repository/[module]-ivy-[revision].xml"/>
</filesystem>
<ibiblio name="ibiblio" m2compatible="true" usepoms="false"/>
</dual>
</resolvers>
</ivysettings>
在这里,我们配置了一个解析器,即默认的解析器,这是一个双解析器。这个双解析器有两个子解析器:第一个子解析器称为双解析器的“ivy”或“metadata”解析器,第二个子解析器称为“artifact”解析器。 It is important that the dual resolver has exactly two sub resolvers in this given order.(重要的是,双解析器正好有两个子解析器在这个给定的顺序。)
metadata解析器(这里是文件系统解析器)仅用于查找模块描述符,在本例中是Ivy文件。这里显示的设置告诉Ivy,所有Ivy文件都在repository目录中,根据以下模式命名:[module]-Ivy-[revision].xml。如果我们检查repository目录,我们可以确认它包含一个名为commons-httpclient-ivy-2.0.2.xml的文件。此文件与模式匹配,因此解析器将找到它。
artifact解析器只是一个ibiblio解析器,在m2compatible模式下配置为使用Maven 2存储库,并使用usepoms=“false”确保它不会使用Maven 2元数据。
请注意,这是没有必要的,因为双解析器中的第二个解析器(artifact解析器)从未被要求查找模块元数据。
Open a shell (or command line) window, and go to the dual directory.
On the prompt type : ant
This will clean up the entire project directory tree (compiled classes and retrieved libs) and the Ivy cache. You can run this each time you want to clean up this example.
Go to the project directory. And simply run ant.
[ivy@apache:/ivy/dual/project]$ ant
Buildfile: /ivy/dual/project/build.xml
resolve:
[ivy:retrieve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file = /ivy/dual/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: org.apache#hello-ivy;working@apache
[ivy:retrieve] confs: [default]
[ivy:retrieve] found commons-httpclient#commons-httpclient;2.0.2 in ivys
[ivy:retrieve] found commons-logging#commons-logging;1.0.4 in ibiblio
[ivy:retrieve] found commons-lang#commons-lang;2.6 in ibiblio
[ivy:retrieve] downloading https://repo1.maven.org/maven2/commons-httpclient/commons-httpclient/2.0.2/commons-httpclient-2.0.2.jar ...
[ivy:retrieve] ......................... (220kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] commons-httpclient#commons-httpclient;2.0.2!commons-httpclient.jar (1940ms)
[ivy:retrieve] downloading https://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6.jar ...
[ivy:retrieve] ............................. (277kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] commons-lang#commons-lang;2.6!commons-lang.jar (1518ms)
[ivy:retrieve] downloading https://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.4/commons-logging-1.0.4.jar ...
[ivy:retrieve] ................ (37kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] commons-logging#commons-logging;1.0.4!commons-logging.jar (1020ms)
[ivy:retrieve] :: resolution report :: resolve 1542ms :: artifacts dl 4483ms
---------------------------------------------------------------------
| | modules || artifacts |
| conf | number| search|dwnlded|evicted|| number|dwnlded|
---------------------------------------------------------------------
| default | 3 | 3 | 1 | 0 || 3 | 3 |
---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: org.apache#hello-ivy
[ivy:retrieve] confs: [default]
[ivy:retrieve] 3 artifacts copied, 0 already retrieved (534kB/8ms)
run:
[mkdir] Created dir: /ivy/dual/project/build
[javac] Compiling 1 source file to /ivy/dual/project/build
[java] standard message : Hello Ivy!
[java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy!
[java] Oct 20, 2019 11:25:02 AM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse
[java] WARNING: Redirect from protocol http to https is not supported
[java] head status code with httpclient: 301
[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: 13 seconds
如您所见,Ivy不仅下载了commons-lang和commons-httpclient,还下载了commons-logging。事实上,commons-logging是httpclient的一个依赖项,正如我们在reppository目录中的httpclient Ivy文件中所看到的:
<ivy-module version="1.0">
<info
organisation="commons-httpclient"
module="commons-httpclient"
revision="2.0.2"
status="release"
publication="20041010174300"/>
<dependencies>
<dependency org="commons-logging" name="commons-logging" rev="1.0.4" conf="default"/>
</dependencies>
</ivy-module>
So everything seemed to work. The Ivy file was found in the repository directory and the artifacts have been downloaded from ibiblio.(所以一切似乎都正常。在repository目录中找到了Ivy文件,这些工件已经从ibiblio下载。)
This kind of setup can be useful if you don’t want to rely on the Maven 2 repository for metadata, or if you want to take full advantage of Ivy files for some or all modules. Combining chain and dual resolvers should give you enough flexibility to meet almost any requirement.(如果您不想依赖Maven 2存储库来获取元数据,或者您想充分利用Ivy文件来实现某些或所有模块,这种设置非常有用。结合链和双解析器应该给你足够的灵活性,以满足几乎任何要求。)