Skip to content

Commit

Permalink
yoyiyi
Browse files Browse the repository at this point in the history
  • Loading branch information
zzq committed Jan 3, 2021
1 parent ad440b3 commit 9c48ad7
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion Gradle/002.Gradle基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,100 @@ plugins {

#### 7.5.1 build.gradle 编写

```groovy
//1.简单的自定义插件
apply plugin:CustomPlugin
class CustomPlugin implements Plugin<Project>{
@Override
void apply(Project target) {
target.task('CustomPluginTask'){
doLast{
println('自定义插件')
}
}
}
}
//gradlew.bat CustomPlugin
//2.自定义插件扩展
apply plugin:CustomPlugin
class CustomPluginExtension{
String msg = 'from CustomPlugin'
}
class CustomPlugin implements Plugin<Project>{
@Override
void apply(Project target) {
def ext = target.extensions.create('custom', CustomPluginExtension)
target.task('CustomPlugin'){
doLast{
println('自定义插件')
println ext.msg
}
}
}
}
```



#### 7.5.2 buildSrc 工程项目中编写

#### 7.5.3 独立项目中编写
在根目录创建 **buildSrc/src/main/groovy/MyCustomPlugin.groovy** 文件

```groovy
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyCustomPlugin implements Plugin<Project>{
@Override
void apply(Project target) {
target.task('MyCustomPlugin'){
doLast{
println('我的自定义插件')
}
}
}
}
apply plugin:MyCustomPlugin
```

#### 7.5.3 独立项目中编写

buildSrc 只能在自己项目中使用,独立项目使用要配置

```groovy
//build.gradle
apply plugin: 'groovy'
dependencies {
compile gradleApi()
compile localGroovy()
}
//创建插件
package com.test.groovy
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyCustomPlugin2 implements Plugin<Project>{
@Override
void apply(Project target) {
target.task('MyCustomPlugin2'){
doLast{
println('MyCustomPlugin2 自定义插件')
}
}
}
}
```

0 comments on commit 9c48ad7

Please sign in to comment.