有了Ivy的幫忙,咱們不須要爲了一個庫依賴管理而捨棄Ant去學那個難搞的Maven了。
基本配置步驟以下:
一、copy Ivy插件(見
附件)到ant_home/lib下;
二、在項目根目錄下新建
ivysettings.xml;
三、在項目根目錄下新建
ivy.xml,內容根據項目須要來;
四、修改你原來的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>
完整的build.xml示例見
http://code.google.com/p/smartpagination/source/browse/trunk/build.xml
Over!
至此,你已經爲螞蟻插上了Ivy的翅膀,下面的工做只是錦上添花而已——在Eclipse配置Ivy,這個工做的做用是把ivy.xml變成classpath的一部分,使得咱們只須要維護ivy.xml不須要維護.classpath文件。
配置步驟:
一、Window->preference->ant->RunTime->Classpath->Ant Home Entries,
右邊Add External Jars,添加org.apache.ivy_2.1.0.cr1_20090319213629.jar。
二、安裝Ivy插件:Help->Install new software->add,
Name: IvyDE,Location: http://www.apache.org/dist/ant/ivyde/updatesite
安裝成功後重啓eclipse;
三、重啓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裏面聲明瞭一下,可是你的項目已經能夠編譯經過了,就好像那些第三方類庫已經在你本地了同樣。