2015年11月28日

Gradle(14)–– Dependency Management

目標
在本章我們會著重在如何管理專案相依、解決相互衝突、解析策略並教你如何發佈artifacts
在不同的元件版本下。

歷史相依管理在建構工具是一個很重要的指標,在傳統的ANT建構工具,他無法幫你處理相依
問題,你必須自己將每一個jar名稱和它的位置寫在build.xml。但是在企業應用,有些軟體
會相依其他函式庫,使用ANT的方式,需要花費很多成本去維護相依的問題。後來,Maven
解決這個缺點,ANT整合Ivy也是解決這個問題。在Gradle有自己的相依管理方式,同時它也
支援Ivy和Maven的套件。Gradle主要定義相依關係

  • 相依環境
當我們開發完成的軟體套件,必須要做版本部署並儲存中央版本庫(central repository),讓所有
團隊可以分享使用這些軟體套件。版本命名參考http://semver.org/

  • Gradle相依管理
gradle相依管理定義在dependencies裡面,需要遵循它的規則和定義方式即可。
宣告相依定義:
dependencies
{
    <configuration name> <dependencies>
}

區塊內定義方式:
1. 個別定義
    compile group: 'log4j', name: 'log4j', version: '1.2.16'
2.陣列定義:
   compile 'log4j:log4j:1.2.16','junit:junit:4.10'
3. Closure定義:
   compile ('log4j:log4j:1.2.16') ) {
            // extra configurations
   }

apply plugin: 'java'repositories {
       
mavenCentral()}
dependencies {
        compile group: 'log4j', name: 'log4j', version: '1.2.16'
}

  • 版本庫(Repositories)
建立版本庫是告訴dependencies要去哪裡下載套件,他會檢查群組、名稱和版本
,符合才下載到gradle版本庫中。

1. 內建版本庫
(1) Maven Central repository :  mavenCentral()
(2) Maven JCenter repository:  jcenter()
(3) Maven local Repository :    mavenLocal()
(4) Ivy repository:    
   ivy {
       url "
http://<ivyrepositorylocation>"
       layout "ivy" // valid values are maven, gradle, ivy
   }

(5) Organization repository: 你可以為你的公司定義私人的程式庫。
repositories {
   maven {
      url "
http://private.repository/path"
      credentials {
        username 'guest'
        password '123123'
     }
   }
   ivy { // For Ivy repositories
   url "
http://private.repository/path"
   }
}

(6) Local directory repository : 有時候開發軟體載入相依性,會用到本地系統檔案目錄,可以定義flatDir方法。
repositories {
   flatDir {
       dirs '/localfile/dir1', '/localfile/dir2'
   }
}

  • 相依深入解析
剛剛介紹相依探討,本節介紹如何解析如何
(1) 遞移相依 :
當套件相依另一個套件時,另一個套件有相依其它套件,一層層的相依關係,可以稱為遞移相依,例如
commons-httpclient –> commons-logging –> 其它套件->…。Gradle會處理這些複雜的相依關係,預設
將這些相依下載到最後一層。

(2)排除相依設定

(2.1) 完全排除(Exclude)
有些情況,你不希望gradle處理相依問題,我們可以使用將transitive為off,
範例一、 只下載commons-httpclient套件apply plugin:'java'
repositories {
          mavenCentral()
}

dependencies {
     compile group:'commons-httpclient', name:'commons-httpclient',
     version:'3.1',
transitive: false}

(2.2) 選擇排除(selective)
如果你不想要全部都不檢查相依,只是部分檢查可以使用exclude定義
dependencies{
    compile('commons-httpclient:commons-httpclient:3.1') {
    exclude group:'commons-codec' // exclude by group
     //exclude group:'commons-codec',module:'commons-codec'
    }
}


(3)動態訂義相依版本

(3.1)版本
Gradle抓取版本超過1.0以上的,如果是用'latest.integration只會捉最新的版本。
compile group:'commons-codec',name:'commons-codec', version: '1.+'
compile group:'commons-codec',name:'commons-codec', version:'latest.integration’

