## 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目錄用來緩存編譯的腳本。只有變換的時候,才會重新生成緩存的腳本。
- 前言
- 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