在主项目中新建一个myplugin(Android Library),除了build.gradle其余文件全删除。
重新组织myplugin下的文件:
myplugin/src/main/groovy/包名/MyPlugin
package com.george.plugin
import org.gradle.api.Plugin
import org.gradle.api.Project
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
//这里实现plugin的逻辑
println "hello, this is my plugin!"
}
}
myplugin/src/main/resources/META-INF.gradle-plugins/myplugin.properties
implementation-class=com.george.plugin.MyPlugin
myplugin/build.gradle
apply plugin: 'groovy'
//添加maven plugin, 用于发布我们的jar
apply plugin: 'maven'
dependencies {
compile gradleApi()
compile localGroovy()
}
repositories {
mavenCentral()
}
//设置maven deployer
uploadArchives {
repositories {
mavenDeployer {
//设置插件的GAV参数
pom.groupId = 'com.george.plugin'
pom.artifactId = 'myplugin'
pom.version = 1.0
//文件发布到下面目录
repository(url: uri('../release'))
}
}
}
在gradle任务中,点击uploadArchives生成maven本地仓库插件资源到release中
在主项目中使用:
在app/build.gradle中添加
apply plugin: 'myplugin'
buildscript{
repositories{
maven{
url uri('../release')
}
}
dependencies{
classpath 'com.george.plugin:myplugin:1.0'
}
点击gradle中的build会在任务中打印出:hello, this is my plugin!
对gradle添加Task
在MyPLugin.groovy中添加任务
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.task("testTask") << {
println "Hello2 gradle plugin"
}
}
}
然后重复上面步骤点击upload/uploadArchives生成新的插件,然后在右侧gradle列表下的app(gradule)右键点击(Refresh Gradle project)就会在app/Tasks/other下生成testTask任务。点击这个任务就会输出:
Hello2 gradle plugin
实战:统计各个Task执行时间
TimingsListener.groovy
/**
* 统计各个Task执行时间
*/
class TimingsListener implements TaskExecutionListener, BuildListener {
private long startTime// 开始时间
private timings = []// 记录事件和时间
@Override
void beforeExecute(Task task) {
startTime = System.currentTimeMillis()
}
@Override
void afterExecute(Task task, TaskState taskState) {
def ms = System.currentTimeMillis() - startTime
timings.add([ms, task.path])
task.project.logger.warn "${task.path} took ${ms}ms"
}
@Override
void buildFinished(BuildResult result) {
println "Task timings:"
for (timing in timings) {
if (timing[0] >= 50) {
printf "%7sms %s\n", timing
}
}
}
}
在MyPlugin.groovy把TimingsListener放进gradle监听中
public class MyPlugin implements Plugin<Project> {
project.gradle.addListener(new TimingsListener())
}
}
然后同步下,随便执行个Task就会打印出时间信息
参数传递
新建一个PluginExtension.groovy,承载参数的实体类
public class PluginExtension{
def param1 = "param1 default"
def param2 = "param2 default"
}
新建一个PluginNestExtension.groovy,承载参数的实体类
public class PluginNestExtension{
def nestParam1 = "nestParam1 default"
def nestParam2 = "nestParam2 default"
}
新建CustomTask.groovy,继承DefaultTask,使用@TaskAction注解标注方法,输出参数日志
import org.gradle.api.tasks.TaskAction
public class CustomTask extends DefaultTask{
@TaskAction
void output(){
println "param1 is ${project.pluginExt.param1}"
println "param2 is ${project.pluginExt.param2}"
println "nestparam1 is ${project.pluginExt.nestExt.nestParam1}"
println "nestparam2 is ${project.pluginExt.nestExt.nestParam2}"
}
}
在MyPlugin.groovy中添加:
定义参数关系:pluginExt为一级参数闭包,nestExt是pluginExt下的参数闭包。
public class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create('pluginExt',PluginExtension)
project.pluginExt.extensions.create('nestExt',PluginNestExtension)
project.task('customTask',type: CustomTask)
}
}
在app.gradle中添加:
pluginExt{
param1 = 'app param1'
param2 = 'app param2'
nestExt{
nestParam1='app nestParam1'
nestParam2='app nestParam2'
}
}
网友评论