gradle学习(13)-有的没的

1.用脚本创建目录

通常情况下,创建目录使用mkdir来创建目录,但是有一种更好的方式来做到,避免每次都手动创建。定义一个任务来创建目录,然后用dependsOn来依赖该任务,这有可以在需要的时候创建目录。

def classesDir = new File("build/classes")
task resources << {
        classesDir.mkdirs()
}

task compile(dependsOn:"resources")<<{
        if(classesDir.isDirectory()){
        println "The class directory exists.I canoperate"
}
}

执行compile任务

qianhuis-Mac-mini:0111 qianhui$ gradle -q compile
The class directory exists.I canoperate

然后就会在该目录下生成build/classes目录

2.gradle属性和系统属性

有3种方式添加属性,命令行中的-P和-D参数,以及属性文件gradle.properties

通过命令行中的-P命令传递属性。首先build.gradle定义属性

task printProps <<{
        println commandLineProjectProp

}

在命令行中执行该命令,如果不带参数会出错。


qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps

FAILURE: Build failed with an exception.

  • Where:
    Build file "/Users/qianhui/Documents/Developer/gradle_project/0111/build.gradle" line: 2

  • What went wrong:
    Execution failed for task ":printProps".

    Could not find property "commandLineProjectProp" on task ":printProps".

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

    
    

    gradle -q printProps -PcommandLineProjectProp=thisisacommandlineprop

    输出如下
    

    qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -PcommandLineProjectProp=thisisacommandlineprop
    thisisacommandlineprop

    上面的写法一定要注意,-P后面也可以有空格,执行的效果是一样的。
    

    qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp=thisisacommandlineprop
    thisisacommandlineprop

    但是等号(=)两端不能有空格,且属性值也不能有空格。但是如果你用单引号 或者双引号包裹的话可以有空格。
    

    qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp =this is acommandlineprop

FAILURE: Build failed with an exception.

  • What went wrong:
    Task "=this" not found in root project "0111".

  • Try:
    Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

用单引号或双引号包裹

qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp="this is a commandline prop"
this is a commandline prop


qianhuis-Mac-mini:0111 qianhui$ gradle -q  printProps -P commandLineProjectProp="this is a commandline prop"
this is a commandline prop

**-D添加gradle属性。**

build.gradle文件如下

如果这个时候我们还想刚才那样执行命令的话,会报错

qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp="this is a commandline prop"
this is a commandline prop

FAILURE: Build failed with an exception.

  • Where:
    Build file "/Users/qianhui/Documents/Developer/gradle_project/0111/build.gradle" line: 3

  • What went wrong:
    Execution failed for task ":printProps".

    Could not find property "systemProjectProp" on task ":printProps".

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

说明build.gradle定义的systemProjectProp不存在,这个时候就需要用-D来添加gradle属性

qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp="this is a commandline prop" -D org.gradle.project.systemProjectProp="this a gradle property"
this is a commandline prop
this a gradle property

**gradle.properties文件添加属性**

build.gradle文件添加一个属性

task printProps <<{

    println commandLineProjectProp
    println systemProjectProp
    println gradleFileProperties

}

这个时候执行命令,会报错

qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp="this is a commandline prop" -D org.gradle.project.systemProjectProp="this a gradle property"
this is a commandline prop
this a gradle property

FAILURE: Build failed with an exception.

  • Where:
    Build file "/Users/qianhui/Documents/Developer/gradle_project/0111/build.gradle" line: 4

  • What went wrong:
    Execution failed for task ":printProps".

    Could not find property "gradleFileProperties" on task ":printProps".

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

    在gradle.properties 文件中添加属性
    

    gradleFileProperties = gradleFileProperties

    这个时候执行命令
    

    qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -P commandLineProjectProp="this is a commandline prop" -D org.gradle.project.systemProjectProp="this a gradle property"
    this is a commandline prop
    this a gradle property
    gradleFileProperties

    ## 3.包含其他构建脚本
    

gradle 允许你包含其他构建的脚本,比如一些模版脚本,这样的扩展性挺好。

定义一个模版脚本:template.gradle

task hello <<{
        println "this a template"
}

然后在build.gradle脚本中包含改脚本

apply from:"template.gradle"  

执行任务hello

qianhuis-Mac-mini:0111 qianhui$ gradle -q hello
this a template

4.自定义对象

task configure <<{
        def pos = configure(new java.text.FieldPosition(10)){
                beginIndex = 1
                endIndex = 5
        }
        println pos.beginIndex
        println pos.endIndex
}

5.使用外部的脚本自定义对象

template.gradle修改如下:

beginIndex = 1
endIndex = 5

build.gradle修改如下

task configure <<{
        def pos = new java.text.FieldPosition(10)
        apply from:"template.gradle",to:pos
        println pos.beginIndex
        println pos.endIndex
}

其中的apply from:"template.gradle",to :pos将template的属性注入到pos对象中。执行命令后,输出是一样的。

qianhuis-Mac-mini:0111 qianhui$ gradle -q configure
1
5

6.缓存

gradle创建了一个.gradle目录用来缓存编译的脚本。只有变换的时候,才会重新生成缓存的脚本。

文章导航