用Apache Ivy实现项目里的依赖管理 .

来源:http://blog.csdn.net/stefaniezhao/article/details/6412748

Apache Ivy是一个管理项目依赖的工具。

它与Maven
Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意。Maven 引入了 JAR 文件公共存储库的概念,可通过公开的 Web 服务器访问(称为 ibiblio)。Maven 的方法减少了 JAR 文件膨胀的情况,不会占用大多数版本控制存储库。但使用 Maven 时,它会鼓励您采用其 “惯例优于配置” 的方法来构建软件,这会制约您定制构建脚本的灵活性。

但问题是Maven过于Heavy,而大部分已有的项目都用Ant做build,所以Ivy是更加合适的选择。

Ivy 提供了最一致、可重复、易于维护的方法,来管理项目的所有构建依赖项。

用Ivy进行项目管理

开始使用 Ivy 非常简单,只需创建两个 Ivy 特有的文件,添加一些 Ant 目标即可。Ivy 特有的文件是 ivy.xml 和一个 Ivy 设置文件。ivy.xml 文件中列举了项目的所有依赖项。ivysettings.xml 文件(可以随意为此文件命名)用于配置从中下载有依赖关系的 JAR 文件的存储库。

Ivy的安装

Ivy依赖于Ant,所以需要先安装Ant,然后下载Ivy,将他的jar文件考到Ant的lib下面,就可以在Ant里使用Ivy进行依赖管理了。

下载ivy 2.0

http://ant.apache.org/ivy/download.cgi

校内镜像:http://labs.xiaonei.com/apache-mirror/ant/ivy/2.0.0/apache-ivy-2.0.0-bin-with-deps.zip

下载好后安装它,把它解压到f:/ivy-2.0.0(把此目录认为是IVY_HOME),把IVY_HOME/ivy-2.0.0.jar放到 ANT_HOME/lib目录下。然后命令行入到IVY_HOME/src/example/hello-ivy目录,运行ant。然后它会下载依赖的所有jar包。

看下hello-ivy的依赖配置:

1. <ivy-module version="2.0">
2. <info organisation="org.apache" module="hello-ivy"/>
3. <dependencies>
4. <dependency org="commons-lang" name="commons-lang" rev="2.0"/>
5. <dependency org="commons-cli" name="commons-cli" rev="1.0"/>
6. </dependencies>
7. </ivy-module>

依赖commons-lang-2.0.jar 和 commons-cli-1.0.jar,ivy会自动下载,当然还有这些*.jar所依赖的jar, 如这里的commons-cli-1.0.jar依赖commons-logging-1.0.jar,不用在ivy.xml文件定义。它们已经在lib 目录下了。

然后你再一次运行ant,ivy不会再下载这些jar,因为本地有缓存了。

当然也可以用ant report任务,输出jar依赖报告,默认在build目录,org.apache-hello-ivy-default.html。

延伸:默认缓存目录为${user.home}/cache。你也可以改它的默认目录在运行ant时,设置,如ivy.default.ivy.user.dir=f:/ivy2,所以它会缓存到f:/ivy2/cache

使用Ivy

ivy.xml

在 ivy 中,配置(conf)是比较重要的概念,它对应一组依赖的jar。比较一个编译期间的conf(compile),它依赖commons-lang。运行期间它还要依赖log4j,可以定义一个运行期配置(runtime),它扩展compile。配置是可以扩展的,依次类推,可以定义一个测试用的jar 依赖配置(test),它扩展runtime。

ivy的jar依赖配置在ivy.xml文件里定义与说明,类似:

1. <ivy-module version="1.0">
2. <info organisation="com.chenlb" module="ivy-hello"/>
3.
4. <configurations>
5. <conf name="compile" visibility="private" description="compilation only need jar" />
6. <conf name="runtime" visibility="private" extends="compile" description="for runtime need jar" />
7. <conf name="test" visibility="private" extends="runtime" description="for test" />
8. <conf name="default" visibility="public" extends="runtime" description="default jar" />
9. </configurations>
10. <dependencies>
11. <dependency org="commons-lang" name="commons-lang" rev="2.1" conf="compile->default"/>
12. <dependency org="log4j" name="log4j" rev="1.2.12" conf="runtime->default"/>
13.
14. <dependency org="junit" name="junit" rev="3.8.2" conf="test->default"/>
15. </dependencies>
16. </ivy-module>

