Ivy: 搭建本地倉庫

基本上, 搭建一個本地的ivy倉庫, 包含兩件獨立的事情: 搭建倉庫本身, 和配置如何使用這個倉庫

 

倉庫本身

Ivy的Repository是由一個個Module組成的, Module則包含自身的artifacts(通常是jar文件)和描述文件.

Module的描述文件是ivy的核心, 裏面記錄了這個module包含哪些artifacts和這個module對其它module的依賴. 這樣就能順藤摸瓜, 牽出所有的依賴來

<ivy-module version="1.0">

<info organisation="your.company" module="your.project" />

<configurations>

<conf name="release" />

<conf name="testing" extends="release" />

</configurations>

<publications>

<artifact name="common" />

<artifact name="client" />

<artifact name="server" />

</publications>

<dependencies defaultconf="release->default">

<dependency name="ant" rev="1.7.0" />

<dependency name="antlr" rev="2.7.6" />

<dependency name="xstream" rev="1.2.2" />

</dependencies>

</ivy-module>

倉庫在文件系統上的結構, 可以由你自己決定, 只要在使用這個倉庫時, 把它的佈局信息用配置文件描述出來告訴使用者就可以了

 

如何使用這個倉庫

基本上, 我們藉助 Ant 來使用ivy, 那麼我們需要告訴 Ant 一些repository相關的信息 : Where is the local repository, and How it looks like

<property name="ivy.local.default.root" location="/your/local/ivy/repository/folder" />

<ivy:settings id="ivy.instance" file="${ivy.local.default.root}/ivy.repository.settings.xml" />

那個ivy.repository.settings.xml就是來描述repository的佈局的:

<ivysettings>

<settings defaultResolver="local" />

<resolvers>

<filesystem name="local" checkmodified="true">

<artifact pattern="${ivy.local.default.root}/[module]/[artifact].jar" />

<artifact pattern="${ivy.local.default.root}/[module]/[artifact]-[revision].jar" />

<ivy pattern="${ivy.local.default.root}/[module]/[module]-dependencies.xml" />

</filesystem>

</resolvers>

</ivysettings>

然後你告訴 Ant 你的項目的依賴和artifacts, Ant就可以幫你獲得依賴和發佈artifacts了

<property name="ivy.dep.file" location="${basedir}/my-module-dependencies.xml" />

<ivy:retrieve />

<!-- retrieve dependencies first, then compile, package, ... then publish-->

<ivy:publish resolver="local" pubrevision="1.0" overwrite="true" forcedeliver="true">

<artifacts pattern="${build.dir}/dist/[artifact].[ext]" />

<artifacts pattern="${basedir}/my-module-dependencies.xml" />

</ivy:publish>

那個my-module-dependencies.xml就是你的項目的依賴描述符, 事實上ivy也將它看作一個module,和repository裏的module一視同仁. 參考最開始的例子.

 

Ivy 如何解決我們的問題

Q: 我的項目在開發環境和產品環境有不同的依賴, 怎麼辦?

A: Ivy 提供了"configuration" 的概念. 在那個my-module-dependencies.xml中, 你可以爲你的module定義development和product兩種配置, 可以指定每個artifact隸屬於哪個配置, 指定每個dependency屬於哪個配置. 注意這是一個遞歸定義, 因爲你依賴的module也有可能定義了多個配置, 你需要指定你依賴於依賴的哪個配置,所以這是一個映射. 比如你的產品在運行環境中依賴於spring的產品環境:

<ivy-module version="1.0">

<info organisation="your.company" module="your.project" />

<configurations>

<conf name="product" />

<conf name="development" />

</configurations>

<dependencies defaultconf="product->default">

<dependency name="spring" rev="1.7.0" conf="release->product" />

<dependency name="antlr" rev="2.7.6" conf="development->debug" />

<dependency name="junit" rev="4.4" conf="development->release" />

</dependencies>

</ivy-module>

ivy 的各個Ant task允許你指定在哪個 configuration 上操作; 如爲product配置取得所有依賴:

    <ivy:retrieve conf="product" />

 

Q: 我的項目在開發環境和產品環境有太多相同的依賴, 事實上開發環境包含產品環境所有的依賴, 額外再加上junit,jmock等; 如何消除重複的依賴描述呢?

A: Ivy 提供了配置之間 "extends" , 也就是"繼承"的概念, 你可以讓開發環境的配置繼承自產品環境的配置, 這樣就可以複用產品環境的配置

<configurations>

<conf name="product" />

<conf name="development" extends="product" />

</configurations>

 

Q: 缺省ivy總是從緩存中讀取配置, 這樣我的依賴配置更新後卻得不到反映; 怎麼禁止從cache中讀取配置?

A: <filesystem name="local" checkmodified="true">

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章