Ivy的模塊配置(module configurations)是一個很是重要的概念,Ivy裏的configurations 能夠理解爲模塊的概覽。apache
以Ivy自帶的例子學習,源代碼位於src/example/configurations/multi-projects.api
裏面有兩個工程數組
-filter-framework 是一個庫,定義了一個api來過濾字符串數組以及兩個對該api的實現app
-myapp是一個很是小的app,它會使用到filter-framework。ide
filter-framework工程會產生3個組件:一個api jar,一個沒有擴展依賴的對api的實現的jar,以及一個須要commons-collections去執行的實現jar。學習
咱們首先來看一下filter-framework工程。爲了有一個更細粒度的組件發佈定義,咱們定義了幾個configurations,每個都映射到一組其餘工程可使用的組件。測試
ivy.xml定義以下ui
<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>
如你所見,咱們定義了4個configurations,3個是public的,1個是private(junit用來測試的依賴)。spa
兩個實現的配置,homemade-impl 和cc-impl 繼承了api 配置,這樣全部定義在api的組件都會是擴展配置的一部分code
在publications標籤裏,咱們定義了要生成的組件(在這裏就是jar包),而且將他們分配給一個配置。當其餘工程使用這個庫時,它們就能夠靈活的獲取它們所須要的組件。
使用Ivy的publish任務咱們能夠將這些組件發佈到存儲庫裏。而後其餘工程就可使用這些發佈上去的jar包。下面咱們就看看myapp是如何使用這些存儲庫裏的組件的。
myapp的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>
咱們建立了3個配置來定義不一樣的方式去使用該應用。build 配置定義了編譯時的依賴,所以編譯時只須要filter-framework工程的api配置。其餘兩個配置定義了運行時的依賴。一個只使用"home-made"jar包,另一個則須要用到擴展jar包。
咱們還定義了一個對以前庫的依賴。在這個依賴裏,咱們使用配置映射來匹配咱們的配置和所依賴的組件的配置。
1.build->api: 咱們告訴Ivy build配置依賴於依賴模塊的api配置
2.noexternaljar->homemade-impl:意思是咱們的noexternaljar配置依賴於所依賴模塊的homemade-impl配置
3.withexternaljar->cc-impl: 是說咱們的withexternaljar配置依賴於所依賴模塊的cc-impl配置。
請注意咱們並無在每一個配置裏聲明任何咱們須要依賴的組件,是依賴模塊的的ivy文件聲明瞭要發佈的組件以及每一個組件應用於哪一個配置。
在Ant build.xml文件裏,咱們定義了「resolve」 target:
<target name="resolve" description="--> retreive dependencies with ivy"> <ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact].[ext]"/> </target>
當咱們調用該target時,Ivy將會使用咱們的ivy.xml文件作解析而且檢索全部的組件。全部檢索到的組件將會根據它所屬的配置存放在不一樣的文件夾裏。
而後咱們在運行的時候就能夠指定不一樣的路徑做爲classpath來啓動應用,以達到不一樣的運行結果和目的。
咱們應該儘量多的使用configurations,它是Ivy很是重要的概念。使用configurations你能夠將組件根據不一樣的用途分類,當你爲工程編寫ivy文件,而且這些工程是準備給其餘人使用的,用configurations可使使用者只得到他們所須要的,而不須要使用者在它們的依賴列表裏逐個指定。