gradle规则

mac 安装 gradle
brew install gradle
gradle 第一个程序 后辍为 .gradle

 task allen{
    doLast{
        println 'fuck gradle!'
    }
}
 liuhailongdeAir:Desktop liuhailong$ gradle -q allen
fuck gradle!
liuhailongdeAir:Desktop liuhailong$ gradle  allen

> Task :allen
fuck gradle!

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
liuhailongdeAir:Desktop liuhailong$ 

gradle规则
生成 wrapper

 liuhailongdeAir:testdradle liuhailong$ ls
build.gradle
liuhailongdeAir:testdradle liuhailong$ gradle  allen

> Task :allen
fuck gradle!

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ gradle wrapper

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ ls
build.gradle    gradle      gradlew     gradlew.bat
liuhailongdeAir:testdradle liuhailong$ tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

2 directories, 5 files
liuhailongdeAir:testdradle liuhailong$ 

日志

 //apply plugin: 'java'  //加载插件

task allen << {
    println '输出一段日志信息apply plugin:java加载插件'

    logger.quiet('quiet日志信息.')
    logger.error('error日志信息.')
    logger.warn('warn日志信息.')
    logger.lifecycle('lifecycle日志信息.')
    logger.info('info日志信息.')
    logger.debug('debug日志信息.')
}
 liuhailongdeAir:testdradle liuhailong$ gradle -s -S -i -q  allen
输出一段日志信息apply plugin:java加载插件
quiet日志信息.
error日志信息.
liuhailongdeAir:testdradle liuhailong$ 

字符串

task printStringClass << {
    def str1 = '单引号'
    def str2 = "双引号"

    println "单引号定义的字符串类型:"+str1.getClass().name
    println "双引号定义的字符串类型:"+str2.getClass().name
}

task printStringVar << {
    def name = "张三"

    println '单引号的变量计算:${name}'
    println "单引号的变量计算:$name"
    println "单引号的变量计算:${1+1}"
}
liuhailongdeAir:testdradle liuhailong$ gradle printStringVar

> Task :printStringVar
单引号的变量计算:${name}
单引号的变量计算:张三
单引号的变量计算:2

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ gradle printStringClass

> Task :printStringClass
单引号定义的字符串类型:java.lang.String
双引号定义的字符串类型:java.lang.String

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ 

集合 与闭包

task printList << {
    def numList =[1,2,3,4,5,6];
    println numList.getClass().name

    println numList[1]//访问第二个元素
    println numList[-1]//访问最后一个元素
    println numList[-2]//访问倒数第二个元素
    println numList[1..3]//f访问第二个到第四个元素
 //groovy 支持闭包 也就是支持参数为代码块
//一般写法 numList.each (
//{
//println it
//}
//)
//numList.each (){
//println it
//}
简写
    numList.each {
        println it
    }
}

task printlnMap << {
    def map1 =['width':1024,'height':768]
    println map1.getClass().name

    println map1['width']
    println map1.height

    map1.each {
        println "Key:${it.key},Value:${it.value}"
    }
}
liuhailongdeAir:testdradle liuhailong$ gradle printList printlnMap

> Task :printList
java.util.ArrayList
2
6
5
[2, 3, 4]
1
2
3
4
5
6

> Task :printlnMap
java.util.LinkedHashMap
1024
768
Key:width,Value:1024
Key:height,Value:768

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
liuhailongdeAir:testdradle liuhailong$ 

函数与返回值

task invokeMethod << {
    method1(1,2)
    method1 1,2
 println "最后一句默认为返回值也就是 return可以不写"
}

def method1(int a,int b){
    println a+b
}

task printMethodReturn << {
    def add1 = method2 1,2
    def add2 = method2 5,3
    println "add1:${add1},add2:${add2}"
}

def method2(int a,int b){
    if(a>b){
        a
    }else{
        b
    }
}
liuhailongdeAir:testdradle liuhailong$ gradle invokeMethod printMethodReturn

> Task :invokeMethod
3
3
最后一句默认为返回值也就是 return可以不写

> Task :printMethodReturn
add1:2,add2:5

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
liuhailongdeAir:testdradle liuhailong$ 

javabean 闭包(关键字Closure) 闭包委托

 task helloJavaBean << {
Person2 p = new Person2()

println "名字是:${p.name}"
p.name = "张三"
println "名字是:${p.name}"
println "年龄是:${p.age}"
}

class Person2 {
private String name

public int getAge(){
12
}
}

task helloClosure << {
    //使用我们自定义的闭包
    customEach {
        println it
    }

    //多个参数
    eachMap {k,v ->
        println "${k} is ${v}"
    }
}

