Gradle vs Maven

Gradle是一個(gè)構(gòu)建工具浙宜,構(gòu)建包括依賴管理官辽、編譯、打包等過(guò)程粟瞬⊥停總結(jié)了Gradle和maven之間的主要區(qū)別:靈活性、用戶體驗(yàn)和依賴關(guān)系管理亩钟。

依賴管理

1.申明簡(jiǎn)潔

<!-- pom.xml scope-->
<dependencies>
<dependency>
    <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
// build.gradle  configuration
dependencies {
     testCompile 'junit:junit:4.12'
}

2.依賴顆粒度

Maven When available Leaks into consumers’ compile time Leaks into consumers’ runtime Included in Artifact
compile compile time, runtime乓梨, test鳖轰, compile time, test runtime yes yes yes
provided compile time, runtime test 扶镀,compile time, test runtime no no no
runtime runtime test, runtime no yes yes
test test compile, test runtime no no no
system
import
Gradle When available Leaks into consumers’ compile time Leaks into consumers’ runtime Included in Artifact
implementation compile time, runtime test compile time, test runtime no yes yes
api compile time蕴侣, runtime test compile time, test runtime yes no no
compileOnly compile time no no yes
runtimeOnly runtime no yes yes
testImplementation test compile time, test runtime no no no
testCompileOnly test compile time no no no
testRuntimeOnly test runtime no no no

3.靈活性

3.1以一個(gè)常見(jiàn)的JAVA日志庫(kù)沖突問(wèn)題為例
image-20201113134242866.png

<!-- pom.xml  -->
<dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>2.1.0</version>
      <exclusions>
          <exclusion>
             <artifactId>log4j-slf4j-impl</artifactId>
              <groupId>org.apache.logging.log4j</groupId>
          </exclusion>
     </exclusions>
</dependency>
// build.gradle

// 全局排除依賴
configurations {
    // 支持通過(guò)group、module排除臭觉,可以同時(shí)使用
    all*.exclude group: 'commons-logging', module: 'commons-logging' // common-logging
    all*.exclude group: 'log4j', module: 'log4j' // log4j
    all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-core' // slf4j -> log4j2
    all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl' // log4j2
    all*.exclude group: 'org.slf4j', module: 'slf4j-jdk14' // slf4j -> jdk-logging
    all*.exclude group: 'org.slf4j', module: 'slf4j-jcl' // slf4j -> common-logging
    all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12' // slf4j -> log4j
}
// 引入依賴 所有類型的日志庫(kù)都橋接到 slf4j 實(shí)現(xiàn)庫(kù)為logback
dependencies {
    // log
    compile "org.slf4j:slf4j-api:$slf4j_version"
    compile "org.slf4j:jul-to-slf4j:$slf4j_version"
    compile "org.slf4j:jcl-over-slf4j:$slf4j_version"
    compile "org.slf4j:log4j-over-slf4j:$slf4j_version"
    compile "org.apache.logging.log4j:log4j-api:$log4j2_version"
    compile "org.apache.logging.log4j:log4j-to-slf4j:$log4j2_version"
    compile "ch.qos.logback:logback-classic:$logback_version"
}

3.2 依賴?yán)【彺娌呗?/h3>
configurations.all {
    //每隔24小時(shí)檢查遠(yuǎn)程依賴是否存在更新
    resolutionStrategy.cacheChangingModulesFor 24, 'hours'
    //每隔10分鐘..
    // resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
    // 采用動(dòng)態(tài)版本聲明的依賴緩存10分鐘
    // resolutionStrategy.cacheDynamicVersionsFor 10*60, 'seconds'
}

3.3 依賴傳遞可配置

// 依賴不傳遞
compile("org.springframework:spring-web:4.3.4.RELEASE") {
    transitive = false
}


configurations {
    //編譯期排除commons模塊
    compile.exclude module: 'commons'
    //在整個(gè)構(gòu)建過(guò)程中排除pkaq.tiger:share
    all*.exclude group: 'pkaq.tiger', module: 'share'
}


dependencies {
    //當(dāng)前指定依賴不傳遞
    compile("pkaq.tiger:web:1.0") {
        exclude module: 'share'
    }      
}

// 強(qiáng)制使用該版本
compile('org.hibernate:hibernate:3.1') {
    force = true
}

// 全局配置昆雀,使用版本
configurations.all {
   resolutionStrategy {
       force 'org.hamcrest:hamcrest-core:1.3'
   }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蝠筑,隨后出現(xiàn)的幾起案子狞膘,更是在濱河造成了極大的恐慌,老刑警劉巖什乙,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挽封,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡臣镣,警方通過(guò)查閱死者的電腦和手機(jī)辅愿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)忆某,“玉大人点待,你說(shuō)我怎么就攤上這事∑妫” “怎么了癞埠?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)聋呢。 經(jīng)常有香客問(wèn)我苗踪,道長(zhǎng),這世上最難降的妖魔是什么坝冕? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任徒探,我火速辦了婚禮,結(jié)果婚禮上喂窟,老公的妹妹穿的比我還像新娘测暗。我一直安慰自己,他們只是感情好磨澡,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布碗啄。 她就那樣靜靜地躺著,像睡著了一般稳摄。 火紅的嫁衣襯著肌膚如雪稚字。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音胆描,去河邊找鬼瘫想。 笑死,一個(gè)胖子當(dāng)著我的面吹牛昌讲,可吹牛的內(nèi)容都是我干的国夜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼短绸,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼车吹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起醋闭,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤窄驹,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后证逻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體乐埠,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年瑟曲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了饮戳。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡洞拨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出负拟,到底是詐尸還是另有隱情烦衣,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布掩浙,位于F島的核電站花吟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏厨姚。R本人自食惡果不足惜衅澈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望谬墙。 院中可真熱鬧今布,春花似錦、人聲如沸拭抬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)造虎。三九已至傅蹂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背份蝴。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工犁功, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人婚夫。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓波桩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親请敦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子镐躲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容