在上一個(gè)教程中,你已經(jīng)看到如何處理兩個(gè)簡單項(xiàng)目之間的依賴。
這個(gè)教程將引導(dǎo)你完成在一個(gè)更加復(fù)雜的環(huán)境下的ivy使用。這個(gè)教程的所有源文件在ivy發(fā)行包的src/example/multi-project下可以得到。
1) 上下文
這里是這個(gè)教程涉及到得項(xiàng)目的概況:
* version 幫助通過版本列表來標(biāo)識模塊
* list 獲取目錄下的文件列表(遞歸地)
* size 獲取目錄下的所有文件或者一個(gè)文件集合的總大小
* find 在一個(gè)給定目錄或者文件列表之間查找匹配給定名稱的文件
* sizewhere 給出在一個(gè)目錄下配置名稱的文件的總大小
* console 通過簡單的控制臺應(yīng)用提供入口給所有其他模塊特性
可以肯定的是這不是旨在展示如何開發(fā)一個(gè)復(fù)雜的應(yīng)用或者提供高級算法的指示。
而是給出一個(gè)簡單的理解,關(guān)于ant + ivy如何使用來開發(fā)一個(gè)被分割為多個(gè)模塊的應(yīng)用。
現(xiàn)在,這是這些模塊如何關(guān)聯(lián)到其他模塊:
黃色的模塊是在這個(gè)教程中要講述的模塊,而藍(lán)色的模塊是外部依賴(我們將在這個(gè)教程中看到如何生成這個(gè)圖形)。
如你所見,我們在這里有一個(gè)優(yōu)美關(guān)聯(lián)的彼此相互依賴的模塊,每個(gè)都依賴其他的最新版本。
2) 示例文件
這個(gè)教程的源文件可以在ivy發(fā)行包的src/example/multi-project下找到。在這個(gè)目錄中,你將發(fā)現(xiàn)下面的文件:
* build.xml
這是根構(gòu)建文件,將被所有模塊用來調(diào)用target,按照他們的依賴的順序(例如,確保在一個(gè)模塊的構(gòu)建通常在任何依賴它的模塊之
前)
* common
o common.xml
每個(gè)項(xiàng)目的build.xml都會導(dǎo)入的通用構(gòu)建文件。這個(gè)構(gòu)建定義了被所有項(xiàng)目使用的target。
o build.properties
對所有項(xiàng)目通用的一些屬性
* projects
包含所有模塊,每個(gè)模塊一個(gè)目錄,每個(gè)目錄下
o ivy.xml
模塊的ivy文件,描述它的對其他模塊和/或外部模塊的依賴。
例如:
<ivy-module version="1.0">
<info
organisation="org.apache.ivy.example"
module="find"
status="integration"/>
<configurations>
<conf name="core"/>
<conf name="standalone" extends="core"/>
</configurations>
<publications>
<artifact name="find" type="jar" conf="core" />
</publications>
<dependencies>
<dependency name="version" rev="latest.integration" conf="core->default" />
<dependency name="list" rev="latest.integration" conf="core" />
<dependency org="commons-collections" name="commons-collections" rev="3.1" conf="core->default" />
<dependency org="commons-cli" name="commons-cli" rev="1.0" conf="standalone->default" />
</dependencies>
</ivy-module>
o build.xml
項(xiàng)目的構(gòu)建文件,主要由一個(gè)common構(gòu)建文件和一個(gè)項(xiàng)目特殊屬性文件的導(dǎo)入組成:
<project name="find" default="compile">
<property file="build.properties"/>
<import file="${common.dir}/common.xml"/>
</project>
o build.properties
模塊特殊屬性 + 查找通用構(gòu)建文件的屬性
projects.dir = ${basedir}/..
wkspace.dir = ${projects.dir}/..
common.dir = ${wkspace.dir}/common
o src
源文件目錄,有所有java源文件
注意這里不展示通常的軟件開發(fā)的良好實(shí)踐,尤其是你將發(fā)現(xiàn)在這些例子中沒有任何單元測試,即使我們認(rèn)為單元測試非常重要。但是這不是這個(gè)教程的目標(biāo)。
現(xiàn)在你對結(jié)構(gòu)有一點(diǎn)更多的了解了,讓我們看一下這個(gè)教程最重要的部分:通用構(gòu)建文件。實(shí)際上,你已經(jīng)看到,所有模塊的構(gòu)建文件都僅僅是導(dǎo)入這個(gè)通用構(gòu)建文件,并在他們的ivy文件(你應(yīng)該開始熟悉)中定義他們的依賴。
因此,這里是這個(gè)通用構(gòu)建文件的一些方面:
1. ivy 設(shè)置
<!-- setup ivy default configuration with some custom info -->
<property name="ivy.local.default.root" value="${repository.dir}/local"/>
<property name="ivy.shared.default.root" value="${repository.dir}/shared"/>
<!-- here is how we would have configured ivy if we had our own ivysettings file
<ivy:settings file="${common.dir}/ivysettings.xml" id="ivy.instance" />
-->
這個(gè)聲明只為ivy配置了兩個(gè)屬性: 本地倉庫的位置和共享倉庫的位置。這是僅為這里進(jìn)行的配置,因?yàn)閕vy默認(rèn)是配置為在團(tuán)隊(duì)環(huán)境下工作(關(guān)于這個(gè)的詳情請看默認(rèn)配置教程)。可以肯定的是在實(shí)際環(huán)境中共享倉庫位置將會是在團(tuán)隊(duì)共享目錄(或者在一個(gè)更加復(fù)雜的倉庫中,再次查看默認(rèn)設(shè)置教程來了解如何使用實(shí)際上不同的東西)。你可以從注釋中看到如果默認(rèn)設(shè)置不符合你的目標(biāo)時(shí)如何進(jìn)行設(shè)置。
2. 解析依賴
<target name="resolve" depends="clean-lib, load-ivy" description="--> resolve and retrieve dependencies with ivy">
<mkdir dir="${lib.dir}"/> <!-- not usually necessary, ivy creates the directory IF there are dependencies -->
<!-- the call to resolve is not mandatory, retrieve makes an implicit call if we don't -->
<ivy:resolve file="${ivy.file}"/>
<ivy:retrieve pattern="${lib.dir}/[artifact].[ext]" />
</target>
你應(yīng)該開始熟悉這種ivy的使用方法。我們顯式調(diào)用resolve來使用ivy文件配置(默認(rèn)就足夠了),然后調(diào)用retrieve來從緩存中復(fù)制解析的依賴制品到本地lib目錄(不帶版本號),在IDE中這樣使用非常簡單,當(dāng)制品版本更改時(shí)IDE配置不會改變。
3. ivy-new-version
<target name="ivy-new-version" depends="load-ivy" unless="ivy.new.revision">
<!-- default module version prefix value -->
<property name="module.version.prefix" value="${module.version.target}-dev-b" />
<!-- asks to ivy an available version number -->
<ivy:info file="${ivy.file}" />
<ivy:buildnumber
organisation="${ivy.organisation}" module="${ivy.module}"
revision="${module.version.prefix}" defaultBuildNumber="1" revSep=""/>
</target>
這個(gè)target使用ivy來查找模塊的新版本。為了獲取關(guān)于我們正在處理的模塊的詳情,我們直接使用在ivy文件中通過ivy:info任務(wù)找到的信息。然后buildnumber 任務(wù)用來獲取一個(gè)新的版本,基于我們通過屬性設(shè)置前綴,默認(rèn)它將會是1.0-dev-b(在通用構(gòu)建屬性文件中看一下module.version.target的默認(rèn)值)。每個(gè)被這個(gè)通用構(gòu)建文件構(gòu)建的模塊都可以很容易的通過在它的模塊特有的build.properties中設(shè)置一個(gè)不同的module.version.target,或者設(shè)置覆蓋module.version.prefix.來覆蓋這個(gè)。為了獲取新的修訂版本,ivy掃描倉庫來獲取最新的帶有給定前綴的可用版本,并在這個(gè)版本上增加1.
4. publish
<target name="publish" depends="clean-build, jar" description="--> publish this project in the ivy repository">
<ivy:publish artifactspattern="${build.dir}/[artifact].[ext]"
resolver="shared"
pubrevision="${version}"
status="release"
/>
<echo message="project ${ant.project.name} released with version ${version}" />
</target>
這個(gè)target在共享倉庫中發(fā)布模塊,使用在version屬性中找到的修訂版本,這是在其他target中設(shè)置的(在上面我們已經(jīng)看到是基于ivy-new-version)。當(dāng)模塊到達(dá)一個(gè)特定的里程碑時(shí),或者任何你想團(tuán)隊(duì)可以從模塊的一個(gè)新版本中獲益的時(shí)它可以被使用。
5. publish-local
<target name="publish-local" depends="local-version, jar" description="--> publish this project in the local ivy
repository">
<ivy:publish artifactspattern="${build.dir}/[artifact].[ext]"
resolver="local"
pubrevision="${version}"
pubdate="${now}"
status="integration"
forcedeliver="true"
/>
<echo message="project ${ant.project.name} published locally with version ${version}" />
</target>
這個(gè)和publish任務(wù)非常相像,除了這個(gè)是在本地倉庫中發(fā)布修訂版本,這僅僅在你的環(huán)境下使用不會打擾團(tuán)隊(duì)。當(dāng)你在一個(gè)模塊中修改一些東西并且想在其他模塊中從這些修改中獲益,你可以簡單的調(diào)用在這個(gè)模塊中調(diào)研用publish-local,然后你的其他模塊的下一次構(gòu)建將自動得到這個(gè)本地版本。
6. clean-local
<target name="clean-local" description="--> cleans the local repository for the current module">
<delete dir="${ivy.local.default.root}/${ant.project.name}"/>
</target>
這個(gè)target在當(dāng)你不想再使用你的本地版本時(shí)使用,例如當(dāng)你發(fā)型一個(gè)新的版本到整個(gè)團(tuán)隊(duì)時(shí),或者拋棄你的本地修改并想從團(tuán)隊(duì)的新版本中獲益時(shí)。
7. report
<target name="report" depends="resolve" description="--> generates a report of dependencies">
<ivy:report todir="${build.dir}"/>
</target>
同時(shí)生成html報(bào)告和圖形報(bào)告。
例如,為了生成類似這個(gè)教程開頭展示的那樣的圖形,你僅僅需要遵循這里給出的說明,graphml文件你將在這里找到
projects/console/build/
在項(xiàng)目的控制臺調(diào)用report之后,就這樣,你得到了所有你的應(yīng)用的依賴的清晰的概況。
3) Playing with the projects
你可以使用正規(guī)ant命令來玩這個(gè)教程。在這個(gè)教程的基礎(chǔ)目錄(src/example/multi-project)下開始,運(yùn)行ant -p:
Buildfile: build.xml
Main targets:
clean clean tutorial: delete repository, ivy cache, and all projects
clean-all clean all projects
publish-all compile, jar and publish all projects in the right order
這里給出你可以在這里做什么的注意。為了確認(rèn)所有你的模塊在你的倉庫中都至少發(fā)布了一個(gè)版本(被依賴于其他模塊的模塊構(gòu)建要求),你可以運(yùn)行ant publish-all (
這里是示例的日志)。
你將可以看到ivy在所有模塊上調(diào)用publish target,按照依賴的順序,因此被依賴的總是在依賴它的前面構(gòu)建和發(fā)布。隨意修改一個(gè)模塊的源文件(例如修改一個(gè)方法名稱)和使用這個(gè)方法的模塊,然后調(diào)用publish-all看被依賴的是如何首先編譯,發(fā)布,然后依賴它的模塊可以得到它并成功編譯。
然后你可以進(jìn)入示例項(xiàng)目目錄的其中一個(gè)(如 projects/find for instance),并運(yùn)行ant -p:
Buildfile: build.xml
Main targets:
clean --> clean the project
clean-build --> clean the project built files
clean-lib --> clean the project libraries directory (dependencies)
clean-local --> cleans the local repository for the current module
compile --> compile the project
jar --> make a jar file for this project
publish --> publish this project in the ivy repository
publish-local --> publish this project in the local ivy repository
report --> generates a report of dependencies
resolve --> resolve and retrieve dependencies with ivy
run --> compile and run the project
Default target: compile
你可以看到可用的target,感謝common.xml構(gòu)建文件的導(dǎo)入。通過調(diào)用resolve, publish玩這個(gè)項(xiàng)目,并且當(dāng)你在其他項(xiàng)目中同樣做后
看發(fā)生了什么。舉例來說一個(gè)有趣的事情是修改項(xiàng)目的依賴:如果現(xiàn)在模塊版本依賴于一個(gè)新的通用類庫,你將看到一旦這個(gè)版本的項(xiàng)目被發(fā)布,所有其他依賴于版本的項(xiàng)目都將得到這個(gè)類庫作為他們間接依賴的一部分。非常簡單!如果一個(gè)項(xiàng)目引入一些新變化導(dǎo)致依賴于它的項(xiàng)目不穩(wěn)定,你可以非常簡單的修改依賴它的項(xiàng)目的依賴,從latest.integration 到一個(gè)固定的穩(wěn)定版本(可能是這個(gè)修改之前的最后版本)。現(xiàn)在保持你的模塊可控式非常容易的!
你將非常熟悉使用ivy進(jìn)行多項(xiàng)目開發(fā),我們希望你能欣賞它的能力和彈性!這些教程僅僅是你的ivy旅程的開始,游覽
參考文檔來學(xué)習(xí)關(guān)于這些特性的更多內(nèi)容,加入
郵件列表來分享你的經(jīng)驗(yàn)并在社區(qū)中提問。瀏覽源文件,打開jira問題,提交補(bǔ)丁,加入并幫助ivy成為最好的依賴管理工具!