程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> ivy教程(6)-項目依賴

ivy教程(6)-項目依賴

編輯:關於JAVA

這個示例將舉例說明在兩個項目之間的依賴。

depender項目聲明它使用dependee 項目。我們將闡明兩個事情:

* 被獨立的項目聲明的公共類庫將被依賴的項目自動獲取

* depender項目將獲取dependee項目的"最新"版本

1) 使用到的項目

1. dependee

dependee項目非常簡單。它依賴apache類庫commons-lang並只包含一個類: standalone.Main : standalone.Main提供兩個服務:

* 返回項目的版本

* 使用org.apache.commons.lang.WordUtils.capitalizeFully大寫一個字符串

這裡是項目的內容:

* build.xml: 項目的ant構建文件

* ivy.xml: 項目的ivy文件

* src\standalone\Main.java: 項目僅有的一個類

看一下ivy.xml文件:

<ivy-module version="1.0">
     <info organisation="org.apache" module="dependee"/>
     <dependencies>
         <dependency org="commons-lang" name="commons-lang" rev="2.0"/>
     </dependencies>
</ivy-module>

ivy依賴文件只聲明了一個依賴apache commons-lang類庫。

2) depender

項目depender也非常簡單。它僅僅聲明了一個對dependee項目的最新版本的依賴,而它僅僅包含一個 類depending.Main,干了兩件事情:

* 通過對 standalone.Main.getVersion() 的調用獲取獨立項目的版本。

* 通過對standalone.Main.capitalizeWords(str)的調用轉換字符串

看一下ivy.xml文件:

<ivy-module version="1.0">
     <info organisation="org.apache" module="depender"/>
     <dependencies>
         <dependency name="dependee" rev="latest.integration" />
     </dependencies>
</ivy-module>

2) 設置

ivy設置在settings目錄下,包含兩個文件:

* ivysettings.properties: 屬性文件

* ivysettings.xml: 包含設置的文件

讓我們看一下ivysettings.xml文件:

<ivysettings>
     <properties file="${ivy.settings.dir}/ivysettings.properties"/>
     <settings defaultCache="${ivy.settings.dir}/ivy-cache"  defaultResolver="libraries"/>
     <resolvers>
         <filesystem name="projects">
             <artifact pattern="${repository.dir}/[artifact]-[revision].[ext]" />
             <ivy pattern="${repository.dir}/[module]-[revision].xml" />
         </filesystem>
         <ibiblio name="libraries" m2compatible="true" usepoms="false" />
     </resolvers>
     <modules>
         <module organisation="org.apache" name="dependee" resolver="projects"/>
     </modules>
</ivysettings>

文件包含四個主要標簽:properties, settings, resolvers 和 modules.

1. properties

這個標簽僅僅如ant所做的那樣為ivy程序裝載一些屬性。

2. settings

這個標簽負責初始化一些為ivy程序使用的參數。ivy用於緩存制品的目錄將是包含vysettings.xml文 件的目錄自身的名為ivy-cache的子目錄。

第二個參數,告訴ivy使用一個名為"libraries"的解析器作為默認解析器。更多的信息可以再設置參 考文檔中找到。

3. resolvers

這個標簽定義要使用的解析器。這裡我們有兩個定義要的解析器:"projects" 和 "libraries".

名為"projects"的文件系統解析器可以通過在本地文件系統中定位依賴來解析內部依賴。

名為"libraries"的ibiblio解析器用於查找在maven2 倉庫內的依賴,但是不使用maven poms。

4. modules

modules標簽容許配置用哪個解析器來解析哪個依賴。這個實際上只對應於一個模塊,但是可以使用正 則表達式,或者其他類型的表達式(如glob 表達式)。

對於其他模塊(例如所有不是org.apache#dependee的模塊),因為這裡沒有特別設置,將使用默認解析 器: "libraries".

3) walkthrough

step 1 : 准備

打開一個dos或者shell窗口,並進入"src/example/dependence"目錄

step 2 : 清理

在提示符下: ant

這將清理整個項目目錄樹(已編譯的類和獲得的libs)和ivy緩存。你可以再每次你想清理這個例子時做 這個事情。

step 3 : 發布dependee項目

進入depende目錄,並發布項目

