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

更多高级配置示例,在依赖模块具有多个 Artifact 时非常有用

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

在这两种表示法中,除了 name 之外,所有属性都是可选的。

外部依赖项由 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 项目中配置项目A时使用的语法。

项目依赖项通过将目标项目中的每个可消费配置视为一个变体,并对其执行变体感知的属性匹配来解析。然而,为了覆盖此过程,可以显式指定目标配置

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 附带的某些 Gradle API 或库。这对于 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
}

模块表示法与上面描述的依赖项表示法相同,只是 classifier 属性不可用。客户端模块使用 ClientModule 表示。

属性

属性描述
components

此项目的组件元数据处理器。返回的处理器可用于添加修改依赖软件组件元数据的规则。

constraints

此项目的依赖约束处理器。

extensions

扩展容器。

modules

此项目的组件模块元数据处理器。返回的处理器可用于添加修改依赖软件组件元数据的规则。

方法

方法描述
add(configurationName, dependencyNotation)

将依赖项添加到给定配置。

add(configurationName, dependencyNotation, configureClosure)

将依赖项添加到给定配置,并使用给定的闭包配置该依赖项。

components(configureAction)

配置此项目的组件元数据。

constraints(configureAction)

配置此项目的依赖约束。

create(dependencyNotation)

创建一个依赖项,但不将其添加到配置中。

create(dependencyNotation, configureClosure)

创建一个依赖项,但不将其添加到配置中,并使用给定的闭包配置该依赖项。

createArtifactResolutionQuery()

创建 Artifact 解析查询。

enforcedPlatform(notation)

声明对强制平台的依赖项。如果目标坐标表示多个潜在组件,将选择平台组件,而不是库。强制平台是指其直接依赖项是强制的平台,这意味着它们将覆盖图中找到的任何其他版本。

enforcedPlatform(notation, configureAction)

声明对强制平台的依赖项。如果目标坐标表示多个潜在组件,将选择平台组件,而不是库。强制平台是指其直接依赖项是强制的平台,这意味着它们将覆盖图中找到的任何其他版本。

enforcedPlatform(dependencyProvider)

配置此依赖提供者以选择目标组件的强制平台变体

enforcedPlatform(dependencyProviderConvertible)

配置此依赖提供者以选择目标组件的强制平台变体

gradleApi()

创建一个依赖于当前 Gradle 版本 API 的依赖项。

gradleTestKit()

创建一个依赖于 Gradle test-kit 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)

注册一个 artifact transform

脚本块

无脚本块

属性详情

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()

创建 Artifact 解析查询。

这是一个旧版 API,处于维护模式。在未来的 Gradle 版本中,此 API 将被弃用并移除。新代码不应使用此 API。建议使用 <UNHANDLED-LINK>ArtifactView.ViewConfiguration#withVariantReselection()</UNHANDLED-LINK> 来解析源和 Javadoc。

Dependency enforcedPlatform(Object notation)

声明对强制平台的依赖项。如果目标坐标表示多个潜在组件,将选择平台组件,而不是库。强制平台是指其直接依赖项是强制的平台,这意味着它们将覆盖图中找到的任何其他版本。

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

声明对强制平台的依赖项。如果目标坐标表示多个潜在组件,将选择平台组件,而不是库。强制平台是指其直接依赖项是强制的平台,这意味着它们将覆盖图中找到的任何其他版本。

配置此依赖提供者以选择目标组件的强制平台变体

配置此依赖提供者以选择目标组件的强制平台变体

Dependency gradleApi()

创建一个依赖于当前 Gradle 版本 API 的依赖项。

Dependency gradleTestKit()

创建一个依赖于 Gradle test-kit 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)

注册一个 artifact transform

注册动作需要指定 fromto 属性。它还可以通过使用 TransformSpec.parameters(org.gradle.api.Action) 为 transform 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")
    }
}