用Apache Ivy实现项目里的依赖管理
Apache Ivy是一個管理項目依賴的工具。
?
它與Maven?
Apache Maven 構(gòu)建管理和項目管理工具已經(jīng)吸引了 Java 開發(fā)人員的注意。Maven 引入了 JAR 文件公共存儲庫的概念,可通過公開的 Web 服務(wù)器訪問(稱為 ibiblio)。Maven 的方法減少了 JAR 文件膨脹的情況,不會占用大多數(shù)版本控制存儲庫。但使用 Maven 時,它會鼓勵您采用其 “慣例優(yōu)于配置” 的方法來構(gòu)建軟件,這會制約您定制構(gòu)建腳本的靈活性。
?
但問題是Maven過于Heavy,而大部分已有的項目都用Ant做build,所以Ivy是更加合適的選擇。
?
Ivy 提供了最一致、可重復(fù)、易于維護的方法,來管理項目的所有構(gòu)建依賴項。
?
用Ivy進行項目管理
?
開始使用 Ivy 非常簡單,只需創(chuàng)建兩個 Ivy 特有的文件,添加一些 Ant 目標即可。Ivy 特有的文件是 ivy.xml 和一個 Ivy 設(shè)置文件。ivy.xml 文件中列舉了項目的所有依賴項。ivysettings.xml 文件(可以隨意為此文件命名)用于配置從中下載有依賴關(guān)系的 JAR 文件的存儲庫。
?
Ivy的安裝
Ivy依賴于Ant,所以需要先安裝Ant,然后下載Ivy,將他的jar文件考到Ant的lib下面,就可以在Ant里使用Ivy進行依賴管理了。
下載ivy 2.0
http://ant.apache.org/ivy/download.cgi
?
校內(nèi)鏡像:http://labs.xiaonei.com/apache-mirror/ant/ivy/2.0.0/apache-ivy-2.0.0-bin-with-deps.zip
?
?
下載好后安裝它,把它解壓到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的依賴配置:
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文件定義。它們已經(jīng)在lib 目錄下了。
然后你再一次運行ant,ivy不會再下載這些jar,因為本地有緩存了。
當然也可以用ant report任務(wù),輸出jar依賴報告,默認在build目錄,org.apache-hello-ivy-default.html。
延伸:默認緩存目錄為${user.home}/cache。你也可以改它的默認目錄在運行ant時,設(shè)置,如ivy.default.ivy.user.dir=f:/ivy2,所以它會緩存到f:/ivy2/cache
?
使用Ivy
?
ivy.xml
在 ivy 中,配置(conf)是比較重要的概念,它對應(yīng)一組依賴的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.?
4. <configurations>?
5. <conf name="compile" visibility="private" description="compilation only need jar" />?
6. <conf name="runtime" visibility="private" extends="compile" description="for runtime need jar" />?
7. <conf name="test" visibility="private" extends="runtime" description="for test" />?
8. <conf name="default" visibility="public" extends="runtime" description="default jar" />?
9. </configurations>?
10. <dependencies>?
11. <dependency org="commons-lang" name="commons-lang" rev="2.1" conf="compile->default"/>?
12. <dependency org="log4j" name="log4j" rev="1.2.12" conf="runtime->default"/>?
13.?
14. <dependency org="junit" name="junit" rev="3.8.2" conf="test->default"/>?
15. </dependencies>?
16. </ivy-module>
?
上面定義了,compile、runtime、test、default配置(一個配置對應(yīng)一個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發(fā)布的名,[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>
?
可以在編譯任務(wù)用${compile.lib.path}的classpath,test的也同樣。
?
現(xiàn)在可以基本運行ant 和 ivy了,運行ant resolve就可以看到ivy下載相關(guān)的jar包。
?
如何構(gòu)建自己的Repository
?
Ivy的例子里已經(jīng)包括了一個構(gòu)建repo的例子,在build-a-ivy-repository里,主要運行build.xml就可以構(gòu)建一個簡單的repo,如果你想用namespace管理一個專業(yè)的repo,可以運行ant maven2-namespace,就會在本地構(gòu)建一個專業(yè)的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里是什么內(nèi)容
?
<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,然后會在你設(shè)置的public repo下載jar。更多的關(guān)于Ivy的信息請查閱Apache Ivy的官方doc: http://ant.apache.org/ivy/總結(jié)
以上是生活随笔為你收集整理的用Apache Ivy实现项目里的依赖管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Nutch2.2.1基础教程之2.1】
- 下一篇: 单机/伪分布式Hadoop2.4.1安装