若已安裝 Ant 與 JDK,無需額外安裝 Ivy,即可在 30 秒內體驗依賴管理。範例位於 src/example/hello-ivy。
<ivy-module version="2.0">
<info organisation="apache" module="hello-ivy"/>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.0"/>
<dependency org="commons-cli" name="commons-cli" rev="1.0"/>
</dependencies>
</ivy-module>
org、name 對應 Maven 的 groupId、artifactId。rev 指定版本。可至 mvnrepository.com 查詢坐標。<project xmlns:ivy="antlib:org.apache.ivy.ant" name="hello-ivy" default="run">
<target name="resolve" description="--> retrieve dependencies with ivy">
<ivy:retrieve />
</target>
</project>
xmlns:ivy="antlib:org.apache.ivy.ant"。<ivy:retrieve/> 無屬性時,自動尋找同目錄 ivy.xml,使用預設設定解析並下載到快取,再複製到專案 lib/ 目錄。在專案目錄執行 ant(預設 target 為 run,依賴 resolve):
resolve:
[ivy:retrieve] :: resolving dependencies :: [ org.apache | hello-ivy | working@... ]
[ivy:retrieve] confs: [default]
[ivy:retrieve] found [ commons-lang | commons-lang | 2.0 ] in public
[ivy:retrieve] found [ commons-cli | commons-cli | 1.0 ] in public
[ivy:retrieve] found [ commons-logging | commons-logging | 1.0 ] in public
[ivy:retrieve] downloading ... commons-lang-2.0.jar ... (165kB)
[ivy:retrieve] downloading ... commons-cli-1.0.jar ... (29kB)
[ivy:retrieve] downloading ... commons-logging-1.0.jar ... (21kB)
[ivy:retrieve] :: resolution report ::
[ivy:retrieve] :: evicted modules: [ commons-lang | 1.0 ] by [ commons-lang | 2.0 ]
| default | 4 | 3 | 0 | 1 || 3 | 3 |
[ivy:retrieve] :: retrieving :: [ org.apache | hello-ivy ]
[ivy:retrieve] 3 artifacts copied, 0 already retrieved
run:
[java] standard message : hello ivy !
[java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy !
commons-cli 依賴 commons-logging,一併下載。commons-lang 有 1.0 與 2.0 兩版本被引入,預設策略保留 2.0(較新),1.0 被排除。${user.home}/.ivy2/cache,再複製到 lib/。resolve:
[ivy:retrieve] :: resolution report ::
| default | 4 | 0 | 0 | 1 || 3 | 0 |
[ivy:retrieve] :: retrieving :: [ org.apache | hello-ivy ]
[ivy:retrieve] 0 artifacts copied, 3 already retrieved
run: ... (瞬間完成)
零下載、零複製,建構極速完成。這展示 Ivy 「快取 ≠ 本地倉庫」的設計:快取可隨時刪除,制品永遠可從遠端倉庫重獲。
執行 ant report 可在 build/ 目錄生成詳細 HTML 依賴報表,視覺化模組關係與衝突處理。