自定义Gradle插件

1,425 阅读2分钟

自定义Gradle插件主要有三种方式:

build.gradle中编写;

buildSrc工程项目中编写;

独立项目中编写 Groovy、Java、Kotlin都可以作为实现插件的语言,在本文的示例中,使用Groovy作为实现语言。

方式1:build.gradle中编写

build.gradle

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('hello') {
            doLast {
                println 'Hello from the GreetingPlugin'
            }
        }
    }
}

// Apply the plugin
apply plugin: GreetingPlugin

运行task:gradle -q hello

> gradle -q hello
Hello from the GreetingPlugin

自定义插件 扩展

build.gradle
class GreetingPluginExtension {
    String message = 'Hello from GreetingPlugin'
}

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Add the 'greeting' extension object
        def extension = project.extensions.create('greeting', GreetingPluginExtension)
        println "extension.msg:${extension.message}"
        // Add a task that uses configuration from the extension object
        project.task('hello') {
            doLast {
                println extension.message
            }
        }
    }
}
apply plugin: GreetingPlugin

运行task

> gradle -q hello
extension.msg:Hello from GreetingPlugin
Hi from Gradle

自定义插件,配置信息

class GreetingPluginExtension {
    String message = 'Hello'
    String greeter = 'GreetingPlugin'
}

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Add the 'greeting' extension object
        def extension = project.extensions.create('greeting', GreetingPluginExtension)
        println "${extension.message} from ${extension.greeter}"
        // Add a task that uses configuration from the extension object
        project.task('hello') {
            doLast {
                println "${extension.message} from ${extension.greeter}"
            }
        }
    }
}
apply plugin: GreetingPlugin

// Configure the extension using a DSL block
greeting {
    message = 'Hi'
    greeter = 'Gradle'
}

运行task

> gradle -q hello
Hello from GreetingPlugin
Hi from Gradle from Gradle

方式2:buildSrc工程项目中编写

可以把插件代码放到如下路径:

rootProjectDir/buildSrc/src/main/java
或
rootProjectDir/buildSrc/src/main/groovy
或
rootProjectDir/buildSrc/src/main/kotlin

根据编写脚本的语言自行配置。

插件在整个项目里是可见的,但是对外不可见。

运行 Gradle 时会检查项目中是否存在一个名为 buildSrc 的目录。然后 Gradle 会自动编译并测试这段代码,并将其放入构建脚本的类路径中, 对于多项目构建,只能有一个 buildSrc 目录,该目录必须位于根项目目录中, buildSrc 是 Gradle 项目根目录下的一个目录,它可以包含我们的构建逻辑,与脚本插件相比,buildSrc 应该是首选,因为它更易于维护、重构和测试代码

目录结构

package com.qh.sample.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project

class SamplePluginImpl implements Plugin<Project> {
    void apply(Project project) {
        project.task('testTask') {
            println "Hello gradle plugin buildSrc"
        }
    }
}

运行task

> gradle -q testTask
Hello gradle plugin buildSrc

方式3:独立项目中编写

settings.gradle

include ':sample_plugin'

build.gradle

uploadArchives {
    repositories {
        mavenDeployer {
            //设置插件的GAV参数
            pom.groupId = 'com.qh.sample.plugin'//你的包名
            pom.artifactId = 'samplePlugin'
            pom.version = '1.0.1'//版本号
            //文件发布到下面目录  ../是父目录
            repository(url: uri(rootProject.ext.localRepoURL))
        }
    }
}

发布到maven

示例代码

github.com/bineanzhou/…

参考链接

docs.gradle.org/6.7.1/userg…