自定义gradle插件
Posted 劲火星空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义gradle插件相关的知识,希望对你有一定的参考价值。
一、概念
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 命令得结果
(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
以上是关于自定义gradle插件的主要内容,如果未能解决你的问题,请参考以下文章
Android Gradle 插件将自定义 Gradle 插件上传到自建 Maven 仓库 ③ ( 配置上传工件 | 将 Gradle 插件 jar 包源码文档上传到本地Maven 仓库 )
Android Gradle 插件自定义 Gradle 插件模块 ② ( 在模块中定义插件 | 引入自定义 Gradle 插件模块 | 配置 Gradle 插件上传选项 | 配置分组名称版本号 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑦ ( 自定义 Gradle 插件导入方式 | buildSrc 插件导入 | 构建脚本中自定义插件 | 独立文件 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑦ ( 自定义 Gradle 插件导入方式 | buildSrc 插件导入 | 构建脚本中自定义插件 | 独立文件 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑤ ( 自定义插件中获定义方法 | 在插件中创建 Gradle 任务 | 代码示例 )