Ant-Ivy使用

來源:互聯網
上載者:User

Apache Ivy是專門用來管理項目的jar包依賴的。我們知道Maven已經有很出色的這方面的功能,如果你已經在使用Maven,就沒必要使用Ivy了。但是其實Maven除了這方面功能,還有很多強大的功能,如果你只需要管理jar包依賴而已,那麼可以只用Ivy就夠了,用Maven就有點大材小用。 

1.      首先,你的機器應該已經安裝了Ant和JDK,最好是1.6或更高版本的Ant,並在電腦環境變數裡已經配置好了正確的ANT_HOME,JAVA_HOME和path變數。

2.      接下來,在某磁碟,比如D盤,隨意建一個檔案夾,用來安裝Ivy,比如D:/AntIvy

3.      然瀏覽器,訪問以下連結,可以直接下載得到build.xml檔案:

http://ant.apache.org/ivy/history/latest-milestone/samples/build.xml

將下載到的build.xml檔案儲存在D:/AntIvy中。 

4.      開啟DOS命令列cd進入該目錄D:/AntIvy

輸入ant,斷行符號,就會開始安裝Ivy了。不過可能失敗,有些時候電腦無法直接下載一些東西,需要代理,所以,允許ant命令前需要設定Ant代理,先運行:

set ANT_OPTS=-Dhttp.proxyHost=xxx.xxx.xxx -Dhttp.proxyPort=1234

然後再運行ant命令就可以了,這樣將會進行安裝Ivy。 

5.      安裝成功後,D:/AntIvy下會有ivy和src目錄,ivy目錄下有個ivy.jar,將該jar包拷貝到ANT_HOME/lib下。(其實整個說來說去,最重要的就是要獲得這ivy.jar檔案。其他的都是浮雲。撇開這些步驟,如果你能用其他方式獲得這個ivy.jar那麼就等於你已經可以使用Ivy了,最簡單的方式就是去maven
Repo中搜ivy就可以下載了,哈哈。)

 

6.      經過以上5個步驟後,實際上已經完成Ivy的安裝和配置了,下面我們來看一個Example使用Ivy。訪問以下路徑,會有幾個Ivy例子:

https://svn.apache.org/repos/asf/ant/ivy/core/trunk/src/example

可以下載到hello-ivy,將該例子儲存下來,DOS命令列進入該hello-ivy工程根目錄。運行命令ant如下: 

 

D:\AntIvy\src\example\hello-ivy>ant

Buildfile: build.xml

 

resolve:

No ivy:settings found for the default reference 'ivy.instance'.  A default insta

nce will be used

no settings file found, using default...

:: Ivy 2.0.0-beta1 - 20071206070608 :: http://ant.apache.org/ivy/ ::

:: loading settings :: url = jar:file:/D:/tool/Ant/lib/ivy.jar!/org/apache/ivy/c

ore/settings/ivysettings.xml

[ivy:retrieve] :: resolving dependencies :: org.apache#hello-ivy;working@SHNI132

 

[ivy:retrieve]  confs: [default]

[ivy:retrieve]  found commons-lang#commons-lang;2.0 in public

[ivy:retrieve]  found commons-cli#commons-cli;1.0 in public

[ivy:retrieve]  found commons-logging#commons-logging;1.0 in public

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-lang/commons-la

ng/2.0/commons-lang-2.0.jar ...

[ivy:retrieve] ...........

[ivy:retrieve] .... (165kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-lang#commons-lang;2.0!commons-lang.jar (35

55ms)

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-cli/commons-cli

/1.0/commons-cli-1.0.jar ...

[ivy:retrieve] ..... (29kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-cli#commons-cli;1.0!commons-cli.jar (1559m

s)

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-logging/commons

-logging/1.0/commons-logging-1.0.jar ...

[ivy:retrieve] ... (21kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-logging#commons-logging;1.0!commons-loggin

g.jar (1715ms)

[ivy:retrieve] :: resolution report :: resolve 6705ms :: artifacts dl 6829ms

[ivy:retrieve]  :: evicted modules:

[ivy:retrieve]  commons-lang#commons-lang;1.0 by [commons-lang#commons-lang;2.0]

 in [default]

        ---------------------------------------------------------------------

        |                  |            modules            ||   artifacts   |

        |       conf       | number| search|dwnlded|evicted|| number|dwnlded|

        ---------------------------------------------------------------------

        |      default     |   4   |   3   |   0   |   1   ||   3   |   3   |

        ---------------------------------------------------------------------

[ivy:retrieve] :: retrieving :: org.apache#hello-ivy

[ivy:retrieve]  confs: [default]

[ivy:retrieve]  3 artifacts copied, 0 already retrieved (216kB/78ms)

 

run:

    [mkdir] Created dir: D:\AntIvy\src\example\hello-ivy\build

    [javac] Compiling 1 source file to D:\AntIvy\src\example\hello-ivy\build

     [java] standard message : hello ivy !

     [java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy !

 

BUILD SUCCESSFUL

Total time: 16 seconds

D:\AntIvy\src\example\hello-ivy>

這隻是第一次運行ant,需要下載jar包等所以慢,還列印了不少東西,但是第二次再運行ant時,就很快了。

7.      好了,現在我們可以看看hello-ivy執行個體工程中根目錄中ivy.xml和build.xml,這是很簡單的檔案,你看看一定會有所領悟的,而且你知道你可以隨便copy,然後修修改改就可以用到其他的項目中。比如現在我們在Eclipse中建立了一個JavaProject,我們只要把這兩個檔案copy到新項目的根目錄下。Eclipse中一般有內建的Ant,所以我麼運行Ant的build時,使用的是內建的Ant,這時需要配置一下Eclipse中的Ant,加上我們的ivy.jar,你懂的。 

8.      Window  --> Preferences --> Ant --> Runtime  --> 點選Ant Home Entries或GlobalEntries  -->  Add External Jar將我們安裝的Ivy下的ivy/ivy.jar添加進來就可以了。 

其實,使用Ivy最重要的就是獲得ivy.jar,這個可以上http://search.maven.org/搜尋ivy就可以獲得了。將獲得的jar包放入Ant的lib裡面就OK啦。

其次就是build.xml和ivy.xml,這裡主要的是一些Ant命令的寫法,在build.xml中如何編輯標籤來配置ant命令,另外ivy.xml中配置的是dependency,而且兩個檔案可以合二為一,如果你對這些配置已經很熟悉,那麼就沒什麼好看的啦。

 在Eclipse中安裝IvyDE外掛程式

http://www.apache.org/dist/ant/ivyde/updatesite

Help  --> Install New Software...  -->  Add

然後名稱設為:ApacheIvy update site

Location即上面的url:http://www.apache.org/dist/ant/ivyde/updatesite

然後安裝的時候選擇這個新添加的UpdateSite,勾選ApacheIvyDE,Next,開始安裝。

好了,重啟Eclipse,OK。安裝好外掛程式是可以讓Eclipse也可以像Maven那樣管理jar包。

 

當然,還有其他的方法,比如下載個IvyDE的.zip自己copy到Eclipse的plugin和feature裡也可以。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.