Groovy 简单使用笔记
Groovy 是一种基于 JVM 的语言.
其语法特点类似于Python, Ruby 和 Smalltalk.
Groovy 代码能够与 Java 代码很好地结合, 同时也能用于扩展现有代码.
由于其运行在 JVM 上的特性, Groovy 也可以使用其他非 Java 语言编写的库
简单使用 Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| version = 1.0 description = 'demo'
ext.project_var0 = 'value' ext { project_var1 = 'value' project_var2 = 'value' }
task _01 { println "------------------------Task 01" }
task _02 { println "------------------------Task 02" doLast { for (i in 0..<3){ print i + ' ' } }
doFirst { def s = "hello" println "First action: $s" } }
tasks.create(name: '_03') { doFirst { println "tasks属性的类型: ${tasks.class}"
tasks.each {e -> println e } } }
tasks.create(name: '_04', dependsOn: '_02') { doFirst { println "Task 04 依赖 Task 02" } }
tasks.create(name: '_05', type: Copy) { from 'build.gradle' into 'back' }
task _06(dependsOn: '_02', type: DefaultTask)
class HelloTask extends DefaultTask { @Internal def message = 'Hello Task'
@TaskAction def hello() { println "HelloTask -> hello(): $message" }
def info() { println "HelloTask -> info(): $message" } }
task hello(type: HelloTask) { doFirst { info() } }
task hello2(type: HelloTask) { message = 'HELLO2' }
task myTask(type: test.MyTask)
task myTask2(type: test.MyTask) { file = file('dist.txt') doLast { show() } }
apply plugin: 'java'
task compile(type: JavaCompile) {
source = fileTree('src/main/java') classpath = sourceSets.main.compileClasspath destinationDirectory = file('build/classes/main') options.fork = true options.incremental = true }
task run(type: JavaExec, dependsOn: 'compile') {
classpath = sourceSets.main.runtimeClasspath mainClass = 'Test' }
task showProps { description = 'showProps'
ext.task_var0 = 'value' ext { task_var1 = 'value' task_var2 = 'value' }
doFirst { println version println description println project.description } }
task fileContentCopy { def sourceTxt = fileTree("source") def dest = file('dist.txt')
inputs.dir sourceTxt outputs.file dest
doLast { dest.withPrintWriter {writer -> sourceTxt.each {s -> writer.write(s.text) } } }
}
sourceSets { xModel }
repositories {
mavenLocal()
maven{url 'https://maven.aliyun.com/nexus/content/groups/public/'}
}
configurations { xDependence
implementation compileOnly runtimeOnly testImplementation testCompileOnly testRuntimeOnly archives }
dependencies { xDependence group: 'commons-logging', name: 'commons-logging', version: '1.2' xDependence 'com.baomidou:mybatis-plus-boot-starter:3.5.1'
xDependence (group: 'net.coobird', name: 'thumbnailator', version: '0.4.17') { } xDependence ('org.jetbrains:annotations:23.0.0') { }
xDependence 'com.alibaba:fastjson:1.2.80', 'com.google.guava:guava:31.1-jre' }
task showDependences { doFirst { println configurations.xDependence.asPath } }
class HelloPlugin implements Plugin<Project> {
void apply(Project project) { project.extensions.creat("user", User)
project.task('showUser') { doLast { println "用户名: ${project.user.name}" } } } } final class User { String name = "" }
apply plugin: HelloPlugin user.name = "Patrick"
apply plugin: MyPlugin
|