上面定义了,compile、runtime、test、default配置(一个配置对应一个jar依赖集)。compile只依赖 commons-lang-2.1.jar;但runtime还依赖log4j-1.2.12.jar;测试用的还依赖junit-3.8.2.jar。

在Ant里使用ivy。

加ivy的xmlns。如

1. <project name="ivy-hello" default="init" xmlns:ivy="antlib:org.apache.ivy.ant">
2. <!-- ... -->
3. </project>

下载jar。

1. <target name="resolve" description="--> retreive dependencies with ivy">
2. <ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
3. </target>

ivy.lib.dir默认是当前目录下的lib。[conf]是配置名。[artifact]是jar发布的名,[revision]是版本号,[ext]是扩展名。

classpath

1. <path id="build.lib.path">
2. <fileset dir="${lib.dir}/build" />
3. </path>
4. <path id="test.lib.path">
5. <fileset dir="${lib.dir}/test" />
6. <pathelement location="${build.java.dir}" />
7. </path>

可以在编译任务用${compile.lib.path}的classpath,test的也同样。

现在可以基本运行ant 和 ivy了,运行ant resolve就可以看到ivy下载相关的jar包。

如何构建自己的Repository

Ivy的例子里已经包括了一个构建repo的例子,在build-a-ivy-repository里,主要运行build.xml就可以构建一个简单的repo,如果你想用namespace管理一个专业的repo,可以运行ant maven2-namespace,就会在本地构建一个专业的repo。

Repo-Location/[org]/[name]/ivy-[version].xml
e.g. apache/commons-lang/

       contains a jar and a definition file, ivy-[version].xml

下面我们看看ivy-[version].xml里是什么内容

<ivy-module version="1.0" xmlns:m="http://ant.apache.org/ivy/maven ">

<info organisation="apache"
module="commons-lang"
revision="1.0"
status="release"
publication="20051124132021"
namespace="maven2">
<description homepage="">
.....
</description>
<m:maven.plugins>null maven-surefire-plugin null</m:maven.plugins>
</info>
<configurations>
<conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/>
<conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/>
.....
</configurations>
<publications>
<artifact name="commons-lang" type="jar" ext="jar" conf="master"/>
<artifact name="commons-lang" type="javadoc" ext="jar" conf="javadoc" m:classifier="javadoc"/>
</publications>
<dependencies>
<dependency org="junit" name="junit" rev="3.7" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
</dependencies>

</ivy-module>

其实他和普通的ivy.xml的格式是一样,只是用于定义jar本身的依赖,只是多了publication对提供的jar进行描述。

IVY的配置 - ivysettings.xml

ivy本身有3中repo的类型:local,shared和public的。

ivy默认的setting:在jar里org.apache.ivy.core.setting包中

<ivysettings>
<settings defaultResolver="default"/>
<include url="${ivy.default.settings.dir}/ivysettings-public.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>
 
你可以在这里将public的repo改为你自己的repo
 
<include url="http://myserver/ivy/myivysettings-public.xml "/>
 
myivysettings-public.xml
 
<ivysettings>
<resolvers>
<filesystem name="public">
<ivy pattern="/path/to/my/public/rep/[organisation]/[module]/ivy-[revision].xml" />
<artifact pattern="/path/to/my/public/rep/[organisation]/[module]/[artifact]-[revision].[ext]" />
</filesystem>
</resolvers>
</ivysettings>
 
这样当resolve是,ivy会先从user local,然后是shared,然后会在你设置的public repo下载jar。
 
更多的关于Ivy的信息请查阅Apache Ivy的官方doc: http://ant.apache.org/ivy/

猜你喜欢

转载自selvemen.iteye.com/blog/1389099