buildSrc 文件夹中的 .gradle 文件是项目的插件吗?

Is a .gradle file in buildSrc folder a plugin for projects?

我在gradle学习gradle官网:

https://docs.gradle.org/current/userguide/declaring_dependencies_between_subprojects.html

这里,在其中一个示例中,它在 buildSrc 文件夹中有一个文件-

buildSrc/src/main/kotlin/myproject.java-conventions.gradle.kts

plugins {
    id("java")
}

group = "com.example"
version = "1.0"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("junit:junit:4.13")
}

项目结构为:

.
├── buildSrc
│   ...
├── api
│   ├── src
│   │   └──...
│   └── build.gradle.kts
├── services
│   └── person-service
│       ├── src
│       │   └──...
│       └── build.gradle.kts
├── shared
│   ├── src
│   │   └──...
│   └── build.gradle.kts
└── settings.gradle.kts

模块构建脚本 api/build.gradle.ktsshared/build.gradle.kts 正在使用插件 'myproject.java-conventions' 作为:

plugins {
    id("myproject.java-conventions")
}

我参考了下面关于如何创建自定义插件的内容。

https://www.tutorialspoint.com/gradle/gradle_plugins.htm#:~:text=Plugin%20is%20nothing%20but%20set,are%20handled%20by%20plugins.&text=Extend%20the%20basic%20Gradle%20model,elements%20that%20can%20be%20configured).

我很困惑为什么在 buildSrc 中有一个 .gradle 或 .gradle.kts 文件是项目能够使用的插件。而且,如果是这样,他们为什么不将其用作:

应用插件:myproject.java-约定

应用插件有什么区别apply plugin: myproject.java-conventions

plugins {
    id("myproject.java-conventions")
}

是的,myproject.java-conventions.gradle.kts 文件是一个 gradle 插件。具体来说就是buildSrc项目中的一个precompiled script plugin. All sub-modules are able to use this plugin because by packaging a plugin

Gradle will take care of... making it available on the classpath of the build script. The plugin is visible to every build script used by the build.

plugins 块只是一种应用插件的新方法。参见: