当前位置:网站首页>Gradle 笔记
Gradle 笔记
2022-07-02 03:00:00 【howeres】
Groovy 的闭包 (关键语法)
定义: 类似于 lambda 表达式, 传递时使用匿名内部类对象的方式
特点: 可以先声明,然后设置代理来执行
Closure closure = {
sayHello()
}
// 测试准备
class Foo {
void sayHello() {
println("Hello!!!")
}
}
def foo = new Foo()
// 开始测试, 可以看出 delegate 是一个成员变量, 而且类型是匿名的接口
closure.delegate = foo
closure()
- 调用特性:
- 可以外置 (闭包对象在最后一个参数时)
- 可以外置 + 省略括号 (仅有一个参数且为闭包时)
def running(Closure closure) {
println("start~")
closure()
println("end.")
}
running({
println("running...") })
// 闭包可以后置
running() {
println("running...")}
def caculate(Closure closure) {
def num1 = 10
def num2 = 15
closure(num1, num2)
}
caculate() {
k,v -> println("$k + $v = ${k+v}")}
// 括号可以省略, 也就是最熟悉的 Gradle 方式
caculate {
k,v -> println("$k + $v = ${k+v}") }
Gradle 的构建流程
- 生成一个
Settings对象,执行settings.gradle对这个对象进行配置 - 使用
Settings对象生成工程结构,创建Project对象 - 对所有
Project执行对应的build.gradle进行配置
脚本文件 build.gradle 的执行方式
总结一下,build.gradle 就是一个 Groovy 格式的脚本文件, 构建的时候会顺序执行, 但是打开 build.gradle 可能还是一头雾水, 一个个字符和大括号?以最长使用的 dependencies 举例
dependencies {
// This dependency is found on compile classpath of this component and consumers.
implementation 'com.google.guava:guava:26.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
同时 implementation 也可以这样写
implementation('com.google.guava:guava:26.0-jre')
implementation 其实就是函数. Groovy 中函数调用可以使用空格加参数的形式调用
void foo(String params1, int param2) {
println("param1 = $params1, param2 = $param2")
}
foo "hello", 996
Gradle 和 Groovy 的关系
Gradle 支持 Kotlin 或 Groovy 两种语言编写, 两者的关系为实现关系. Groovy 是 Gradle 的一种 DSL ( Domain Specific Language i.e., 领域专用语言)
Gradle 的 DSL 专门用于配置项目的构建不能用于其他工作. 语法还是 Groovy 自身的语法, 但调用只能依靠 Gradle 规定的 API 方式或者特定的一套规范. 强调一个 Specific 专用 (约定)
与 DSL (HTML、Makefile、LaTeX、AWK) 相对应的是可以做任何工作的通用语言 (Java 、C/C++)
边栏推荐
猜你喜欢

Which brand of sports headset is better? Bluetooth headset suitable for sports

Deployment practice and problem solving of dash application development environment based on jupyter Lab

Baohong industry | 6 financial management models at different stages of life

QT implementation interface jump

CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes

2022-2028 global encryption software industry research and trend analysis report

Après le mariage

高并发场景下缓存处理方案

STM32__ 05 - PWM controlled DC motor

Share the basic knowledge of a common Hongmeng application
随机推荐
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
Jvm-01 (phased learning)
3124. Word list
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
Qualcomm platform wifi-- WPA_ supplicant issue
es面试题
2022-2028 global wood vacuum coating machine industry research and trend analysis report
Cache processing scheme in high concurrency scenario
Questions d'entrevue
QT uses sqllite
【做题打卡】集成每日5题分享(第二期)
Deployment practice and problem solving of dash application development environment based on jupyter Lab
Connected block template and variants (4 questions in total)
The video number will not be allowed to be put on the shelves of "0 yuan goods" in the live broadcasting room?
多线程查询,效率翻倍
[staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
Use the open source project [banner] to achieve the effect of rotating pictures (with dots)
2022低压电工考试题模拟考试题库模拟考试平台操作
What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
2022-2028 global aluminum beverage can coating industry research and trend analysis report