Sky's blog

          我和我追逐的夢(mèng)

          常用鏈接

          統(tǒng)計(jì)

          其他鏈接

          友情鏈接

          最新評(píng)論

          ivy教程(8)-使用ivy模塊配置

              這個(gè)教程介紹ivy文件中的模塊配置的使用。ivy模塊配置事實(shí)上是一個(gè)非常重要的概念。某些人甚至告訴我使用ivy而不用ivy配置就像吃乳酪而不動(dòng)就在你旁邊的Chateau Margaux 1976!

              嚴(yán)肅的說(shuō),ivy中的配置可以更好的理解為你的模塊的視圖,你將可以看到在這里他們將如何被高效地使用。

              關(guān)于配置的參考文件可以在這里這里找到。

              1) Introduction

              源文件在這里 src/example/configurations/multi-projects.
              我們有兩個(gè)項(xiàng)目:

          - filter-framework 是一個(gè)類庫(kù),定義一個(gè)api來(lái)過(guò)濾字符串?dāng)?shù)組,這個(gè)api有兩個(gè)實(shí)現(xiàn).
          - myapp 是一個(gè)使用filter-framework的非常小的應(yīng)用.

              這個(gè)類庫(kù)產(chǎn)生3個(gè)制品:
          - api的jar
          - 一個(gè)沒(méi)有外部依賴的實(shí)現(xiàn)的jar
          - 另一個(gè)需要commons-collections來(lái)執(zhí)行的實(shí)現(xiàn)jar

              應(yīng)用僅僅需要api來(lái)編譯,在運(yùn)行時(shí)可以使用兩個(gè)實(shí)現(xiàn)中的任意一個(gè)。

              2) 類庫(kù)項(xiàng)目

              在這個(gè)教程中我們定義的第一個(gè)項(xiàng)目是filter-framework.
              為了得到一個(gè)良好處理的制品發(fā)布定義,我們定義配置來(lái)計(jì)劃讓其他人使用我們的類庫(kù)的使用方式。

              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. 解釋

              如你所見(jiàn),我們定義了3個(gè)公共配置和一個(gè)私有配置(為測(cè)試定義junit依賴)。
              2個(gè)實(shí)現(xiàn)配置homemade-impl, cc-impl 繼承自api配置,因此在api中定義的制品在它的繼承配置中同樣是需要的。
              在publications標(biāo)簽中,我們定義了我們要產(chǎn)生的制品(在這里它們是jar)并給他們使用了配置。
              后面當(dāng)其他模塊要使用我們的類庫(kù)時(shí),他們將有非常靈活的方式來(lái)定義他們需要的東西。

              3. 在實(shí)戰(zhàn)中檢驗(yàn)
              The library project is build using ant. Open a shell in the root directory of the project and type ant.
              類庫(kù)項(xiàng)目使用ant來(lái)構(gòu)建。在項(xiàng)目的根目錄下打開(kāi)一個(gè)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的默認(rèn)target是publish。
              這個(gè)target使用ivy發(fā)布我們的類庫(kù)到本地倉(cāng)庫(kù)。
              因?yàn)槲覀儧](méi)有指定任何倉(cāng)庫(kù)路徑,因此使用默認(rèn)倉(cāng)庫(kù)。({home.dir}/.ivy2/local/org.apache/filter-framework/)現(xiàn)在我們準(zhǔn)備好了使用我們的類庫(kù)。

              3) 應(yīng)用項(xiàng)目

              現(xiàn)在我們已經(jīng)完成了我們美妙的類庫(kù),我們想用它!
              這個(gè)教程帶來(lái)一個(gè)名為myapp的示例應(yīng)用。
           
              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. 解釋

              我們創(chuàng)建了3個(gè)配置來(lái)定義我們想使用應(yīng)用的方式。
              build配置定義了編譯時(shí)的依賴,而這個(gè)只需要來(lái)自filter-framework的api conf。
              其他配置定義了運(yùn)行時(shí)依賴。一個(gè)將僅僅使用"home-made"的jars,而另一個(gè)將使用外部的jars。

              我們同樣定義了對(duì)于上面類庫(kù)的依賴。
              在依賴中我們使用配置映射來(lái)匹配我們和類庫(kù)的配置。
              你可以在這里找到更多的關(guān)于配置映射的信息。

             1. build->api : 這里我們告訴ivy,我們的build配置依賴于依賴的api配置。
             2. noexternaljar->homemade-impl : 這里我們告訴ivy,我們的noexternaljar 配置依賴于依賴的homemade-impl配置。
             3. withexternaljar->cc-impl : 這里我們告訴ivy,我們的的withexternaljar 配置依賴于依賴的cc-impl配置。

              注意我們從不定義在每個(gè)配置中需要的任何依賴制品:依賴模塊文件將定義發(fā)布的制品和將被哪個(gè)配置使用。

              在ant的build.xm文件中我們定義解析的target如下:

          <target name="resolve" description="--> retreive dependencies with ivy">
              
          <ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact].[ext]"/>
          </target>

              當(dāng)我們調(diào)用這個(gè)target時(shí),ivy將使用我們的在root文件夾中的ivy.xml來(lái)做解析并隨后獲取所有的制品。制品被獲取并分別保存在和他們所屬的配置對(duì)應(yīng)目錄下。在調(diào)用這個(gè)target后你的lib目錄將看起來(lái)像這樣:

           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

              如你所見(jiàn)對(duì)于每個(gè)配置我們現(xiàn)在都有了一個(gè)jar的集合。

              讓我們?cè)囋噯?dòng)我們的app。

              3. 在實(shí)戰(zhàn)中檢驗(yàn)

              使用ant來(lái)運(yùn)行應(yīng)用。
              默認(rèn)ant target是run-cc,將使用apache commons-collections實(shí)現(xiàn)來(lái)啟動(dòng)應(yīng)用。

          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來(lái)啟動(dòng)應(yīng)用時(shí)很直接的。
              鍵入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

              很好,我們得到了同樣的結(jié)果,但是我們可以看到實(shí)現(xiàn)的類是不一樣的。

              4) 結(jié)論
              你應(yīng)該盡可能多的使用配置。
              配置是ivy中非常重要的概念。他們?nèi)菰S你按照意愿來(lái)分組制品。
              當(dāng)你書(shū)寫(xiě)假定要被重用的項(xiàng)目的ivy文件時(shí),使用配置來(lái)容許人們只獲取他們需要的東西,而不需要通過(guò)在依賴塊中使用制品標(biāo)簽來(lái)手工指定。


          posted on 2009-10-04 10:15 sky ao 閱讀(4945) 評(píng)論(3)  編輯  收藏 所屬分類: project building

          評(píng)論

          # re: ivy教程(8)-使用ivy模塊配置 2009-10-04 11:16 伊莎貝兒

          配置是ivy中非常重要的概念  回復(fù)  更多評(píng)論   

          # re: ivy教程(8)-使用ivy模塊配置 2009-10-04 16:09 羅萊家紡

          要通過(guò)在依賴塊中使用制品標(biāo)簽來(lái)手工指定  回復(fù)  更多評(píng)論   

          # re: ivy教程(8)-使用ivy模塊配置 2012-11-09 10:37 udaye

          你親da爺  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 福海县| 电白县| 松阳县| 南岸区| 凯里市| 肥东县| 甘谷县| 台江县| 驻马店市| 奉新县| 罗江县| 桃园市| 伊金霍洛旗| 台湾省| 田东县| 元氏县| 晋江市| 柳州市| 甘谷县| 云龙县| 泽库县| 盐边县| 芦山县| 凤冈县| 安阳市| 英德市| 耒阳市| 南陵县| 关岭| 大邑县| 吴江市| 精河县| 虞城县| 克什克腾旗| 新竹市| 广平县| 莱州市| 临颍县| 遵义县| 伊吾县| 布尔津县|