Ant+Ivy起步

有了Ivy的幫忙,我們不需要爲了一個庫依賴管理而捨棄Ant去學那個難搞的Maven了。
基本配置步驟如下:
1、copy Ivy插件
到ant_home/lib下;
2、在項目根目錄下新建
ivysettings.xml
3、在項目根目錄下新建
ivy.xml,內容根據項目需要來;
4、修改你原來的build.xml,如下:

增加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裏面聲明瞭一下,但是你的項目已經可以編譯通過了,就好像那些第三方類庫已經在你本地了一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章