上一篇Android使用FreeMarker制作代碼模板中寫(xiě)道缩滨,利用FreeMarker來(lái)實(shí)現(xiàn)代碼模板轉(zhuǎn)換為java文件天试,這一篇將要描述如果通過(guò)插件將我們的FreeMarker工具類(lèi)利用上趁啸。
1. 準(zhǔn)備工作
在上一篇中所使用的項(xiàng)目的基礎(chǔ)上進(jìn)行更改天通,來(lái)實(shí)現(xiàn)通過(guò)利用Gradle插件自動(dòng)生成java文件的功能娃肿。這里我使用的是Gradle-Plugins網(wǎng)站疾嗅,通過(guò)這個(gè)網(wǎng)站,我們可以發(fā)布我們的插件應(yīng)用谴蔑。
????1.1 首先豌骏,打開(kāi)Module下的build.gradle文件,輸入如下部分隐锭,創(chuàng)建groovy的語(yǔ)言環(huán)境:
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
compile gradleApi()
compile localGroovy()
}
repositories {
mavenCentral()
}
1.2 在mian文件夾下肯适,創(chuàng)建一個(gè)文件夾,命名為groovy成榜,創(chuàng)建完成后框舔,android studio會(huì)自動(dòng)識(shí)別。在groovy文件夾下創(chuàng)建我們的包名赎婚,這里我創(chuàng)建的包名與java文件夾下的包名一樣刘绣,但不影響,如下圖:1.3 實(shí)現(xiàn)插件挣输。如下代碼PluginImpl所示纬凤,其主要部分是使用project.afterEvaluate將任務(wù)插入到所有任務(wù)中:
package com.hu.freemarkerlib
import com.hu.freemarkerlib.bean.PluginBean
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File
import com.hu.freemarkerlib.task.FreeMarkerTask
public class PluginImpl implements Plugin<Project> {
void apply(Project project) {
println "Hello gradle plugin"
project.extensions.create('pluginImpl', PluginBean)
project.task("showName") << {
def pluginBean = project.extensions.pluginImpl
println("--------------------------------------------------------------------------------------")
println(pluginBean.outDirFile);
println(pluginBean.packageName);
println(pluginBean.clazzName);
println(pluginBean.id);
println(pluginBean.sex);
}
def freeMarkerTask = project.tasks.create("FreeMarkerTask", FreeMarkerTask)
project.afterEvaluate {
freeMarkerTask.execute()
}
}
}
然后在src/mian/resources/中創(chuàng)建文件夾META-INF/gradle-plugins/,在其下創(chuàng)建以.properties結(jié)尾的文件撩嚼,名稱(chēng)任意停士,在文件中放入你的插件實(shí)現(xiàn):
implementation-class=com.hu.freemarkerlib.PluginImpl
放入你的插件的路徑的意義在于告知插件的位置所在。
2. 實(shí)現(xiàn)代碼
FreeMarkerTask實(shí)現(xiàn):
package com.hu.freemarkerlib.task
import com.hu.freemarkerlib.Generator
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class FreeMarkerTask extends DefaultTask {
FreeMarkerTask() {
super()
}
@TaskAction
def doAction() {
def pluginBean = project.extensions.pluginImpl
println("----------------------------------------***************************--------------------")
println(pluginBean.toString())
def daoGenerator = new Generator()
daoGenerator.generate(pluginBean.outDirFile,pluginBean.packageName, pluginBean.clazzName,pluginBean.id,pluginBean.sex,pluginBean.name)
}
}
其PluginBean的實(shí)現(xiàn)如下:
package com.hu.freemarkerlib.bean
class PluginBean {
private Integer id = 23;
private String name = 'Jeck';
private String sex = 'man';
private String clazzName = 'defaultClassName';
private String packageName = 'defaultPackage';
private String outDirFile = 'defaultDir';
Integer getId() {
return id
}
void setId(Integer id) {
this.id = id
}
String getName() {
return name
}
void setName(String name) {
this.name = name
}
String getSex() {
return sex
}
void setSex(String sex) {
this.sex = sex
}
String getClazzName() {
return clazzName
}
void setClazzName(String clazzName) {
this.clazzName = clazzName
}
String getPackageName() {
return packageName
}
void setPackageName(String packageName) {
this.packageName = packageName
}
String getOutDirFile() {
return outDirFile
}
void setOutDirFile(String outDirFile) {
this.outDirFile = outDirFile
}
@Override
public String toString() {
return "PluginBean{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", clazzName='" + clazzName + '\'' +
", packageName='" + packageName + '\'' +
", outDirFile='" + outDirFile + '\'' +
'}';
}
}
- 添加發(fā)布插件配置信息
在Gradle-Plugins網(wǎng)站中可以了解到如何配置你的插件完丽,這里貼出主要部分:
// First, apply the publishing plugin
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.gradle.publish:plugin-publish-plugin:0.9.10"
}
}
apply plugin: "com.gradle.plugin-publish"
// Unless overridden in the pluginBundle config DSL, the project version will
// be used as your plugin version when publishing
version = "0.15"
group = "com.hu.freemarkerlib"
// The configuration example below shows the minimum required properties
// configured to publish your plugin to the plugin portal
pluginBundle {
website = 'https://github.com/RaysHuch/'
vcsUrl = 'https://github.com/RaysHuch/FreeMarkerDemo'
description = 'Use FreeMarker for android and gradle'
tags = ['freeMarkerPlugin']
plugins {
greetingsPlugin {
id = 'com.hu.freemarkerlib'
displayName = 'Gradle Greeting plugin'
}
}
}
在此同時(shí)恋技,需要在C:\Users${電腦用戶(hù)名}.gradle\gradle.properties中放入你的key和secret:
gradle.publish.key=************************************
gradle.publish.secret=*************************************
進(jìn)行了以上操作后,你就可以將你的插件發(fā)布的網(wǎng)站中了逻族,有時(shí)需要科學(xué)上網(wǎng)蜻底。
在你的項(xiàng)目中你會(huì)發(fā)現(xiàn)有如下變化:
首選需要點(diǎn)擊login的task,在Console中將會(huì)看到:
這里需要你點(diǎn)擊一下鏈接聘鳞,之后會(huì)出現(xiàn)成功的字樣(當(dāng)然是英文)薄辅。
到了最后一步,點(diǎn)擊你的publishPlugins的Task抠璃,將你的插件發(fā)布站楚,這樣你的插件就真真正正可以使用了!
3搏嗡、使用插件
插件的使用也是相當(dāng)簡(jiǎn)單窿春,按照如下配置,你的項(xiàng)目就可以起飛了:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.hu.freemarkerlib:FreeMarkerLib:0.15"
}
}
apply plugin: "com.hu.freemarkerlib"
4. 結(jié)語(yǔ)
由于是第一次使用插件,創(chuàng)建插件還是蠻簡(jiǎn)單的谁尸,但怎么執(zhí)行Task就成了老大難的問(wèn)題,一開(kāi)始認(rèn)為纽甘,直接在Plugin的apply方法中調(diào)用你的Java代碼就可以了良蛮,說(shuō)的沒(méi)錯(cuò),確實(shí)能夠順利執(zhí)行悍赢,讓我一度很興奮决瞳,終于插件能跑了。但是左权,當(dāng)我實(shí)現(xiàn)從build.gradle文件中讀取配置信息的時(shí)候就面臨了問(wèn)題皮胡,無(wú)論怎樣都不會(huì)讀取到所寫(xiě)的配置信息,各種搜索才知道赏迟,需要在project.afterEvaluate中執(zhí)行你的Task屡贺,build.gradle所配置的信息才會(huì)真正的加載運(yùn)行環(huán)境里。
傳送門(mén):