gradle排除某些依赖

1、在dependencies里排除某个依赖版本

dependencies {  
	//排除nacos-config里的nacos-client依赖
	implementation('com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config') {  
	exclude group: 'com.alibaba.nacos', module: 'nacos-client'  
	}
}

2、在configurations里排除某个依赖任何版本

configurations{  
	implementation {  
		exclude group: 'com.alibaba.nacos', module: 'nacos-client'
	}  
}

### 如何在 Gradle 中使用 `dependencyManagement` 排除特定依赖 为了管理复杂的多模块项目中的依赖关系,有时需要排除某些传递性依赖。这可以通过配置 `dependencyManagement` 来实现。 #### 配置 `dependencyManagement` 当定义依赖项时,在 `dependencies` 块内可以指定排除依赖项: ```groovy dependencies { implementation('org.example:some-library:1.0') { exclude group: 'com.unwanted', module: 'unneeded-dependency' } } ``` 对于更广泛的控制,可以在顶层设置 `dependencyManagement`,从而影响整个项目的依赖解析行为[^1]。 如果希望在整个项目范围内应用这些排除规则而不必重复声明,则可在根项目的 `build.gradle` 文件中加入如下代码片段: ```groovy dependencyManagement { imports { mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}" } rules { allModules { eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'javax.annotation') { details.useTarget 'jakarta.annotation:jakarta.annotation-api' } } } // 定义全局排除策略 resolutionStrategy.eachDependency { DependencyResolveDetails details -> if ('specific-group-id' == details.requested.group && 'specific-artifact-id' == details.requested.name) { details.exclude(group: 'excluded-group-id') } } } } ``` 上述脚本展示了如何通过自定义规则来修改或排除特定版本的依赖项以及怎样实施全局性的排除逻辑[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值