DependencyHandler

DependencyHandler 用於宣告依賴項。依賴項會分組到組態中 (請參閱 Configuration)。

若要為組態宣告特定的依賴項,您可以使用以下語法:

dependencies {
    configurationName dependencyNotation
}

範例顯示宣告依賴項的基本方式。

plugins {
    id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
}

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

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

  //declaring arbitrary files as dependencies
  implementation files('hibernate.jar', 'libs/spring.jar')

  //putting all jars from 'libs' onto compile classpath
  implementation fileTree('libs')
}

進階依賴配置

若要在宣告依賴項時進行一些進階配置,您可以額外傳遞配置閉包:

dependencies {
    configurationName(dependencyNotation){
        configStatement1
        configStatement2
    }
}

進階依賴項宣告範例,包括:

  • 在發生衝突時強制使用特定依賴項版本。
  • 依名稱、群組或同時依兩者排除特定依賴項。關於依賴項排除的更多詳細資訊,請參閱 ModuleDependency.exclude(java.util.Map) 的文件。
  • 避免特定依賴項的傳遞依賴。
plugins {
    id 'java' // so that I can declare 'implementation' dependencies
}

dependencies {
  implementation('org.hibernate:hibernate') {
    //in case of versions conflict '3.1' version of hibernate wins:
    version {
      strictly('3.1')
    }

    //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
  }
}

更多進階配置範例,在依賴項模組具有多個產出時很有用:

plugins {
    id("java-library")
}

dependencies {
  // Configuring dependency to specific configuration of the module
  // This notation should _only_ be used for Ivy dependencies
  implementation(group: "org.someOrg", name: "someModule", version: "1.0", configuration: "someConf")

  // Configuring dependency on 'someLib' module
  implementation(group: 'org.myorg', name: 'someLib', version:'1.0') {
    // Explicitly adding the dependency artifact:
    // Prefer variant-aware dependency resolution
    artifact {
      // Useful when some artifact properties unconventional
      name = 'someArtifact' // Artifact name different than module name
      extension = 'someExt'
      type = 'someType'
      classifier = 'someClassifier'
    }
  }
}

依賴標記法

支援數種依賴標記法。這些標記法如下所述。對於以此方式宣告的每個依賴項,都會建立 Dependency 物件。您可以使用此物件來查詢或進一步配置依賴項。

您也可以隨時直接新增 Dependency 的執行個體:

configurationName <instance>

依賴項也可以使用 Provider 宣告,此提供器會提供任何其他支援的依賴項標記法。

外部依賴項

有兩種標記法支援宣告對外部模組的依賴項。一種是字串標記法,格式如下:

configurationName "group:name:version:classifier@extension"

另一種是 Map 標記法:

configurationName group: group, name: name, version: version, classifier: classifier, ext: extension

在這兩種標記法中,除了名稱以外的所有屬性都是選用的。

外部依賴項由 ExternalModuleDependency 表示。

plugins {
    id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
}

