有了Ivy的帮忙,我们不需要为了一个库依赖管理而舍弃Ant去学那个难搞的Maven了。
基本配置步骤如下: 1、copy Ivy插件到ant_home/lib下;ivy安装
简单的安装方法: 直接从网上下载ivy.jar,然后保存在ant安装目录的lib下即可。
复杂的方式:如果没有下载到或从其他渠道获得ivy.jar文件,则需要如下步骤安装了。
1.1 浏览器下载build.xml
地址: http://ant.apache.org/ivy/history/latest-milestone/samples 地址下下载build.xml,然后在某个盘中保存
1.2 安装ivy
在控制台中跳到保存build.xml的路径下然后输入ant,将自动安装ivy。
不过这样的安装有可能出错,有些时候电脑无法直接下载一些东西,需要代理,所以,允许ant命令前需要设置Ant代理,先运行:
set ANT_OPTS=-Dhttp.proxyHost=xxx.xxx.xxx -Dhttp.proxyPort=1234
然后再运行ant命令就可以了,这样将会进行安装Ivy。
运行完成之后会在build.xml路径下生成
在ivy下就自动生成了ivy.jar 文件,然后copy到ANT_HOME的lib目录下即可。这样ivy就安装好了!
2、在项目根目录下新建ivysettings.xml;
3、在项目根目录下新建ivy.xml,内容根据项目需要来; 4、修改你原来的build.xml,如下: Java代码 增加ivy需要的属性: <property name="publish.version" value="0.1" /> <property name="ivy.report.todir" value="build" /> <property name="repository.dir" value="d:/Local_Repository" /> 初始化ivy: <ivy:settings file="ivysettings.xml" /> 添加resolve target,用于下载依赖包: <target name="resolve" description="--> resolve and retrieve dependencies with ivy"> <ivy:resolve file="ivy.xml" conf="*" /> <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[revision].[ext]" /> </target> 让原来的compile依赖于resolve: <target name="compile" depends="resolve" 添加publish target,这个不是必须的: <target name="publish" depends="jar" description="publish"> <ivy:publish resolver="local" pubrevision="${publish.version}" overwrite="true"> <artifacts pattern="dist/[artifact].[ext]" /> </ivy:publish> <echo message="project ${ant.project.name} released with version ${publish.version}" /> </target> 添加report target用于生产漂亮的依赖报告,当然这个也不是必须的: <target name="report" depends="resolve" description="--> resolve and retrieve dependencies with ivy"> <ivy:report /> </target> 增加ivy需要的属性: <property name="publish.version" value="0.1" /> <property name="ivy.report.todir" value="build" /> <property name="repository.dir" value="d:/Local_Repository" /> 初始化ivy: <ivy:settings file="ivysettings.xml" /> 添加resolve target,用于下载依赖包: <target name="resolve" description="--> resolve and retrieve dependencies with ivy"> <ivy:resolve file="ivy.xml" conf="*" /> <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[revision].[ext]" /> </target> 让原来的compile依赖于resolve: <target name="compile" depends="resolve" 添加publish target,这个不是必须的: <target name="publish" depends="jar" description="publish"> <ivy:publish resolver="local" pubrevision="${publish.version}" overwrite="true"> <artifacts pattern="dist/[artifact].[ext]" /> </ivy:publish> <echo message="project ${ant.project.name} released with version ${publish.version}" /> </target> 添加report target用于生产漂亮的依赖报告,当然这个也不是必须的: <target name="report" depends="resolve" description="--> resolve and retrieve dependencies with ivy"> <ivy:report /> </target> 完整的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依赖了: 选中项目->右键 属性->Java Build Path->Libraries->Add Library...->IvyIDE Managed Dependencies->finish->OK 然后神奇的事情就出现了——虽然你一个jar包也没下载,只是在ivy.xml里面声明了一下,但是你的项目已经可以编译通过了,就好像那些第三方类库已经在你本地了一样。 ----------