0
点赞
收藏
分享

微信扫一扫

自定义插件

  • 自定义插件 ->​​参考​​

interface GreetingPluginExtension {
Property<String> getMessage()
Property<String> getGreeter()
}

class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
def extension = project.extensions.create('greeting', GreetingPluginExtension)
project.task('hello') {
doLast {
println "${extension.message.get()} from ${extension.greeter.get()}"
}
}
}
}

apply plugin: GreetingPlugin

// Configure the extension using a DSL block
greeting {
message = 'Hi'
greeter = 'Gradle'
}

  • buildSrc 项目

# buildSrc 是 Gradle 默认的插件目录,编译 Gradle 的时候会自动识别这个目录,将其中的代码编译为插件

1. 在父工程中新建1个名为buildSrc的子模块
2. 打开父工程的settings.gradle,从 included modules 移除该子模块,重新构建,然后只保留 build.gradle 和 src/main 目录,其他全部删掉
3. 编写该子模块下的build.gradle
apply plugin: 'groovy' //必须
apply plugin: 'maven-publish'
dependencies {
implementation gradleApi() //必须
implementation localGroovy() //必须
}
repositories {
google()
jcenter()
mavenCentral() //必须
}
//把项目入口设置为src/main/groovy
sourceSets {
main {
groovy {
srcDir 'src/main/groovy'
}
}
}

  • 在如下目录下新建Text.groovy
  • 自定义插件_Gradle

package com.atguigu
import org.gradle.api.Plugin
import org.gradle.api.Project
class Text implements Plugin<Project>{
@Override
void apply(Project project) {
project.task("atguigu"){
doLast{
println("自定义atguigu插件")
}
}
}
}

  • 在如下目录下新建以.properties 结尾的文件
  • 自定义插件_Gradle_02

# 指明我们实现插件的全类名 
implementation-class=com.atguigu.Text

  • 在其他子模块中引入该插件

apply plugin:'com.atguigu.plugin'

  • 测试

# 命令行进入项目根路径
gradle atguigu



举报

相关推荐

0 条评论