用Apache Ivy實現項目裏的依賴管理

用Apache Ivy實現項目裏的依賴管理html

Apache Ivy是一個管理項目依賴的工具。java

它與Maven Apache Maven 構建管理和項目管理工具已經吸引了 Java 開發人員的注意。Maven 引入了 JAR 文件公共存儲庫的概念,可經過公開的 Web 服務器訪問(稱爲 ibiblio)。Maven 的方法減小了 JAR 文件膨脹的狀況,不會佔用大多數版本控制存儲庫。但使用 Maven 時,它會鼓勵您採用其 「慣例優於配置」 的方法來構建軟件,這會制約您定製構建腳本的靈活性。apache

但問題是Maven過於Heavy,而大部分已有的項目都用Ant作build,因此Ivy是更加合適的選擇。緩存

Ivy 提供了最一致、可重複、易於維護的方法,來管理項目的全部構建依賴項。服務器

用Ivy進行項目管理maven

開始使用 Ivy 很是簡單,只需建立兩個 Ivy 特有的文件,添加一些 Ant 目標便可。Ivy 特有的文件是 ivy.xml 和一個 Ivy 設置文件。ivy.xml 文件中列舉了項目的全部依賴項。ivysettings.xml 文件(能夠隨意爲此文件命名)用於配置從中下載有依賴關係的 JAR 文件的存儲庫。工具

Ivy的安裝 Ivy依賴於Ant,因此須要先安裝Ant,而後下載Ivy,將他的jar文件考到Ant的lib下面,就能夠在Ant裏使用Ivy進行依賴管理了。 下載ivy 2.0 http://ant.apache.org/ivy/download.cgi測試

校內鏡像:http://labs.xiaonei.com/apache-mirror/ant/ivy/2.0.0/apache-ivy-2.0.0-bin-with-deps.zipui

下載好後安裝它,把它解壓到f:/ivy-2.0.0(把此目錄認爲是IVY_HOME),把IVY_HOME/ivy-2.0.0.jar放到 ANT_HOME/lib目錄下。而後命令行入到IVY_HOME/src/example/hello-ivy目錄,運行ant。而後它會下載依賴的全部jar包。 看下hello-ivy的依賴配置:this

  1. <ivy-module version="2.0">

  2. <info organisation="org.apache" module="hello-ivy"/>

  3. <dependencies>

  4. <dependency org="commons-lang" name="commons-lang" rev="2.0"/>

  5. <dependency org="commons-cli" name="commons-cli" rev="1.0"/>

  6. </dependencies>

  7. </ivy-module>

依賴commons-lang-2.0.jar 和 commons-cli-1.0.jar,ivy會自動下載,固然還有這些*.jar所依賴的jar, 如這裏的commons-cli-1.0.jar依賴commons-logging-1.0.jar,不用在ivy.xml文件定義。它們已經在lib 目錄下了。 而後你再一次運行ant,ivy不會再下載這些jar,由於本地有緩存了。 固然也能夠用ant report任務,輸出jar依賴報告,默認在build目錄,org.apache-hello-ivy-default.html。 延伸:默認緩存目錄爲${user.home}/cache。你也能夠改它的默認目錄在運行ant時,設置,如ivy.default.ivy.user.dir=f:/ivy2,因此它會緩存到f:/ivy2/cache

使用Ivy

ivy.xml 在 ivy 中,配置(conf)是比較重要的概念,它對應一組依賴的jar。比較一個編譯期間的conf(compile),它依賴commons-lang。運行期間它還要依賴log4j,能夠定義一個運行期配置(runtime),它擴展compile。配置是能夠擴展的,依次類推,能夠定義一個測試用的jar 依賴配置(test),它擴展runtime。

ivy的jar依賴配置在ivy.xml文件裏定義與說明,相似:

  1. <ivy-module version="1.0">

  2. <info organisation="com.chenlb" module="ivy-hello"/>

  3. <configurations>

  4. <conf name="compile" visibility="private" description="compilation only need jar" />

  5. <conf name="runtime" visibility="private" extends="compile" description="for runtime need jar" />

  6. <conf name="test" visibility="private" extends="runtime" description="for test" />

  7. <conf name="default" visibility="public" extends="runtime" description="default jar" />

  8. </configurations>

  9. <dependencies>

  10. <dependency org="commons-lang" name="commons-lang" rev="2.1" conf="compile->default"/>

  11. <dependency org="log4j" name="log4j" rev="1.2.12" conf="runtime->default"/>

  12. <dependency org="junit" name="junit" rev="3.8.2" conf="test->default"/>

  13. </dependencies>

  14. </ivy-module>

