---
title: "Ant+Ivy快速起步"
author: "Software_King"
date: "2014-07-14T15:07:35Z"
tags: []
categories: ["企业架构"]
slug: "ant-ivy"
draft: false
lastmod: "2014-07-14T15:07:35Z"
id: 2091896
url: "https://software-king.iteye.com/blog/2091896"
---

有了Ivy的帮忙，我们不需要为了一个库依赖管理而舍弃Ant去学那个难搞的Maven了。   
基本配置步骤如下：   
1、copy Ivy插件（见[附件](http://dl.iteye.com/topics/download/f1136cc8-034b-3fc0-a50a-c25c85dcf4ab)）到ant\_home/lib下；   
2、在项目根目录下新建[ivysettings.xml](http://code.google.com/p/smartpagination/source/browse/trunk/ivysettings.xml)；   
3、在项目根目录下新建[ivy.xml](http://code.google.com/p/smartpagination/source/browse/trunk/ivy.xml)，内容根据项目需要来；   
4、修改你原来的build.xml，如下： 

Java代码  [![收藏代码](http://daquan198163.iteye.com/images/icon_star.png)]()

01. 增加ivy需要的属性：
02. &lt;property name="publish.version" value="0.1" /&gt;
03. &lt;property name="ivy.report.todir" value="build" /&gt;
04. &lt;property name="repository.dir" value="d:/Local\_Repository" /&gt;
05. 初始化ivy：
06. &lt;ivy:settings file="ivysettings.xml" /&gt;
07. 添加resolve target，用于下载依赖包：
08. &lt;target name="resolve" description="--&gt; resolve and retrieve dependencies with ivy"&gt;
09. &lt;ivy:resolve file="ivy.xml" conf="\*" /&gt;
10. &lt;ivy:retrieve pattern="${lib.dir}/\[conf]/\[artifact]-\[revision].\[ext]" /&gt;
11. &lt;/target&gt;
12. 让原来的compile依赖于resolve：
13. &lt;target name="compile" depends="resolve"
14. 添加publish target，这个不是必须的：
15. &lt;target name="publish" depends="jar" description="publish"&gt;
16. &lt;ivy:publish resolver="local" pubrevision="${publish.version}" overwrite="true"&gt;
17. &lt;artifacts pattern="dist/\[artifact].\[ext]" /&gt;
18. &lt;/ivy:publish&gt;
19. &lt;echo message="project ${ant.project.name} released with version ${publish.version}" /&gt;
20. &lt;/target&gt;
21. 添加report target用于生产漂亮的依赖报告，当然这个也不是必须的：
22. &lt;target name="report" depends="resolve" description="--&gt; resolve and retrieve dependencies with ivy"&gt;
23. &lt;ivy:report /&gt;
24. &lt;/target&gt;

完整的build.xml示例见[http://code.google.com/p/smartpagination/source/browse/trunk/build.xml](http://code.google.com/p/smartpagination/source/browse/trunk/build.xml) 

Over！ 

至此，你已经为蚂蚁插上了Ivy的翅膀，下面的工作只是锦上添花而已——在Eclipse配置Ivy，这个工作的作用是把ivy.xml变成classpath的一部分，使得我们只需要维护ivy.xml不需要维护.classpath文件。   
配置步骤：   
1、Window－＞preference－＞ant－＞RunTime－＞Classpath－＞Ant Home Entries，   
右边Add External Jars，添加org.apache.ivy\_2.1.0.cr1\_20090319213629.jar。   
2、安装Ivy插件:Help－＞Install new software－＞add，   
Name: IvyDE，Location: http://www.apache.org/dist/ant/ivyde/updatesite   
安装成功后重启eclipse；   
3、重启eclipse后，Window－＞preference－＞ivy－＞settings   
Ivy settings path设为d:/workspace/ivysettings.xml(这个值取决于你的环境) 

至此，Eclipse的ivy插件配置好了，然后就可以为你的项目classpath添加ivy依赖了：   
选中项目-&gt;右键 属性－＞Java Build Path－＞Libraries-&gt;Add Library...-&gt;IvyIDE Managed Dependencies-&gt;finish-&gt;OK   
然后神奇的事情就出现了——虽然你一个jar包也没下载，只是在ivy.xml里面声明了一下，但是你的项目已经可以编译通过了，就好像那些第三方类库已经在你本地了一样。

- [org.apache.ivy\_2.1.0.cr1\_20090319213629.jar](http://dl.iteye.com/topics/download/f1136cc8-034b-3fc0-a50a-c25c85dcf4ab) (879.2 KB)