## 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目录
![](https://box.kancloud.cn/2016-01-07_568e46678dbda.jpg)
## 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目录用来缓存编译的脚本。只有变换的时候,才会重新生成缓存的脚本。
- 前言
- gradle学习(1)-helloworld
- gradle学习(2)-基础语法
- gradle学习(3)-基础认识
- gradle学习(4)-构建java项目
- gradle学习(5)-创建eclipse项目
- gradle学习(6)-依赖管理
- gradle学习(7)-groovy
- gradle学习(8)-gradle的命令行
- gradle学习(9)-获取build相关信息
- gradle学习(10)-gui
- gradle学习(11)-编写构建脚本
- gradle学习(12)-groovy一些基础语法
- gradle学习(13)-有的没的
- gradle学习(14)-任务
- gradle学习(15)-任务
- gradle学习(16)-操作文件
- gradle学习(17)-被合并的ant
- gradle学习(18)-ant的属性
- gradle学习(19)-log系统
- gradle学习(20)-详解java插件
- gradle学习(21)-在eclipse中构建java项目
- gradle复习(1)-2种定义任务方式的区别
- gradle复习(2)-eclipse中添加依赖jar包
- gradle复习(3)-在gradle项目中使用TestNG
- gradle复习(4)-Cannot find System Java Compiler
- gradle复习(5)-Test remote debug
- gradle复习(6)-深入Jacoco
- gradle复习(7)-深入Jacoco
- gradle复习(8)-Task中行为
- gradle学习(22)-Sonar
- gradle学习(23)-Sonar runner