是Grad中的pmd,存储库等任务

问题描述 投票:1回答:1

我正在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和存储库部分以及为什么他们没有像“任务”这样的明确限定符,但我的listSubProjects需要一个任务限定符?这些继承的任务是否来自插件而不需要任务限定符?

gradle build.gradle
1个回答
1
投票

你看到的块是task extensions,也是discussed here

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

// 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块。

然后在用户build.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'
}

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

© www.soinside.com 2019 - 2024. All rights reserved.