Ant+Ivy快速起步

有了Ivy的幫忙,咱們不須要爲了一個庫依賴管理而捨棄Ant去學那個難搞的Maven了。
基本配置步驟以下:
一、copy Ivy插件(見 附件)到ant_home/lib下;
二、在項目根目錄下新建 ivysettings.xml
三、在項目根目錄下新建 ivy.xml,內容根據項目須要來;
四、修改你原來的build.xml,以下:
Java代碼   收藏代碼
  1. 增長ivy須要的屬性:  
  2. <property name="publish.version" value="0.1" />  
  3. <property name="ivy.report.todir" value="build" />  
  4. <property name="repository.dir" value="d:/Local_Repository" />  
  5.   
  6. 初始化ivy:  
  7. <ivy:settings file="ivysettings.xml" />  
  8.   
  9. 添加resolve target,用於下載依賴包:  
  10. <target name="resolve" description="--> resolve and retrieve dependencies with ivy">  
  11.         <ivy:resolve file="ivy.xml" conf="*" />  
  12.         <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[revision].[ext]" />  
  13. </target>  
  14.   
  15. 讓原來的compile依賴於resolve:  
  16. <target name="compile" depends="resolve"  
  17.   
  18. 添加publish target,這個不是必須的:  
  19. <target name="publish" depends="jar" description="publish">  
  20.         <ivy:publish resolver="local" pubrevision="${publish.version}" overwrite="true">  
  21.             <artifacts pattern="dist/[artifact].[ext]" />  
  22.         </ivy:publish>  
  23.         <echo message="project ${ant.project.name} released with version ${publish.version}" />  
  24. </target>  
  25.   
  26. 添加report target用於生產漂亮的依賴報告,固然這個也不是必須的:  
  27. <target name="report" depends="resolve" description="--> resolve and retrieve dependencies with ivy">  
  28.         <ivy:report />  
  29. </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裏面聲明瞭一下,可是你的項目已經能夠編譯經過了,就好像那些第三方類庫已經在你本地了同樣。
相關文章
相關標籤/搜索