上面定義了,compile、runtime、test、default配置(一個配置對應一個jar依賴集)。compile只依賴 commons-lang-2.1.jar;但runtime還依賴log4j-1.2.12.jar;測試用的還依賴junit-3.8.2.jar。

在Ant裏使用ivy。

加ivy的xmlns。如

  1. <project name="ivy-hello" default="init" xmlns:ivy="antlib:org.apache.ivy.ant">

  2. <!-- ... -->

  3. </project>

下載jar。

  1. <target name="resolve" description="--> retreive dependencies with ivy">

  2. <ivy:retrieve pattern="${ivy.lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
  3. </target>

ivy.lib.dir默認是當前目錄下的lib。[conf]是配置名。[artifact]是jar發佈的名,[revision]是版本號,[ext]是擴展名。

classpath

  1. <path id="build.lib.path">

  2. <fileset dir="${lib.dir}/build" />

  3. </path>

  4. <path id="test.lib.path">

  5. <fileset dir="${lib.dir}/test" />

  6. <pathelement location="${build.java.dir}" />

  7. </path>

能夠在編譯任務用${compile.lib.path}的classpath,test的也一樣。

如今能夠基本運行ant 和 ivy了,運行ant resolve就能夠看到ivy下載相關的jar包。

如何構建本身的Repository

Ivy的例子裏已經包括了一個構建repo的例子,在build-a-ivy-repository裏,主要運行build.xml就能夠構建一個簡單的repo,若是你想用namespace管理一個專業的repo,能夠運行ant maven2-namespace,就會在本地構建一個專業的repo。

Repo-Location/[org]/[name]/ivy-[version].xml e.g. apache/commons-lang/ contains a jar and a definition file, ivy-[version].xml

下面咱們看看ivy-[version].xml裏是什麼內容

<ivy-module version="1.0" xmlns:m="http://ant.apache.org/ivy/maven "> <info organisation="apache" module="commons-lang" revision="1.0" status="release" publication="20051124132021" namespace="maven2"> <description homepage=""> ..... </description> <m:maven.plugins>nullmaven-surefire-plugin null</m:maven.plugins> </info> <configurations> <conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/> <conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/> ..... </configurations> <publications> <artifact name="commons-lang" type="jar" ext="jar" conf="master"/> <artifact name="commons-lang" type="javadoc" ext="jar" conf="javadoc" m:classifier="javadoc"/> </publications> <dependencies> <dependency org="junit" name="junit" rev="3.7" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/> </dependencies> </ivy-module>

其實他和普通的ivy.xml的格式是同樣,只是用於定義jar自己的依賴,只是多了publication對提供的jar進行描述。

IVY的配置 - ivysettings.xml

ivy自己有3中repo的類型:local,shared和public的。

ivy默認的setting:在jar裏org.apache.ivy.core.setting包中 <ivysettings>

<settings defaultResolver="default"/>

<include url="${ivy.default.settings.dir}/ivysettings-public.xml"/>

<include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>

<include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>

<include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>

<include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>

</ivysettings>

你能夠在這裏將public的repo改成你本身的repo

<include url="http://myserver/ivy/myivysettings-public.xml "/>

myivysettings-public.xml

<ivysettings>

<resolvers>

<filesystem name="public">

  <ivy pattern="/path/to/my/public/rep/[organisation]/[module]/ivy-[revision].xml" />

  <artifact pattern="/path/to/my/public/rep/[organisation]/[module]/[artifact]-[revision].[ext]" />

</filesystem>

</resolvers>

</ivysettings>

這樣當resolve是,ivy會先從user local,而後是shared,而後會在你設置的public repo下載jar。

更多的關於Ivy的信息請查閱Apache Ivy的官方doc: http://ant.apache.org/ivy/

相關文章
相關標籤/搜索