关闭

自定义Gradle插件(二)

5225人阅读 评论(2) 收藏 举报
分类:

自定义Gradle插件(一)

自定义Gradle插件(二)


上一篇博客已经介绍了如何实现简单的自定义gradle插件,今天介绍如何自定义Task。

一、自定义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、修改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'))
		}
	}
}
         执行gradle uploadArchives 编译插件包
         

         4、sample目录下的build.gradle

buildscript {
	repositories {
		maven {
			url uri('../repo')
		}
	}

	dependencies {
		classpath group: 'com.micky',
			name: 'gradle-micky',
			version: '1.0.1'
	}
}

apply plugin: 'com.micky.mycustom'
         执行gradle customTask 结果如下:

        

         源码地址:https://github.com/mickyliu945/GradleCustomPlugin


二、向Plugin Task 传递参数

        1、 拷贝一份上面的代码,改名为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'))
		}
	}
}

        执行gradle uploadArchives 编译插件包

        

        4、修改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,结果如下:
        

        源码地址:https://github.com/mickyliu945/GradleCustomPlugin


三、向Plugin 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.'
}
           
         6、执行执行gradle customTask,结果如下:

          

         7、在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'
	}

}

         8、执行gradle customTask,结果如下:

         

源码地址:https://github.com/mickyliu945/GradleCustomPlugin

3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:788636次
    • 积分:7149
    • 等级:
    • 排名:第3236名
    • 原创:112篇
    • 转载:4篇
    • 译文:0篇
    • 评论:237条
    文章分类
    最新评论