這個教程介紹ivy文件中的模塊配置的使用。ivy模塊配置事實上是一個非常重要的概念。某些人甚至 告訴我使用ivy而不用ivy配置就像吃乳酪而不動就在你旁邊的Chateau Margaux 1976!
嚴肅的說,ivy中的配置可以更好的理解為你的模塊的視圖,你將可以看到在這裡他們將如何被高效地 使用。
關於配置的參考文件可以在這裡和這裡找到。
1) Introduction
源文件在這裡 src/example/configurations/multi-projects.
我們有兩個項目:
- filter-framework 是一個類庫,定義一個api來過濾字符串數組,這個api有兩個實現.
- myapp 是一個使用filter-framework的非常小的應用.
這個類庫產生3個制品:
- api的jar
- 一個沒有外部依賴的實現的jar
- 另一個需要commons-collections來執行的實現jar
應用僅僅需要api來編譯,在運行時可以使用兩個實現中的任意一個。
2) 類庫項目
在這個教程中我們定義的第一個項目是filter-framework.
為了得到一個良好處理的制品發布定義,我們定義配置來計劃讓其他人使用我們的類庫的使用方式。
1. ivy.xml 文件
<ivy-module version="1.0">
<info organisation="org.apache" module="filter-framework"/>
<configurations>
<conf name="api" description="only provide filter framework API"/>
<conf name="homemade-impl" extends="api" description="provide a home made implementation of our api"/>
<conf name="cc-impl" extends="api" description="provide an implementation that use apache common collection
framework"/>
<conf name="test" extends="cc-impl" visibility="private" description="for testing our framework"/>
</configurations>
<publications>
<artifact name="filter-api" type="jar" conf="api" ext="jar"/>
<artifact name="filter-hmimpl" type="jar" conf="homemade-impl" ext="jar"/>
<artifact name="filter-ccimpl" type="jar" conf="cc-impl" ext="jar"/>
</publications>
<dependencies>
<dependency org="commons-collections" name="commons-collections" rev="3.1" conf="cc- impl->default"/>
<dependency org="junit" name="junit" rev="3.8" conf="test->default"/>
</dependencies>
</ivy-module>
2. 解釋
如你所見,我們定義了3個公共配置和一個私有配置(為測試定義junit依賴)。
2個實現配置homemade-impl, cc-impl 繼承自api配置,因此在api中定義的制品在它的繼承配置中同 樣是需要的。
在publications標簽中,我們定義了我們要產生的制品(在這裡它們是jar)並給他們使用了配置。
後面當其他模塊要使用我們的類庫時,他們將有非常靈活的方式來定義他們需要的東西。
3. 在實戰中檢驗
The library project is build using ant. Open a shell in the root directory of the project and type ant.
類庫項目使用ant來構建。在項目的根目錄下打開一個shell,並輸入ant。
Buildfile: src\example\configurations\multi-projects\filter- framework\build.xml
clean:
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071105200109 - 20071105200109 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] No ivy:settings found for the default reference 'ivy.instance'. A default instance will be used
[ivy:retrieve] no settings file found, using default...
[ivy:retrieve] :: loading settings :: url = jar:file:/c:/dev/data/opensource_workspace/ivy/build/artifact/ivy-
core.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | filter-framework | working@BEN-ScokartG ]
[ivy:retrieve] confs: [api, homemade-impl, cc-impl, test]
[ivy:retrieve] found [ commons-collections | commons-collections | 3.1 ] in public
[ivy:retrieve] found [ junit | junit | 3.8 ] in public
[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-collections/commons- collections/3.1/commons-collections-
3.1.jar ...
[ivy:retrieve] ....................................................................
[ivy:retrieve] ....................................................................
[ivy:retrieve] ...................................
[ivy:retrieve] ....................................................................
[ivy:retrieve] ................................... (546kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] [ commons-collections | commons-collections | 3.1 ]/commons-collections.jar[jar] (8322ms)
[ivy:retrieve] downloading http://repo1.maven.org/maven2/junit/junit/3.8/junit-3.8.jar ...
[ivy:retrieve] ....................................................................
[ivy:retrieve] ... (118kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] [ junit | junit | 3.8 ]/junit.jar[jar] (3015ms)
[ivy:retrieve] :: resolution report ::
---------------------------------------------------------------------
| | modules || artifacts |
| conf | number| search|dwnlded|evicted|| number|dwnlded|
---------------------------------------------------------------------
| api | 0 | 0 | 0 | 0 || 0 | 0 |
| homemade-impl | 0 | 0 | 0 | 0 || 0 | 0 |
| cc-impl | 1 | 1 | 0 | 0 || 1 | 1 |
| test | 2 | 2 | 0 | 0 || 2 | 2 |
---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | filter-framework ]
[ivy:retrieve] confs: [api, homemade-impl, cc-impl, test]
[ivy:retrieve] 3 artifacts copied, 0 already retrieved
build:
[mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\build
[mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\distrib
[javac] Compiling 4 source files to C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-
projects\filter-framework\build
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[jar] Building jar: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\distrib\filter-api.jar
[jar] Building jar: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\distrib\filter-hmimpl.jar
[jar] Building jar: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\distrib\filter-ccimpl.jar
test:
[mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\build\test-report
[mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-projects\filter-
framework\build\test-classes
[javac] Compiling 3 source files to C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-
projects\filter-framework\build\test-classes
[junit] Running filter.ccimpl.CCFilterTest
[junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0.02 sec
[junit] Running filter.hmimpl.HMFilterTest
[junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0 sec
publish:
[ivy:publish] :: delivering :: [ org.apache | filter-framework | working@BEN- ScokartG ] :: 1.3 :: release :: Mon Nov 05
21:10:46 CET 2007
[ivy:publish] delivering ivy file to C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-
projects\filter-framework\distrib/ivy.xml
[ivy:publish] :: publishing :: [ org.apache | filter-framework ]
[ivy:publish] published filter-hmimpl to C:\Documents and Settings\scokartg\.ivy2\local/org.apache/filter-
framework/1.3/jars/filter-hmimpl.jar
[ivy:publish] published filter-api to C:\Documents and Settings\scokartg\.ivy2\local/org.apache/filter-
framework/1.3/jars/filter-api.jar
[ivy:publish] published filter-ccimpl to C:\Documents and Settings\scokartg\.ivy2\local/org.apache/filter-
framework/1.3/jars/filter-ccimpl.jar
[ivy:publish] published ivy to C:\Documents and Settings\scokartg\.ivy2 \local/org.apache/filter-
framework/1.3/ivys/ivy.xml
[echo] project filter-framework released with version 1.3
BUILD SUCCESSFUL
Total time: 20 seconds
ant的默認target是publish。
這個target使用ivy發布我們的類庫到本地倉庫。
因為我們沒有指定任何倉庫路徑,因此使用默認倉庫。 ({home.dir}/.ivy2/local/org.apache/filter-framework/)現在我們准備好了使用我們的類庫。
3) 應用項目
現在我們已經完成了我們美妙的類庫,我們想用它!
這個教程帶來一個名為myapp的示例應用。
1. ivy.xml文件
<ivy-module version="1.0">
<info organisation="org.apache" module="myapp"/>
<configurations>
<conf name="build" visibility="private" description="compilation only need api jar" />
<conf name="noexternaljar" description="use only company jar" />
<conf name="withexternaljar" description="use company jar and third party jars" />
</configurations>
<dependencies>
<dependency org="org.apache" name="filter-framework" rev="latest.integration" conf="build->api; noexternaljar-
>homemade-impl; withexternaljar->cc-impl"/>
</dependencies>
</ivy-module>
2. 解釋
我們創建了3個配置來定義我們想使用應用的方式。
build配置定義了編譯時的依賴,而這個只需要來自filter-framework的api conf。
其他配置定義了運行時依賴。一個將僅僅使用"home-made"的jars,而另一個將使用外部的jars。
我們同樣定義了對於上面類庫的依賴。
在依賴中我們使用配置映射來匹配我們和類庫的配置。
你可以在這裡找到更多的關於配置映射的信息。
1. build->api : 這裡我們告訴ivy,我們的build配置依賴於依賴的api配置。
2. noexternaljar->homemade-impl : 這裡我們告訴ivy,我們的noexternaljar 配置依賴於依賴 的homemade-impl配置。
3. withexternaljar->cc-impl : 這裡我們告訴ivy,我們的的withexternaljar 配置依賴於依賴 的cc-impl配置。
注意我們從不定義在每個配置中需要的任何依賴制品:依賴模塊文件將定義發布的制品和將被哪個配 置使用。
在ant的build.xm文件中我們定義解析的target如下:
<target name="resolve" description="--> retreive dependencies with ivy">
<ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact].[ext]"/>
</target>
當我們調用這個target時,ivy將使用我們的在root文件夾中的ivy.xml來做解析並隨後獲取所有的制 品。制品被獲取並分別保存在和他們所屬的配置對應目錄下。在調用這個target後你的lib目錄將看起來 像這樣:
Repertoire de D:\ivy\src\example\configurations\multi-projects\myapp\lib
01/24/2006 11:19 AM build
01/24/2006 11:19 AM noexternaljar
01/24/2006 11:19 AM withexternaljar
0 fichier(s) 0 octets
Repertoire de D:\ivy\src\example\configurations\multi-projects\myapp\lib\build
01/24/2006 10:53 AM 1,174 filter-api.jar
1 fichier(s) 1,174 octets
Repertoire de D:\ivy\src\example\configurations\multi- projects\myapp\lib\noexternaljar
01/24/2006 10:53 AM 1,174 filter-api.jar
01/24/2006 10:53 AM 1,030 filter-hmimpl.jar
2 fichier(s) 2,204 octets
Repertoire de D:\ivy\src\example\configurations\multi- projects\myapp\lib\withexternaljar
01/24/2006 10:53 AM 559,366 commons-collections.jar
01/24/2006 10:53 AM 1,174 filter-api.jar
01/24/2006 10:53 AM 1,626 filter-ccimpl.jar
3 fichier(s) 562,166 octets
如你所見對於每個配置我們現在都有了一個jar的集合。
讓我們試試啟動我們的app。
3. 在實戰中檢驗
使用ant來運行應用。
默認ant target是run-cc,將使用apache commons-collections實現來啟動應用。
Buildfile: src\example\configurations\multi-projects\myapp\build.xml
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] No ivy:settings found for the default reference 'ivy.instance'. A default instance will be used
[ivy:retrieve] no settings file found, using default...
[ivy:retrieve] :: loading settings :: url = jar:file:/c:/dev/data/opensource_workspace/ivy/build/artifact/ivy-
core.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | myapp | working@BEN- ScokartG ]
[ivy:retrieve] confs: [build, noexternaljar, withexternaljar]
[ivy:retrieve] found [ org.apache | filter-framework | 1.3 ] in local
[ivy:retrieve] [1.3] [ org.apache | filter-framework | latest.integration ]
[ivy:retrieve] found [ commons-collections | commons-collections | 3.1 ] in public
[ivy:retrieve] downloading C:\Documents and Settings\scokartg\.ivy2 \local\org.apache\filter-framework\1.3\jars\filter-
api.jar ...
[ivy:retrieve] .. (1kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] [ org.apache | filter-framework | 1.3 ]/filter-api.jar[jar] (40ms)
[ivy:retrieve] downloading C:\Documents and Settings\scokartg\.ivy2 \local\org.apache\filter-framework\1.3\jars\filter-
hmimpl.jar ...
[ivy:retrieve] .. (1kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] [ org.apache | filter-framework | 1.3 ]/filter-hmimpl.jar[jar] (20ms)
[ivy:retrieve] downloading C:\Documents and Settings\scokartg\.ivy2 \local\org.apache\filter-framework\1.3\jars\filter-
ccimpl.jar ...
[ivy:retrieve] .. (1kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve] [SUCCESSFUL ] [ org.apache | filter-framework | 1.3 ]/filter-ccimpl.jar[jar] (80ms)
[ivy:retrieve] :: resolution report ::
---------------------------------------------------------------------
| | modules || artifacts |
| conf | number| search|dwnlded|evicted|| number|dwnlded|
---------------------------------------------------------------------
| build | 1 | 1 | 0 | 0 || 1 | 1 |
| noexternaljar | 1 | 1 | 0 | 0 || 2 | 2 |
| withexternaljar | 2 | 1 | 0 | 0 || 3 | 2 |
---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | myapp ]
[ivy:retrieve] confs: [build, noexternaljar, withexternaljar]
[ivy:retrieve] 6 artifacts copied, 0 already retrieved
build:
[mkdir] Created dir: C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi- projects\myapp\build
[javac] Compiling 1 source file to C:\dev\data\opensource_workspace\ivy\src\example\configurations\multi-
projects\myapp\build
run-cc:
[java] Filtering with:class filter.ccimpl.CCFilter
[java] Result :[two, tree]
BUILD SUCCESSFUL
Total time: 4 seconds
僅使用自制jars來啟動應用時很直接的。
鍵入ant run-hm。
Buildfile: src\example\configurations\multi-projects\myapp\build.xml
resolve:
[ivy:retrieve] :: Ivy 2.0.0-beta1-local-20071104204849 - 20071104204849 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] No ivy:settings found for the default reference 'ivy.instance'. A default instance will be used
[ivy:retrieve] no settings file found, using default...
[ivy:retrieve] :: loading settings :: url = jar:file:/c:/dev/data/opensource_workspace/ivy/build/artifact/ivy-
core.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: [ org.apache | myapp | working@BEN- ScokartG ]
[ivy:retrieve] confs: [build, noexternaljar, withexternaljar]
[ivy:retrieve] found [ org.apache | filter-framework | 1.3 ] in local
[ivy:retrieve] [1.3] [ org.apache | filter-framework | latest.integration ]
[ivy:retrieve] found [ commons-collections | commons-collections | 3.1 ] in public
[ivy:retrieve] :: resolution report ::
---------------------------------------------------------------------
| | modules || artifacts |
| conf | number| search|dwnlded|evicted|| number|dwnlded|
---------------------------------------------------------------------
| build | 1 | 1 | 0 | 0 || 1 | 0 |
| noexternaljar | 1 | 1 | 0 | 0 || 2 | 0 |
| withexternaljar | 2 | 1 | 0 | 0 || 3 | 0 |
---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: [ org.apache | myapp ]
[ivy:retrieve] confs: [build, noexternaljar, withexternaljar]
[ivy:retrieve] 0 artifacts copied, 6 already retrieved
build:
run-hm:
[java] Filtering with:class filter.hmimpl.HMFilter
[java] Result :[two, tree]
BUILD SUCCESSFUL
Total time: 3 seconds
很好,我們得到了同樣的結果,但是我們可以看到實現的類是不一樣的。
4) 結論
你應該盡可能多的使用配置。
配置是ivy中非常重要的概念。他們容許你按照意願來分組制品。
當你書寫假定要被重用的項目的ivy文件時,使用配置來容許人們只獲取他們需要的東西,而不需要通 過在依賴塊中使用制品標簽來手工指定。