自定义gradle插件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jinhuoxingkong/article/details/75105034

一、概念



gradle是怎么运行的

Android中每个工程下面都有build.gradle,每个Moudle下面也有build.gradle,里面的内容都是以大括号为层级结构,那么我们猜测task也是对这些层级结构进行解析,最终进行编译,其实事实也是这样的,下面看一下自定义gradle的层级结构


二、典型例子


(1)在目录plugin/src/main/groovy/com/micky/gradle/下新建插件类MyCustomPlugin.groovy

package com.micky.gradle;

import org.gradle.api.*;

class MyCustomPlugin implements Plugin<Project> {
	void apply(Project project) {
		project.task('myTask') << {
			println "Hi this is micky's plugin"
		}
	}
}

(2)在目录plugin/src/main/resources/META-INF/gradle-plugins/下创建文件com.micky.mycustom.properties用来指定插件实现类

    implementation-class=com.micky.gradle.MyCustomPlugin  
特别注意下:文件名“com.micky.mycustom”即是以后我们在使用插件时的apply plugin 'Java' 的java。

(3)在plugin目录下新建settings.gradle,里面写入插件名称

rootProject.name='gradle-micky' 

(4)编译需要在plugin目录下新建build.gradle

    apply plugin: 'groovy'  
    apply plugin: 'maven'  
      
    dependencies {  
        compile gradleApi()  
        compile localGroovy()  
    }  
      
    repositories {  
        mavenCentral()  
    }  
      
    group='com.micky'  
    version='1.0.1'  
      
    uploadArchives {  
        repositories {  
            mavenDeployer {  
                repository(url: uri('../repo'))  
            }  
        }  
    }  
(5)执行命令:   gradle uploadArchives  进行编译,就会在本地创建gradle插件

(6)在sample目录下建立build.gradle,用来测试插件

    buildscript {  
        repositories {  
            maven {  
                url uri('../repo')  
            }  
        }  
      
        dependencies {  
            classpath group: 'com.micky',  
                name: 'gradle-micky',  
                version: '1.0.0'  
        }  
    }  
      
    apply plugin: 'com.micky.mycustom'  
(7)执行命令 gradle customTask 命令得结果



三、自定义task


(1)直接沿用上一篇的代码,copy一份改名CustomPluginTask

在plugin\src\main\groovy\com\micky\gradle目录创建源文件 MyCustomTask.groovy

    package com.micky.gradle;  
      
    import org.gradle.api.DefaultTask  
    import org.gradle.api.tasks.TaskAction  
      
    class MyCustomTask extends DefaultTask {  
        @TaskAction  
        void output() {  
            println "Hello this is my custom task output"  
        }  
    }  

(2)修改MyCustomPlugin.groovy
    package com.micky.gradle;  
      
    import org.gradle.api.*;  
      
    class MyCustomPlugin implements Plugin<Project> {  
        void apply(Project project) {  
            project.task('customTask', type: MyCustomTask)  
        }  
    }  

(3)改一下版本号,直接和上面一样编译,运行即可,这里是自定义的task



四、task有参数



拷贝一份上面的代码,改名为CustomPluginTaskWithParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

    package com.micky.gradle;  
      
    import org.gradle.api.*;  
      
    class MyCustomPluginExtension {  
        def message = "From MyCustomPluginExtention"  
        def sender = "MyCustomPluin"  
    }  
      
    class MyCustomPlugin implements Plugin<Project> {  
        void apply(Project project) {  
            project.extensions.create('myArgs', MyCustomPluginExtension)  
            project.task('customTask', type: MyCustomTask)  
        }  
    }  

(2)修改plugin\src\main\groovy\com\micky\gradle\MyCustomTask.groovy

    package com.micky.gradle;  
      
    import org.gradle.api.DefaultTask  
    import org.gradle.api.tasks.TaskAction  
      
    class MyCustomTask extends DefaultTask {  
        @TaskAction  
        void output() {  
            println "Sender is ${project.myArgs.sender},\nmessage: ${project.myArgs.message}"  
        }  
    }  

