DependencyConstraintHandler

DependencyConstraintHandler 用於宣告相依性約束。

相依性約束標記法

以下描述幾種支援的相依性約束標記法。對於以此方式宣告的每個相依性約束,都會建立一個 DependencyConstraint 物件。您可以使用此物件來查詢或進一步配置相依性約束。

您也可以隨時直接新增 DependencyConstraint 的實例

configurationName(<instance>)

相依性約束也可以使用 Provider 宣告,該 Provider 提供任何其他支援的相依性約束標記法。

外部相依性

以下支援兩種標記法,用於宣告外部模組的相依性約束。一種是字串標記法,格式如下

configurationName("group:name:version")

另一種是 Map 標記法

configurationName(group: group, name: name, version: version)

在這兩種標記法中,除了 name 之外,所有屬性都是選填的。

plugins {
    id("java-library") // so that we can use 'implementation', 'testImplementation' for dependency constraints
}

dependencies {
  constraints {
    //for dependencies found in artifact repositories you can use
    //the string notation, e.g. group:name:version
    implementation 'commons-lang:commons-lang:2.6'
    testImplementation 'org.mockito:mockito:1.9.0-rc1'

    //map notation:
    implementation group: 'com.google.code.guice', name: 'guice', version: '1.0'
  }
}

專案相依性

若要新增專案相依性約束,您可以使用以下標記法

configurationName(project(":some-project"))

屬性

無屬性

方法

方法說明
add(configurationName, dependencyNotation)

將相依性約束新增至給定的配置。

add(configurationName, dependencyNotation, configureAction)

將相依性約束新增至給定的配置,並使用給定的閉包配置相依性約束。

create(dependencyConstraintNotation)

建立相依性約束,但不將其新增至配置。

create(dependencyConstraintNotation, configureAction)

建立相依性約束,但不將其新增至配置,並使用給定的閉包配置相依性約束。

enforcedPlatform(notation)

宣告強制平台上的約束。如果目標座標代表多個潛在組件,則將選擇平台組件,而不是程式庫。強制平台是指直接相依性被強制的平台,這表示它們將覆蓋圖表中找到的任何其他版本。

enforcedPlatform(notation, configureAction)

宣告強制平台上的約束。如果目標座標代表多個潛在組件,則將選擇平台組件,而不是程式庫。強制平台是指直接相依性被強制的平台,這表示它們將覆蓋圖表中找到的任何其他版本。

腳本區塊

無腳本區塊

方法詳情

DependencyConstraint add(String configurationName, Object dependencyNotation)

將相依性約束新增至給定的配置。

DependencyConstraint add(String configurationName, Object dependencyNotation, Action<? super DependencyConstraint> configureAction)

將相依性約束新增至給定的配置,並使用給定的閉包配置相依性約束。

DependencyConstraint create(Object dependencyConstraintNotation)

建立相依性約束,但不將其新增至配置。

DependencyConstraint create(Object dependencyConstraintNotation, Action<? super DependencyConstraint> configureAction)

建立相依性約束,但不將其新增至配置,並使用給定的閉包配置相依性約束。

DependencyConstraint enforcedPlatform(Object notation)

宣告強制平台上的約束。如果目標座標代表多個潛在組件,則將選擇平台組件,而不是程式庫。強制平台是指直接相依性被強制的平台,這表示它們將覆蓋圖表中找到的任何其他版本。

DependencyConstraint enforcedPlatform(Object notation, Action<? super DependencyConstraint> configureAction)

宣告強制平台上的約束。如果目標座標代表多個潛在組件,則將選擇平台組件,而不是程式庫。強制平台是指直接相依性被強制的平台,這表示它們將覆蓋圖表中找到的任何其他版本。