dependencies {
  //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')

標記法 project(':project-a') 類似於在多模組 Gradle 專案中配置 projectA 時使用的語法。

專案依賴項的解析方式,是將目標專案中的每個可取用組態都視為變體,並對其執行感知變體的屬性比對。但是,為了覆寫此程序,可以指定明確的目標組態:

configurationName project(path: ':project-a', configuration: 'someOtherConfiguration')

專案依賴項使用 ProjectDependency 表示。

檔案依賴項

您也可以使用 FileCollection 新增依賴項:

configurationName files('a file')

plugins {
    id 'java' // so that we can use 'implementation', 'testImplementation' for dependencies
}

dependencies {
  //declaring arbitrary files as dependencies
  implementation files('hibernate.jar', 'libs/spring.jar')

  //putting all jars from 'libs' onto compile classpath
  implementation fileTree('libs')
}

檔案依賴項使用 FileCollectionDependency 表示。

對其他組態的依賴項

您可以使用 Configuration 新增依賴項。

當組態與目標組態來自同一個專案時,目標組態會變更為從提供的組態延伸。

當組態來自不同的專案時,會新增專案依賴項。

Gradle 發行版特定依賴項

可以依賴 Gradle API 或 Gradle 隨附的程式庫。這對於 Gradle 外掛程式開發特別有用。例如:

//Our Gradle plugin is written in groovy
plugins {
    id 'groovy'
}
// now we can use the 'implementation' configuration for declaring dependencies

dependencies {
  //we will use the Groovy version that ships with Gradle:
  implementation localGroovy()

  //our plugin requires Gradle API interfaces and classes to compile:
  implementation gradleApi()

  //we will use the Gradle test-kit to test build logic:
  testImplementation gradleTestKit()
}

用戶端模組依賴項

用戶端模組依賴項已棄用,將在 Gradle 9.0 中移除。請改用組件元數據規則。

若要將用戶端模組新增至組態,您可以使用以下標記法:

configurationName module(moduleNotation) {
    module dependencies
}

模組標記法與上述的依賴項標記法相同,但分類器屬性不可用。用戶端模組使用 ClientModule 表示。

屬性

屬性描述
components

此專案的組件元數據處理器。傳回的處理器可用於新增規則,以修改依賴軟體組件的元數據。

constraints

此專案的依賴項約束處理器。

extensions

擴充功能的容器。

modules

此專案的組件模組元數據處理器。傳回的處理器可用於新增規則,以修改依賴軟體組件的元數據。

方法

方法描述
add(configurationName, dependencyNotation)

將依賴項新增至給定的組態。

add(configurationName, dependencyNotation, configureClosure)

將依賴項新增至給定的組態,並使用給定的閉包配置依賴項。

components(configureAction)

為此專案配置組件元數據。

constraints(configureAction)

為此專案配置依賴項約束。

create(dependencyNotation)

建立依賴項,但不將其新增至組態。

create(dependencyNotation, configureClosure)

建立依賴項,但不將其新增至組態,並使用給定的閉包配置依賴項。

createArtifactResolutionQuery()

建立產出解析查詢。

enforcedPlatform(notation)

宣告對強制平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。強制平台是一種平台,其直接依賴項是強制的,這表示它們會覆寫圖表中找到的任何其他版本。

enforcedPlatform(notation, configureAction)

宣告對強制平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。強制平台是一種平台,其直接依賴項是強制的,這表示它們會覆寫圖表中找到的任何其他版本。

enforcedPlatform(dependencyProvider)

配置此依賴項提供器以選取目標組件的強制平台變體

enforcedPlatform(dependencyProviderConvertible)

配置此依賴項提供器以選取目標組件的強制平台變體

gradleApi()

建立對目前 Gradle 版本 API 的依賴項。

gradleTestKit()

建立對 Gradle TestKit API 的依賴項。

localGroovy()

建立對隨目前 Gradle 版本發行的 Groovy 的依賴項。

module(notation)
已棄用

建立對用戶端模組的依賴項。

module(notation, configureClosure)
已棄用

建立對用戶端模組的依賴項。依賴項會在傳回之前使用給定的閉包進行配置。

modules(configureAction)

為此專案配置模組元數據。

platform(notation)

宣告對平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。

platform(notation, configureAction)

宣告對平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。

platform(dependencyProvider)

配置此依賴項提供器以選取目標組件的平台變體

platform(dependencyProviderConvertible)

配置此依賴項提供器以選取目標組件的平台變體

project(notation)

建立對專案的依賴項。

registerTransform(actionType, registrationAction)

註冊 產出轉換

腳本區塊

沒有腳本區塊

屬性詳情

ComponentMetadataHandler components (唯讀)

此專案的組件元數據處理器。傳回的處理器可用於新增規則,以修改依賴軟體組件的元數據。

DependencyConstraintHandler constraints (唯讀)

此專案的依賴項約束處理器。

ExtensionContainer extensions (唯讀)

擴充功能的容器。

此專案的組件模組元數據處理器。傳回的處理器可用於新增規則,以修改依賴軟體組件的元數據。

方法詳情

Dependency add(String configurationName, Object dependencyNotation)

將依賴項新增至給定的組態。

Dependency add(String configurationName, Object dependencyNotation, Closure configureClosure)

將依賴項新增至給定的組態,並使用給定的閉包配置依賴項。

void components(Action<? super ComponentMetadataHandler> configureAction)

為此專案配置組件元數據。

此方法針對此專案的 ComponentMetadataHandler 執行給定的動作。

void constraints(Action<? super DependencyConstraintHandler> configureAction)

為此專案配置依賴項約束。

此方法針對此專案的 DependencyConstraintHandler 執行給定的動作。

Dependency create(Object dependencyNotation)

建立依賴項,但不將其新增至組態。

Dependency create(Object dependencyNotation, Closure configureClosure)

建立依賴項,但不將其新增至組態,並使用給定的閉包配置依賴項。

ArtifactResolutionQuery createArtifactResolutionQuery()

建立產出解析查詢。

這是舊版 API,目前處於維護模式。在 Gradle 的未來版本中,此 API 將被棄用並移除。新程式碼不應使用此 API。解析來源和 javadoc 時,請優先使用 <UNHANDLED-LINK>ArtifactView.ViewConfiguration#withVariantReselection()</UNHANDLED-LINK>

Dependency enforcedPlatform(Object notation)

宣告對強制平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。強制平台是一種平台,其直接依賴項是強制的,這表示它們會覆寫圖表中找到的任何其他版本。

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

宣告對強制平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。強制平台是一種平台,其直接依賴項是強制的,這表示它們會覆寫圖表中找到的任何其他版本。

配置此依賴項提供器以選取目標組件的強制平台變體

配置此依賴項提供器以選取目標組件的強制平台變體

Dependency gradleApi()

建立對目前 Gradle 版本 API 的依賴項。

Dependency gradleTestKit()

建立對 Gradle TestKit API 的依賴項。

Dependency localGroovy()

建立對隨目前 Gradle 版本發行的 Groovy 的依賴項。

Dependency module(Object notation)

注意:此方法已棄用,並將在下一個 Gradle 主要版本中移除。

建立對用戶端模組的依賴項。

Dependency module(Object notation, Closure configureClosure)

注意:此方法已棄用,並將在下一個 Gradle 主要版本中移除。

建立對用戶端模組的依賴項。依賴項會在傳回之前使用給定的閉包進行配置。

void modules(Action<? super ComponentModuleMetadataHandler> configureAction)

為此專案配置模組元數據。

此方法針對此專案的 ComponentModuleMetadataHandler 執行給定的動作。

Dependency platform(Object notation)

宣告對平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。

Dependency platform(Object notation, Action<? super Dependency> configureAction)

宣告對平台的依賴項。如果目標座標代表多個潛在組件,則會選取平台組件,而不是程式庫。

配置此依賴項提供器以選取目標組件的平台變體

配置此依賴項提供器以選取目標組件的平台變體

Dependency project(Map<String, ?> notation)

建立對專案的依賴項。

void registerTransform(Class<? extends TransformAction<T>> actionType, Action<? super TransformSpec<T>> registrationAction)

註冊 產出轉換

註冊動作需要指定 fromto 屬性。它也可以使用 TransformSpec.parameters(org.gradle.api.Action) 為轉換動作提供參數。

例如:

// You have a transform action like this:
abstract class MyTransform implements TransformAction<Parameters> {
    interface Parameters extends TransformParameters {
        @Input
        Property<String> getStringParameter();
        @InputFiles
        ConfigurableFileCollection getInputFiles();
    }

    void transform(TransformOutputs outputs) {
        // ...
    }
}

// Then you can register the action like this:

def artifactType = Attribute.of('artifactType', String)

dependencies.registerTransform(MyTransform) {
    from.attribute(artifactType, "jar")
    to.attribute(artifactType, "java-classes-directory")

    parameters {
        stringParameter.set("Some string")
        inputFiles.from("my-input-file")
    }
}