(3.2)副檔名
當我們用ext只會下載所定義的副檔名,範例只會下載 .war的檔案下來。
dependencies {
     runtime group: 'org.mywar', name: 'sampleWeb', version: '1.0',
    
ext: 'war'}

(3.3)特定檔案名稱
當我們利用classifier來定義特定下載檔案名稱,例如版本庫有兩個jar檔為assys-1.0-test.jar和assys-1.0-publish.jar
兩種。使用classifier只會下載publish的jar檔下來。
dependencies {
    runtime group: 'org.assys, name: ‘assys’, version: '1.0',  classifier: 'publish', ext:'war'
}

  • 報表
我們可以顯示本專案各任務的所有相依關係,例如: compile、testCompile等等

build.gradle
apply plugin: 'java'
version=1.0
repositories {
    mavenCentral()
}
dependencies {
     testCompile 'junit:junit:4+'
    compile group: 'log4j', name: 'log4j', version: '1.2.16'
    compile 'commons-httpclient:commons-httpclient:3.1'
    compile 'dom4j:dom4j:1.6.1'
}
C:\Users\bryan\workspace\ex1>gradle -b build.gradle dependencies
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
archives - Configuration for archive artifacts.
No dependencies
compile - Compile classpath for source set 'main'.
Download https://repo1.maven.org/maven2/commons-httpclient/commons-httpclient/3.
1/commons-httpclient-3.1.pom
Download https://repo1.maven.org/maven2/dom4j/dom4j/1.6.1/dom4j-1.6.1.pom
Download https://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.4/co
mmons-logging-1.0.4.pom
Download https://repo1.maven.org/maven2/commons-codec/commons-codec/1.2/commons-
codec-1.2.pom
Download https://repo1.maven.org/maven2/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2
.pom
+--- log4j:log4j:1.2.16
+--- commons-httpclient:commons-httpclient:3.1
|    +--- commons-logging:commons-logging:1.0.4
|    \--- commons-codec:commons-codec:1.2
\--- dom4j:dom4j:1.6.1
     \--- xml-apis:xml-apis:1.0.b2
default - Configuration for default artifacts.
+--- log4j:log4j:1.2.16
+--- commons-httpclient:commons-httpclient:3.1
|    +--- commons-logging:commons-logging:1.0.4
|    \--- commons-codec:commons-codec:1.2
\--- dom4j:dom4j:1.6.1
     \--- xml-apis:xml-apis:1.0.b2
runtime - Runtime classpath for source set 'main'.
+--- log4j:log4j:1.2.16
+--- commons-httpclient:commons-httpclient:3.1
|    +--- commons-logging:commons-logging:1.0.4
|    \--- commons-codec:commons-codec:1.2
\--- dom4j:dom4j:1.6.1
     \--- xml-apis:xml-apis:1.0.b2
testCompile - Compile classpath for source set 'test'.
+--- log4j:log4j:1.2.16
+--- commons-httpclient:commons-httpclient:3.1
|    +--- commons-logging:commons-logging:1.0.4
|    \--- commons-codec:commons-codec:1.2
+--- dom4j:dom4j:1.6.1
|    \--- xml-apis:xml-apis:1.0.b2
\--- junit:junit:4+ -> 4.12
     \--- org.hamcrest:hamcrest-core:1.3
testRuntime - Runtime classpath for source set 'test'.
+--- log4j:log4j:1.2.16
+--- commons-httpclient:commons-httpclient:3.1
|    +--- commons-logging:commons-logging:1.0.4
|    \--- commons-codec:commons-codec:1.2
+--- dom4j:dom4j:1.6.1
|    \--- xml-apis:xml-apis:1.0.b2
\--- junit:junit:4+ -> 4.12
     \--- org.hamcrest:hamcrest-core:1.3
BUILD SUCCESSFUL
Total time: 20.567 secs

沒有留言:

張貼留言