def customEach(closure){
    //模拟一个有10个元素的集合,开始迭代
    for(int i in 1..10){
        closure(i)
    }
}

def eachMap(closure){
    def map1 = ["name":"张三","age":18]
    map1.each {
        closure(it.key,it.value)
    }
}
task helloDelegate << {
    new Delegate().test {
        println "thisObject:${thisObject.getClass()}"
        println "owner:${owner.getClass()}"
        println "delegate:${delegate.getClass()}"
        method1()
        it.method1()
    }
}

def method1(){
    println "Context this:${this.getClass()} in root"
    println "method1 in root"
}
class Delegate {
    def method1(){
        println "Delegate this:${this.getClass()} in Delegate"
        println "method1 in Delegate"
    }

    def test(Closure<Delegate> closure){
        closure(this)
    }
}

task configClosure << {
    person {
        personName = "张三"
        personAge = 20
        dumpPerson()
    }
}

class Person3 {
    String personName
    int personAge

    def dumpPerson(){
        println "name is ${personName},age is ${personAge}"
    }
}

def person(Closure<Person3> closure){
    Person3 p = new Person3();
    closure.delegate = p
    //委托模式优先
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    closure(p)
}
 liuhailongdeAir:testdradle liuhailong$ gradle  helloJavaBean  helloClosure configClosure

> Task :helloJavaBean
名字是:null
名字是:张三
年龄是:12

> Task :helloClosure
1
2
3
4
5
6
7
8
9
10
name is 张三
age is 18

> Task :configClosure
name is 张三,age is 20

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
3 actionable tasks: 3 executed
liuhailongdeAir:testdradle liuhailong$ 

Task为 Project 对象的一个函数原型为
create(String name,Closure configureCloure);

 task customTask1 {
    doFirst {
        println 'customTask1:doFirst'
    }
    doLast {
        println 'customTask1:doLast'
    }
}

tasks.create("customTask2") {
    doFirst {
        println 'customTask2:doFirst'
    }
    doLast {
        println 'customTask2:doLast'
    }
}
 liuhailongdeAir:testdradle liuhailong$ gradle customTask1 customTask2

> Task :customTask1
customTask1:doFirst
customTask1:doLast

> Task :customTask2
customTask2:doFirst
customTask2:doLast

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
liuhailongdeAir:testdradle liuhailong$ 

任务依赖关键字 dependsOn

 task ex35Hello << {
    println 'hello'
}

task ex35World << {
    println 'world'
}

task ex35Main(dependsOn: ex35Hello) {
    doLast {
        println 'main'
    }
}

task ex35MultiTask {
    dependsOn ex35Hello,ex35World
    doLast {
        println 'multiTask'
    }
}
liuhailongdeAir:testdradle liuhailong$ gradle ex35Hello  ex35World ex35Main ex35MultiTask

> Task :ex35Hello
hello

> Task :ex35World
world

> Task :ex35Main
main

> Task :ex35MultiTask
multiTask

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
liuhailongdeAir:testdradle liuhailong$ 

任务抽象方法执行顺序
https://blog.csdn.net/u012107143/article/details/78723106
task 其时是一个任务类型有 api 可以对其操作
打印属性

    task ex36Hello << {
    println 'dowLast1'
}

ex36Hello.doFirst {
    println 'dowFirst'
}

ex36Hello.doLast {
    println project.hasProperty('ex36Hello')
    println 'dowLast2'
}
liuhailongdeAir:testdradle liuhailong$ gradle ex36Hello

> Task :ex36Hello
dowFirst
dowLast1
true
dowLast2

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ 
添加属性 通过 ext 实现 只要某个对象中使用了ext 那这对象就会有ext添加的属性
    apply plugin: "java"

//自定义一个Project的属性
ext.age = 18

//通过代码块同时自定义多个属性
ext {
    phone = 1334512
    address = ''
}

sourceSets.all {
    ext.resourcesDir = null
}

sourceSets {
    main {
        resourcesDir = 'main/res'
    }
    test {
        resourcesDir = 'test/res'
    }
}

task ex37CustomProperty << {
    println "年龄是:${age}"
    println "电话是:${phone}"
    println "地址是:${address}"

    sourceSets.each {
        println "${it.name}的resourcesDir是:${it.resourcesDir}"
    }
}
///
    liuhailongdeAir:testdradle liuhailong$ gradle ex37CustomProperty

> Task :ex37CustomProperty
年龄是:18
电话是:1334512
地址是:
main的resourcesDir是:main/res
test的resourcesDir是:test/res

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
liuhailongdeAir:testdradle liuhailong$ 

转载于:https://blog.51cto.com/haidragon/2173553

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值