• 使用plugin DSL引用自定义gradle plugin


    该文章同步更新到稀土掘金

    前言

    升级AndroidStudio到Chipmunk版本后,可以发现创建项目时默认使用gradle-7.3.3版本。这时查看项目根目录的build.gradle文件可以发现有了变化:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    plugins {
        id 'com.android.application' version '7.2.1' apply false
        id 'com.android.library' version '7.2.1' apply false
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    再对比旧版本:

    buildscript {
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.2'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    通过dependencies配置插件依赖的方式改成了plugins。
    当然变化不止这一点,将一些通用配置移到了settings.gradle

    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    }
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    rootProject.name = "GradlePluginDemo"
    include ':app'
    include ':libplugin'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    plugin DSL引用自定义的插件

    这里以local repository的方式讲解

    旧版(新版依然可以使用这个方式,自己增加buildscript即可)

    在旧版的配置中,我们只需要做三个事情:

    1. 配置插件路径
    2. 增加插件依赖
    // 这两步都在根目录的build.gradle的buildscript中配置
    buildscript {
        repositories {
            // 1. 配置插件路径
            maven {
                url = uri('./local-plugin-repository')
            }
        }
        dependencies {
            // 2. 增加插件依赖
            classpath 'com.king.libplugin:zipPNG:1.0'
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 引用插件
    // 在需要使用该插件的module的build.gradle引用插件
    plugins {
        id 'com.android.application'
        // 3. 引用插件
        id 'com.king.libplugin'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    新版

    1. 在settings.gradle的pluginManagement中增加配置
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
            // 1. 配置插件路径
            maven {
                url = uri('./local-plugin-repository')
            }
        }
        resolutionStrategy {
            eachPlugin {
                println("resolutionStrategy eachPlugin ${requested.id.namespace}")
                if (requested.id.namespace == 'com.king') {
                    // 2. 增加插件依赖
                    useModule("com.king.libplugin:zipPNG:${requested.version}")
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. 引用插件
    // 这里跟旧版有点不同,需要配置version:因为在第一点中,会遍历子module的build.gradle的plugins中配置了以com.king开头的插件,然后使用useModule进行配置
    plugins {
        id 'com.android.application'
        // 2. 引用插件
        id 'com.king.libplugin' version '1.0'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. useModule
      查看gradle api,看下useModule的解释:
    void useModule​(Object notation)
    Sets the implementation module to use for this plugin.
    Parameters:
    notation - the module to use, supports the same notations as DependencyHandler
    
    • 1
    • 2
    • 3
    • 4

    设置引用的plugin的具体实现模块

    自定义gradle plugin

    1. 新建一个java library的module,并定义plugin
    public class CustomPlugin implements Plugin {
        @Override
        public void apply(Project project) {
            project.getTasks().register("zipPNG", new Action() {
                @Override
                public void execute(Task task) {
                    System.out.println("Hello from plugin 'com.example.plugin.customname'");
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 配置自定义plugin和发布需要引用的插件
    plugins {
        // 用于配置自定义plugin
        id 'java-gradle-plugin'
        // 用于发布插件
        id "maven-publish"
    }
    
    // 定义插件
    gradlePlugin {
        plugins {
            zipPNG {
                // 唯一,用于后续作为引用该插件的标识
                id = 'com.king.libplugin'
                // 指定插件的实现类
                implementationClass = 'com.king.libplugin.CustomPlugin'
            }
        }
    }
    
    // 发布该插件
    publishing {
        publications {
            mavenJava(MavenPublication) {
                // 插件的组ID,建议设置为插件的包名
                groupId = 'com.king.libplugin'
                // 插件的名字,后续在引用时会用到
                artifactId = 'zipPNG'
                // 版本号
                version = '1.0'
                // 组件类型
                from components.java
            }
        }
        // 配置插件生成路径,这里为当前目录的local-plugin-repository目录下
        repositories {
            maven {
                url uri('../local-plugin-repository')
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    1. 在Gradle视图中,找到publishMavenJavaPublicationToMavenRepository点击即可在指定目录生成插件
    2. 引用方式为一二节描述的方式,两种方式都可以
  • 相关阅读:
    CTFshow 命令执行 web29 30 31
    2023年天津理工大学中环信息学院专升本机械制图考试大纲
    EvilJS 的 Golang 实现
    云原生应用安全
    数据库内核面试中我不会的问题(5)
    【学习笔记】Java 一对一培训(2.1)Java基础语法
    “人类高质量数据”如何训练计算机视觉模型?
    使用adobe font style 工具绘制的艺术字,请鉴赏。
    java多线程面试相关的一些问题
    oracle DG 原理
  • 原文地址:https://blog.csdn.net/u012950099/article/details/126021204