(3)修改plugin/build.gradle
    apply plugin: 'groovy'  
    apply plugin: 'maven'  
      
    dependencies {  
        compile gradleApi()  
        compile localGroovy()  
    }  
      
    repositories {  
        mavenCentral()  
    }  
      
    group='com.micky'  
    version='1.0.2'  
      
    uploadArchives {  
        repositories {  
            mavenDeployer {  
                repository(url: uri('../repo'))  
            }  
        }  
    }  

(4)执行gradle uploadArchives 编译插件包,修改sample/build.gradle
    buildscript {  
        repositories {  
            maven {  
                url uri('../repo')  
            }  
        }  
      
        dependencies {  
            classpath group: 'com.micky',  
                name: 'gradle-micky',  
                version: '1.0.2'  
        }  
    }  
      
    apply plugin: 'com.micky.mycustom'  

(5)执行gradle customTask 得到结果

(6)在gradle文件配置参数

    buildscript {  
        repositories {  
            maven {  
                url uri('../repo')  
            }  
        }  
      
        dependencies {  
            classpath group: 'com.micky',  
                name: 'gradle-micky',  
                version: '1.0.2'  
        }  
    }  
      
    apply plugin: 'com.micky.mycustom'  
      
    myArgs {  
        sender='Micky Liu'  
        message='Gradle is so simple.'  
    }  
(7)执行gradle customTask


五、task有嵌套参数



(1)拷贝一份上面的代码,改名为CustomPluginTaskWithNestParam,修改plugin\src\main\groovy\com\micky\gradle\MyCustomPlugin.groovy

    package com.micky.gradle;  
      
    import org.gradle.api.*;  
      
    class MyNestPluginExtension {  
        def receiver = "Kate Zhou"  
        def email = "KateZhou@gmail.com"  
    }  
      
    class MyCustomPluginExtension {  
        def message = "From MyCustomPluginExtention"  
        def sender = "MyCustomPluin"  
    }  
      
    class MyCustomPlugin implements Plugin<Project> {  
        void apply(Project project) {  
            project.extensions.create('myArgs', MyCustomPluginExtension)  
            project.myArgs.extensions.create('nestArgs', MyNestPluginExtension)  
            project.task('customTask', type: MyCustomTask)  
        }  
    }  

(2)修改MyCustomPlugin.groovy

    package com.micky.gradle;  
      
    import org.gradle.api.DefaultTask  
    import org.gradle.api.tasks.TaskAction  
      
    class MyCustomTask extends DefaultTask {  
        @TaskAction  
        void output() {  
            println "Sender is ${project.myArgs.sender} \nmessage: ${project.myArgs.message}"  
            println "Receiver is ${project.myArgs.nestArgs.receiver} \nemail: ${project.myArgs.nestArgs.email}"  
        }  
    }  

(3)修改plugin/build.gradle
    apply plugin: 'groovy'  
    apply plugin: 'maven'  
      
    dependencies {  
        compile gradleApi()  
        compile localGroovy()  
    }  
      
    repositories {  
        mavenCentral()  
    }  
      
    group='com.micky'  
    version='1.0.3'  
      
    uploadArchives {  
        repositories {  
            mavenDeployer {  
                repository(url: uri('../repo'))  
            }  
        }  
    }  

(4)执行gradle uploadArchives 编译插件包

(5)修改sample/build.gradle 配置参数

    buildscript {  
        repositories {  
            maven {  
                url uri('../repo')  
            }  
        }  
      
        dependencies {  
            classpath group: 'com.micky',  
                name: 'gradle-micky',  
                version: '1.0.3'  
        }  
    }  
      
    apply plugin: 'com.micky.mycustom'  
      
    myArgs {  
        sender='Micky Liu'  
        message='Gradle is so simple.'  
        nestArgs {  
            receiver='David Chen'  
            email='David@126.com'  
        }  
      
    }  

(6)执行gradle customTask






尊重作者,尊重原创,参考文章

http://blog.csdn.net/liuhongwei123888/article/details/50542104

http://blog.csdn.net/liuhongwei123888/article/details/50541759



阅读更多

没有更多推荐了,返回首页