首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Ivy 教程 (2):快速入門

     編輯:Java 相關知識

最簡單的 Ivy 體驗:零設定解析 Maven Central

若已安裝 Ant 與 JDK,無需額外安裝 Ivy,即可在 30 秒內體驗依賴管理。範例位於 src/example/hello-ivy。

1. ivy.xml:宣告依賴

<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 查詢坐標。

2. build.xml:一鍵解析與取回

<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>
  • 宣告 Ivy Ant 命名空間:xmlns:ivy="antlib:org.apache.ivy.ant"。
  • <ivy:retrieve/> 無屬性時,自動尋找同目錄 ivy.xml,使用預設設定解析並下載到快取,再複製到專案 lib/ 目錄。

3. 執行與觀察

在專案目錄執行 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 !
  • 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 「快取 ≠ 本地倉庫」的設計:快取可隨時刪除,制品永遠可從遠端倉庫重獲。

4. 進階:生成依賴報表

執行 ant report 可在 build/ 目錄生成詳細 HTML 依賴報表,視覺化模組關係與衝突處理。

下一步

  • 學習模組配置(教程 8),實現編譯/執行時依賴隔離。
  • 探索設定檔(教程 3-7),建構企業級儲存庫。
  • 參考最佳實踐,打造乾淨健壯的建構系統。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved