您可以使用 IntelliJ 原生导入器Eclipse Buildship 在 IDE 中打开此示例。

此示例展示了如何将约定插件共享并应用于buildSrc中的生产代码和构建逻辑代码。

让我们使用以下项目结构

项目结构
├── application
│   ├── src
│   │   └── main
│   └── build.gradle.kts
├── build-conventions
│   ├── src
│   │   ...
│   ├── build.gradle.kts
│   └── settings.gradle.kts
├── buildSrc
│   ├── src
│   │   ...
│   └── build.gradle.kts
├── list
│   ├── src
│   │   ...
│   └── build.gradle.kts
├── utilities
│   ├── src
│   │   ...
│   └── build.gradle.kts
└── settings.gradle.kts
项目结构
├── application
│   ├── src
│   │   └── main
│   └── build.gradle
├── build-conventions
│   ├── src
│   │   ...
│   ├── build.gradle
│   └── settings.gradle
├── buildSrc
│   ├── src
│   │   ...
│   └── build.gradle
├── list
│   ├── src
│   │   ...
│   └── build.gradle
├── utilities
│   ├── src
│   │   ...
│   └── build.gradle
└── settings.gradle

构建配置如下

settings.gradle.kts
rootProject.name = "sample"
includeBuild("build-conventions")
include("application", "utilities", "list")
settings.gradle
rootProject.name = 'sample'
includeBuild 'build-conventions'
include 'application', 'utilities', 'list'

共享的约定插件myproject.java-conventions是在名为build-conventions包含构建中实现的。然后,该构建被包含在根项目的settings文件中,如上面的代码片段所示。有关包含构建的更多详细信息,请参阅组合构建章节。

然后将myproject.java-conventions插件应用于buildSrc构建

buildSrc/build.gradle.kts
plugins {
    id("myproject.java-conventions")
    `kotlin-dsl`
}

repositories {
    // for kotlin-dsl plugin
    gradlePluginPortal()
}
buildSrc/build.gradle
plugins {
    id 'myproject.java-conventions'
    id 'groovy-gradle-plugin'
}

它也应用于根项目的子项目

application/build.gradle.kts
plugins {
    id("myproject.java-conventions")
    // myproject.greeting is implemented in the buildSrc project that has myproject.java-conventions applied as well
    id("myproject.greeting")
    id("application")
}
utilities/build.gradle.kts
plugins {
    id("myproject.java-conventions")
    id("java-library")
}
list/build.gradle.kts
plugins {
    id("myproject.java-conventions")
    id("java-library")
}
application/build.gradle
plugins {
    id 'myproject.java-conventions'
    // myproject.greeting is implemented in the buildSrc project that has myproject.java-conventions applied as well
    id 'myproject.greeting'
    id 'application'
}
utilities/build.gradle
plugins {
    id 'myproject.java-conventions'
    id 'java-library'
}
list/build.gradle
plugins {
    id 'myproject.java-conventions'
    id 'java-library'
}

有关编写自定义 Gradle 插件的更多详细信息,请参阅用户手册