Gradle 排除依賴

本文將介紹如何在 Gradle 中排除掉指定的依賴。

Gradle 3.4 及以下版本

在 Gradle 3.4 及以下版本,我們引入依賴的方式還是通過 compile,如:

dependencies {
    compile 'org.apache.commons:commons-lang3:3.4'
    compile project(':common:util')
    testCompile 'junit:junit:4.12'
}

想要移除掉某個依賴中的模塊,我們通常會這么做:

dependencies {
    compile('commons-beanutils:commons-beanutils:1.9.4') {
        exclude group: 'commons-collections', module: 'commons-collections'
    }
    // 不指定 module
    compile('commons-beanutils:commons-beanutils:1.9.4') {
        exclude group: 'commons-collections'
    }
}

Gradle 3.4 及以上版本

升級到 Gradle 3.4+ 之后,Gradle 支持了默認(rèn)的新的引入依賴的方式: implementation

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

關(guān)于 compileimplementation 的區(qū)別可移步至 whats-the-difference-between-implementation-and-compile-in-gradle 查看詳情。

全局排除依賴

全局排除指定環(huán)境的依賴

configurations {
    runtime.exclude group: "org.slf4j", module: "slf4j-log4j12"
    compile.exclude group: "org.slf4j", module: "slf4j-log4j12"
}

全局排除所有環(huán)境下的依賴

configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

相關(guān)鏈接:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容