I:\dependee>ant publish
Buildfile: src\example\dependence\standalone\build.xml
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 ::  http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file =  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | standalone |  working@BEN-ScokartG ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found [ commons-lang | commons-lang | 2.0 ] in  libraries
[ivy:retrieve] downloading http://www.ibiblio.org/maven/commons-lang/jars/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[jar] (5388ms)
[ivy:retrieve] :: resolution report ::
     ---------------------------------------------------------------------
     |                  |            modules             ||   artifacts   |
     |       conf       | number| search|dwnlded|evicted||  number|dwnlded|
     ---------------------------------------------------------------------
     |      default     |   1   |   1   |   0   |   0    ||   1   |   1   |
     ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | standalone ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     1 artifacts copied, 0 already retrieved
compile:
     [mkdir] Created dir:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\classes
     [javac] Compiling 1 source file to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\classes
jar:
[propertyfile] Creating new property file:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\classes\version .properties
       [jar] Building jar:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\standalone.jar< BR> publish:
[ivy:publish] :: delivering :: [ org.apache | standalone | working@BEN-ScokartG  ] :: 1 :: release :: Sun Nov 04 20:50:24 CET 2007
[ivy:publish]     delivering ivy file to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone/build/ivy.xml
[ivy:publish] :: publishing :: [ org.apache | standalone ]
[ivy:publish]     published standalone to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config/repository/standalone- 1.jar
[ivy:publish]     published ivy to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config/repository/standalone- 1.xml
      [echo] project standalone released with version 1
BUILD SUCCESSFUL
Total time: 11 seconds

這裡我們可以看到:

* 項目依賴1個類型(1個制品)

* 這個類庫不再ivy緩存中並且因此被下載 (1 downloaded)

* 這個項目被發行,版本號為1

給了給出關於publish的更多的解析,如你所見publish任務的調用導致了兩個主要事情:

* 交付被解析的ivy文件到build/ivy.xml

這個被完成時因為默認publish任務不僅僅發行制品們而且也發行ivy文件。因此它查看ivy文件應該被 發行到的路徑,使用artifactspattern: ${build.dir}/[artifact].[ext].

對於一個ivy文件,這將解析到build/ivy.xml。因為這個文件不存在,它自動調用deliver任務交付一 個被解析的ivy文件到這個目的地。

* 制品dependee的發行和ivy文件到倉庫

都僅僅是在當前項目中找到的文件拷貝,更明確的在build目錄中。這是因為artifactspattern 被設 置為${build.dir}/[artifact].[ext],因此dependee 制品分布在build/dependee.jar而ivy文件在 build/ivy.xml. 並且因為我們要求publish任務使用"projects"解析器發行他們,這些文件被復制到 repository\dependee-1.jar 和 repository\dependee-1.xml,遵守我們設置中的artifact和ivy模式。

step 4: 運行depender

進入depender目錄並運行ant

I:\depender>ant
Buildfile: src\example\dependence\depending\build.xml
clean:
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 ::  http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file =  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | depending |  working@BEN-ScokartG ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found [ org.apache | standalone | 1 ] in projects
[ivy:retrieve]     [1] [ org.apache | standalone | latest.integration ]
[ivy:retrieve]     found [ commons-lang | commons-lang | 2.0 ] in  libraries
[ivy:retrieve] downloading  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\repository\standalone- 1.jar ...
[ivy:retrieve] .. (1kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] [ org.apache | standalone | 1  ]/standalone.jar[jar] (20ms)
[ivy:retrieve] :: resolution report ::
     ---------------------------------------------------------------------
     |                  |            modules             ||   artifacts   |
     |       conf       | number| search|dwnlded|evicted||  number|dwnlded|
     ---------------------------------------------------------------------
     |      default     |   2   |   1   |   0   |   0    ||   2   |   1   |
     ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | depending ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     2 artifacts copied, 0 already retrieved
compile:
     [mkdir] Created dir:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending\build\classes
     [javac] Compiling 1 source file to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending\build\classes
run:
      [java] you are using version 1 of class standalone.Main
      [java] standard message : i am depending.Main and standalone.Main will  do the job for me
      [java]     [standalone.Main] capitalizing string "i am depending.Main  and standalone.Main will do the job for me" using  org.apache.commons.lang.WordUtils
      [java] capitalized message : I Am Depending.main And Standalone.main  Will Do The Job For Me
BUILD SUCCESSFUL
Total time: 3 seconds

這裡我們看到:

* 項目依賴2個類庫(2個制品)

* 類庫中的一個在緩存中因為僅有一個被下載(1 downloaded)

* ivy得到項目dependee的版本1. 對standalone.Main.getVersion()的調用返回1.如果你查看 depender/lib目錄,你將看到dependee-1.jar,這是項目dependee的版本1的制品。

