建.是 pmd、存储库等任务在 Gradle 中



我正在 Gradle 中创建基本的自定义任务,并学习如何扩展它们以执行更复杂的操作(从这里学习:https://docs.gradle.org/current/userguide/tutorial_using_tasks.html)。

我的一个参考项目,我正在扩展以学习 Gradle 看起来像这样

// pmd config
pmd {
ignoreFailures = false
reportsDir = file("$globalOutputDir/pmd")
toolVersion = toolVersions.pmdVersion
}
repositories {
mavenCentral()
}
task listSubProjects{
doLast{
println 'Searching in root dir `'
}
}

我的问题是围绕 pmd 和存储库部分,为什么它们没有像"任务"这样的明确限定符,但我的列表子项目需要任务限定符?这些任务是否来自插件,不需要任务限定符?

您看到的块是任务扩展,此处也进行了讨论。

插件创建者可以定义扩展以允许用户配置插件:

// plugin code
class GreetingPluginExtension {
// default value
String message = 'Hello from GreetingPlugin'
}
// plugin code
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension)
// Add a task that uses configuration from the extension object
...
}
}

project.extensions.create('greeting',...中定义了稍后在build.gradle文件中使用的greeting块。

然后在用户构建.gradle文件中

apply plugin: GreetingPlugin
// Configure the extension
greeting.message = 'Hi from Gradle'
// Same effect as previous lines but with different syntax
greeting {
message = 'Hi from Gradle'
}

通常,扩展的名称被选择为与插件和/或任务相同,这可能会使事情变得混乱。

最新更新