【Gradle】Gradle如何排除依赖项目的某些包

这篇博客介绍了如何在Gradle中排除某个项目依赖的特定包。作者在研究官方文档后,发现可以通过在依赖闭包中使用`exclude`方法来排除特定的组和模块。例如,`exclude(group:'com.github.pagehelper', module:'pagehelper-spring-boot-starter')`。通过这种方式,可以解决版本冲突或减少不必要依赖的问题。作者提醒读者,深入学习Groovy和Gradle DSL对于理解和解决问题至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gradle如何排除依赖项目的某些包

在这里插入图片描述

在开发一个Gradle相关项目时,遇到了Gradle如何排除依赖项目的某些包这个问题,网上也并不能找到相关问题的解决方案,这就让我需要我仔细阅读官方文档相关部分了。

官方文档描述如下:

若要为配置声明特定的依赖项,可以使用以下语法:

dependencies {
    configurationName dependencyNotation
}

要在声明依赖项时对其执行一些高级配置,还可以传递一个配置闭包:

dependencies {
    configurationName(dependencyNotation){
        configStatement1
        configStatement2
    }
}

案例:

plugins {
    id 'java' // so that I can declare 'implementation' dependencies
}

dependencies {
  implementation('org.hibernate:hibernate:3.1') {
    //in case of versions conflict '3.1' version of hibernate wins:
    force = true

    //excluding a particular transitive dependency:
    exclude module: 'cglib' //by artifact name
    exclude group: 'org.jmock' //by group
    exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group

    //disabling all transitive dependencies of this dependency
    transitive = false
  }
}

项目依赖语法:

configurationName project(':some-project')

可是上面只说了项目依赖,包依赖,和包排除依赖的例子,根本没有说明如何排除项目依赖里面的包啊。具体可见:
https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html
思考捣鼓了半会,发现闭包可以解决这个问题

dependencies {
	//使用闭包
    implementation (project(path: ':vblog-server-common')){
    	// 排除项目依赖
        exclude(group: 'com.github.pagehelper', module: 'pagehelper-spring-boot-starter')
        exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-data-redis')
    }
    implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
}

搁这查看半天,发现是语法没入门,是时候该认真学习一下Groovy和查阅Gradle相关Api了

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值