通常情况下,创建目录使用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'
}
}
qianhuis-Mac-mini:0111 qianhui$ gradle -q compile
The class directory exists.I canoperate
然后就会在该目录下生成build/classes目录
有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.
因为上面的commandLineProjectProp 参数没有定义。所以这个时候可以通过-P命令添加属性。
gradle -q printProps -PcommandLineProjectProp=thisisacommandlineprop
输出如下
qianhuis-Mac-mini:0111 qianhui$ gradle -q printProps -PcommandLineProjectProp=thisisacommandlineprop
thisisacommandlineprop
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
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.
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
gradle 允许你包含其他构建的脚本,比如一些模版脚本,这样的扩展性挺好。
定义一个模版脚本:template.gradle
task hello <<{
println "this a template"
}
apply from:'template.gradle'
执行任务hello
qianhuis-Mac-mini:0111 qianhui$ gradle -q hello
this a template
task configure <<{
def pos = configure(new java.text.FieldPosition(10)){
beginIndex = 1
endIndex = 5
}
println pos.beginIndex
println pos.endIndex
}
template.gradle修改如下:
beginIndex = 1
endIndex = 5
task configure <<{
def pos = new java.text.FieldPosition(10)
apply from:'template.gradle',to:pos
println pos.beginIndex
println pos.endIndex
}
qianhuis-Mac-mini:0111 qianhui$ gradle -q configure
1
5
gradle创建了一个.gradle目录用来缓存编译的脚本。只有变换的时候,才会重新生成缓存的脚本。