Ivy與Ant集成

 

http://blog.csdn.net/lisonghua/archive/2009/11/05/4770260.aspx

 

 

 基本配置步驟如下: 

1、copy Ivy插件到ant_home/lib下; 

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>  

 

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

 

============================================================

 

可以任意轉載, 轉載時請務必以超鏈接形式標明文章原文出處 , 即下面的聲明.

 

原文出處:http://blog.chenlb.com/2009/02/apache-ant-ivy-quick-start.html

Ivy是一個免費基於Java的依賴管理器。它提供了一些強大的功能包括依賴傳遞,ant集成, maven存儲庫兼容,持續集成,html報告等

下載ivy 2.0 http://ant.apache.org/ivy/download.cgi ,校內鏡像:http://labs.xiaonei.com/apache-mirror/ant/ivy/2.0.0/apache-ivy-2.0.0-bin-with-deps.zip

下載好後安裝它,把它解壓到f:/ivy-2.0.0(把此目錄認爲是IVY_HOME),把IVY_HOME/ivy-2.0.0.jar放到 ANT_HOME/lib目錄下。然後命令行入到IVY_HOME/src/example/hello-ivy目錄,運行ant。然後它會下載依賴的所有jar包。

看下hello-ivy的依賴配置:

  1. < ivy-module   version = "2.0" >   
  2.     < info   organisation = "org.apache"   module = "hello-ivy" />   
  3.     < dependencies >   
  4.         < dependency   org = "commons-lang"   name = "commons-lang"   rev = "2.0" />   
  5.         < dependency   org = "commons-cli"   name = "commons-cli"   rev = "1.0" />   
  6.     </ dependencies >   
  7. </ ivy-module >   
  1. <ivy-module version="2.0">  
  2.     <info organisation="org.apache" module="hello-ivy"/>  
  3.     <dependencies>  
  4.         <dependency org="commons-lang" name="commons-lang" rev="2.0"/>  
  5.         <dependency org="commons-cli" name="commons-cli" rev="1.0"/>  
  6.     </dependencies>  
  7. </ivy-module>  

依賴commons-lang-2.0.jar 和 commons-cli-1.0.jar,ivy會自動下載,當然還有這些*.jar所依賴的jar, 如這裏的commons-cli-1.0.jar依賴commons-logging-1.0.jar,不用在ivy.xml文件定義。它們已經在lib 目錄下了。

然後你再一次運行ant,ivy不會再下載這些jar,因爲本地有緩存了。

當然也可以用ant report任務,輸出jar依賴報告,默認在build目錄,org.apache-hello-ivy-default.html。

延伸:默認緩存目錄爲${user.home}/cache。你也可以改它的默認目錄在運行ant時,設置,如ivy.default.ivy.user.dir=f:/ivy2,所以它會緩存到f:/ivy2/cache。

ant -Divy.default.ivy.user.dir=f:/ivy2

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