* 對standalone.Main.capitalizeWords(str)的調用成功,這意味著在classpath中有需要的類庫。如 果你查看 lib目錄,你將看到類庫commons-lang-2.0.jar已經被獲取。這個類庫是項目"dependee"聲明要 使用的,因此ivy同樣為了 depender項目而獲得它。

step 5: dependee 項目的新版本

和我們在步驟3中做的類型,再次發行dependee項目。這將導致這個項目的新版本。

現在如果你查看你的倉庫文件夾,你將發現dependee項目發行的2個版本。

讓我們看這個:

I:\dependee>ant publish
Buildfile: src\example\dependence\standalone\build.xml
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 ::  http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file =  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | standalone |  working@BEN-ScokartG ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found [ commons-lang | commons-lang | 2.0 ] in  libraries
[ivy:retrieve] :: resolution report ::
     ---------------------------------------------------------------------
     |                  |            modules             ||   artifacts   |
     |       conf       | number| search|dwnlded|evicted||  number|dwnlded|
     ---------------------------------------------------------------------
     |      default     |   1   |   0   |   0   |   0    ||   1   |   0   |
     ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | standalone ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     0 artifacts copied, 1 already retrieved
compile:
jar:
[propertyfile] Updating property file:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\classes\version .properties
       [jar] Building jar:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\standalone.jar< BR> publish:
    [delete] Deleting:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone\build\ivy.xml
[ivy:publish] :: delivering :: [ org.apache | standalone | working@BEN-ScokartG  ] :: 2 :: release :: Sun Nov 04 20:50:33 CET 2007
[ivy:publish]     delivering ivy file to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\standalone/build/ivy.xml
[ivy:publish] :: publishing :: [ org.apache | standalone ]
[ivy:publish]     published standalone to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config/repository/standalone- 2.jar
[ivy:publish]     published ivy to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config/repository/standalone- 2.xml
      [echo] project standalone released with version 2
BUILD SUCCESSFUL
Total time: 2 seconds

好,現在我們的倉庫包含了dependee項目的2個版本,其他項目可以關聯到任何版本。

I:\dependee>dir ..\settings\repository /w
[.]                [..]               dependee-1.jar    dependee-1.xml   dependee-2.jar   dependee-2.xml
I:\dependee>

step 6: 在depender項目中獲取新版本

再次運行depender項目時我們會期望什麼?有兩個主要事情:

* 獲得項目dependee的版本2作為latest.integration 版本

* 運行測試必須顯示項目dependee的版本為2

讓我們試試吧!

I:\depender>ant
Buildfile: src\example\dependence\depending\build.xml
clean:
    [delete] Deleting 3 files from  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending
    [delete] Deleted 4 directories from  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 ::  http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: file =  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | depending |  working@BEN-ScokartG ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found [ org.apache | standalone | 2 ] in projects
[ivy:retrieve]     [2] [ org.apache | standalone | latest.integration ]
[ivy:retrieve]     found [ commons-lang | commons-lang | 2.0 ] in  libraries
[ivy:retrieve] downloading  C:\dev\data\opensource_workspace\ivy\src\example\dependence\config\repository\standalone- 2.jar ...
[ivy:retrieve] .. (1kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] [ org.apache | standalone | 2  ]/standalone.jar[jar] (100ms)
[ivy:retrieve] :: resolution report ::
     ---------------------------------------------------------------------
     |                  |            modules             ||   artifacts   |
     |       conf       | number| search|dwnlded|evicted||  number|dwnlded|
     ---------------------------------------------------------------------
     |      default     |   2   |   1   |   0   |   0    ||   2   |   1   |
     ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | depending ]
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     2 artifacts copied, 0 already retrieved
compile:
     [mkdir] Created dir:  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending\build\classes
     [javac] Compiling 1 source file to  C:\dev\data\opensource_workspace\ivy\src\example\dependence\depending\build\classes
run:
      [java] you are using version 2 of class standalone.Main
      [java] standard message : i am depending.Main and standalone.Main will  do the job for me
      [java]     [standalone.Main] capitalizing string "i am depending.Main  and standalone.Main will do the job for me" using  org.apache.commons.lang.WordUtils
      [java] capitalized message : I Am Depending.main And Standalone.main  Will Do The Job For Me
BUILD SUCCESSFUL
Total time: 5 seconds

很好,我們得到了期望的結果,run target展示我們正在使用項目dependee的main類的版本2.如果我 們看一下resolve target的結果,我們可以看到一個制品從ivy緩存中被下載。實際上這個文件就是從倉 庫中得到的項目dependee的版本2,你現在可以在ivy- cache目錄中得到它。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved