使用 Kotlin , Groovy ,Java 开发一个自己的 Gradle 插件
先上效果图:
功能说明:
korGenerate:
自动生成 Entity,Dao,Controller 模板代码
korFront:
自动 copy 前端 front 工程目录下面的 —— nowa 构建的 dist 目录下的静态资源文件:html, css, js
build.gradle
group 'com.easykotlin.plugin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile gradleApi()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
其中,compile gradleApi() 是使用 Gradle 的 API 依赖。
配置上传到 maven 仓库,这里我们配置上传至本机的目录下:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri('/Users/jack/.m2/repository'))
}
}
}
点击右侧工具栏的: upload > uploadArchives
执行日志:
23:41:48: Executing external task 'uploadArchives'...
:compileKotlin UP-TO-DATE
:compileJava NO-SOURCE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:uploadArchives
BUILD SUCCESSFUL in 0s
4 actionable tasks: 1 executed, 3 up-to-date
23:41:49: External task execution finished 'uploadArchives'.
看一下本机的上传情况:
~/.m2/repository/com/easykotlin/plugin/kor$ tree
.
├── 1.0-SNAPSHOT
│ ├── kor-1.0-20171221.172201-1.jar
│ ├── kor-1.0-20171221.172201-1.jar.md5
│ ├── kor-1.0-20171221.172201-1.jar.sha1
│ ├── kor-1.0-20171221.172201-1.pom
│ ├── kor-1.0-20171221.172201-1.pom.md5
│ ├── kor-1.0-20171221.172201-1.pom.sha1
│ ├── kor-1.0-20171221.172210-2.jar
│ ├── kor-1.0-20171221.172210-2.jar.md5
│ ├── kor-1.0-20171221.172210-2.jar.sha1
│ ├── kor-1.0-20171221.172210-2.pom
│ ├── kor-1.0-20171221.172210-2.pom.md5
│ ├── kor-1.0-20171221.172210-2.pom.sha1
│ ├── maven-metadata-remote.xml
│ ├── maven-metadata-remote.xml.sha1
│ ├── maven-metadata.xml
│ ├── maven-metadata.xml.md5
│ ├── maven-metadata.xml.sha1
│ └── resolver-status.properties
├── maven-metadata-remote.xml
├── maven-metadata-remote.xml.sha1
├── maven-metadata.xml
├── maven-metadata.xml.md5
├── maven-metadata.xml.sha1
└── resolver-status.properties
1 directory, 24 files
其中,kor-1.0-20171221.154128-1.pom 文件的内容是:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.easykotlin.plugin</groupId>
<artifactId>kor</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
新建Gradle 插件的执行逻辑的实现类
实现代码编写:
package com.easykotlin.kor
import org.gradle.api.Plugin
import org.gradle.api.Project
class KorPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
}
}
具体实现代码:
package com.easykotlin.kor
import org.gradle.api.Plugin
import org.gradle.api.Project
class KorPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.task("kor") << {
println("Hello, Kor !")
println(project.group.toString())
println(project.artifacts.toString())
}
project.task("nowabuild") << {
NowaBuildJava nb = new NowaBuildJava()
String projectDir = project.projectDir.absolutePath
println("projectDir = $projectDir")
nb.nowaBuild(projectDir)
}
}
}
kor.properties
implementation-class=com.easykotlin.kor.KorPlugin
build.gradle
group 'com.easykotlin.plugin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
// compile 'org.codehaus.groovy:groovy-all:2.4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
compile gradleApi()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri('/Users/jack/.m2/repository'))
}
}
}
然后,我们在另外项目 reakt 中使用刚才开发的插件:
build.gradle
buildscript {
ext {
kotlinVersion = '1.2.0'
springBootVersion = '2.0.0.M7'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath('com.easykotlin.plugin:kor:1.0-SNAPSHOT')
}
}
apply plugin: 'com.easykotlin.kor'
......
Gradle 插件工程源代码:
https://github.com/EasySpringBoot/kor
使用插件的工程源代码: