API 文件 | DependencySubstitutions |
---|
允許使用其他依賴項替換依賴項。
方法 | 描述 |
all(rule) | 新增一個依賴項替換規則,當組態正在解析時,會為每個依賴項(包括傳遞性依賴項)觸發此規則。此 action 接收一個 |
module(notation) | 從提供的輸入字串建立 ModuleComponentSelector。字串格式必須為「{group}:{module}:{version}」。 |
project(path) | 從提供的輸入字串建立 ProjectComponentSelector。字串格式必須為「:path」。 |
substitute(substitutedDependency) | DSL 友善機制,用於為符合所提供 selector 的依賴項建構依賴項替換。 |
DependencySubstitutions
all
(Action
<? super DependencySubstitution
>
rule)
Action
<? super DependencySubstitution
>新增一個依賴項替換規則,當組態正在解析時,會為每個依賴項(包括傳遞性依賴項)觸發此規則。此 action 接收一個 DependencySubstitution
的實例,可用於找出正在解析哪個依賴項,並影響解析過程。
範例
configurations { main } // add dependency substitution rules configurations.main.resolutionStrategy.dependencySubstitution { // Use a rule to change the dependency module while leaving group + version intact all { DependencySubstitution dependency -> if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.module == 'groovy-all') { dependency.useTarget dependency.requested.group + ':groovy:' + dependency.requested.version } } // Use a rule to replace all missing projects with module dependencies all { DependencySubstitution dependency -> if (dependency.requested instanceof ProjectComponentSelector) { def targetProject = findProject(":${dependency.requested.path}") if (targetProject == null) { dependency.useTarget "org.myorg:" + dependency.requested.path + ":+" } } } }
規則會依宣告順序評估。規則會在強制模組套用後評估(請參閱 ResolutionStrategy.force(java.lang.Object[])
ComponentSelector
module
(String
notation)
從提供的輸入字串建立 ModuleComponentSelector。字串格式必須為「{group}:{module}:{version}」。
ComponentSelector
project
(String
path)
從提供的輸入字串建立 ProjectComponentSelector。字串格式必須為「:path」。
Substitution
substitute
(ComponentSelector
substitutedDependency)
DSL 友善機制,用於為符合所提供 selector 的依賴項建構依賴項替換。
範例
configurations { main } configurations.main.resolutionStrategy.dependencySubstitution { // Substitute project and module dependencies substitute module('org.gradle:api') using project(':api') substitute project(':util') using module('org.gradle:util:3.0') // Substitute one module dependency for another substitute module('org.gradle:api:2.0') using module('org.gradle:api:2.1') }