展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

自定义插件

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
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 结尾的文件
# 指明我们实现插件的全类名
implementation-class=com.atguigu.Text
  • 在其他子模块中引入该插件
apply plugin:'com.atguigu.plugin'
  • 测试
# 命令行进入项目根路径
gradle atguigu
posted @   DogLeftover  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示