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"

另一种是映射表示法

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

属性

属性描述
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 test-kit API 的依赖项。

localGroovy()

创建对当前 Gradle 版本附带的 Groovy 的依赖。

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。优先使用 <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 的依赖。

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