## 14.1\. Directory creation 創建目錄
多個 task 依賴于現存的目錄,這是常見的情況。當然,你可以在 task 前添加?`mkdir`?但這不是好辦法,因為你只需要一次,卻要不斷重復代碼序列(看看 DRY principle )。好的做法是在 task 間使用 dependsOn 來重用 task 創建目錄
Example 14.1\. Directory creation with mkdir
build.gradle
~~~
def classesDir = new File('build/classes')
task resources << {
classesDir.mkdirs()
// do something
}
task compile(dependsOn: 'resources') << {
if (classesDir.isDirectory()) {
println 'The class directory exists. I can operate'
}
// do something
}
~~~
執行 gradle -q compile 輸出
~~~
> gradle -q compile
The class directory exists. I can operate
~~~
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#142-gradle-properties-and-system-properties-屬性)14.2\. Gradle properties and system properties 屬性
Gradle 提供了許多方式將屬性添加到您的構建中。 從 Gradle 啟動的 JVM,你可以使用 -D 命令行選項向它傳入一個系統屬性。 Gradle 命令的 -D 選項和 java 命令的 -D 選項有著同樣的效果。
此外,您也可以通過屬性文件直接向您的 project 對象添加屬性。您可以把一個 gradle.properties 文件放在 Gradle 的用戶主目錄 (默認如果不是 USER_HOME /.gradle 設置的話,就由 “GRADLE_USER_HOME” 環境變量定義 ) ,或您的項目目錄中。對于多項目構建,您可以將 gradle.properties 文件放在任何子項目的目錄中。通過 project 對象,可以訪問到 gradle.properties 里的屬性。用戶的主目錄中的屬性文件比項目目錄中的屬性文件更先被訪問到。
你還可以通過使用 -P 命令行選項來直接向您的 project 對象添加屬性。
也可以通過 特別命名的系統屬性 或者環境屬性把屬性設置 project 屬性。這個特性非常有用,通常出于安全原因,當你在持續集成的服務器沒有管理員權限時,對于設置屬性值你是不可見的。在這種情況下,你就不能使用 -P 選項,也不能修改系統級別的配置文件。正確的策略是,要改變你的持續集成配置建設工作,增加一個環境變量設置符合預期的模式。這不會對系統正常的用戶可見的。( Jenkins, Teamcity, 或者 Bamboo 是這些 CI (Continuous Integration 持續集成)服務商,提供這些功能)
如果環境變量名稱類似與 ORG_GRADLE_PROJECT_prop=somevalue, Gradle 將會設置 prop 屬性到你的 project 對象,值就是 somevalue 。Gradle 也提供了系統屬性的支持,但有著不同命名方式,類似的 org.gradle.project.prop
也可在 gradle.properties 設置系統屬性。如果此類文件中的屬性有一個?`systemProp.`?的前綴,像`systemProp.propName`,該屬性和它的值會被添加到系統屬性,且不帶此前綴。在多 project 構建中,除了在根項目之外的任何項目里的`systemProp.`?屬性集都將被忽略。也就是,只有根 project 的 gradle.properties 文件里的?`systemProp.`前綴的 屬性會被作為系統屬性。
Example 14.2\. Setting properties with a gradle.properties file
gradle.properties
~~~
gradlePropertiesProp=gradlePropertiesValue
sysProp=shouldBeOverWrittenBySysProp
envProjectProp=shouldBeOverWrittenByEnvProp
systemProp.system=systemValue
~~~
build.gradle
~~~
task printProps << {
println commandLineProjectProp
println gradlePropertiesProp
println systemProjectProp
println envProjectProp
println System.properties['system']
}
~~~
執行 gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps 輸出
~~~
> gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
commandLineProjectPropValue
gradlePropertiesValue
systemPropertyValue
envPropertyValue
systemValue
~~~
### [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#1421-checking-for-project-properties-檢查-project-屬性)14.2.1\. Checking for project properties 檢查 project 屬性
當你要使用一個變量時,你可以僅通過其名稱在構建腳本中訪問一個項目的屬性。如果此屬性不存在,則會引發異常,并且構建失敗。如果您的構建腳本依賴于一些可選屬性,而這些屬性用戶可能在比如 gradle.properties 文件中設置,您就需要在訪問它們之前先檢查它們是否存在。你可以通過使用方法 hasProperty('propertyName') 來進行檢查,它返回 true 或 false。
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#143-configuring-the-project-using-an-external-build-script-使用外部構建腳本配置項目)14.3\. Configuring the project using an external build script 使用外部構建腳本配置項目
你可以使用一個外部構建腳本配置當前 project 。所有的 Gradle 構建語言都可用在外部腳本。。您甚至可以在外部腳本中應用其他腳本。
Example 14.3\. Configuring the project using an external build script
build.gradle
~~~
apply from: 'other.gradle'
other.gradle
println "configuring $project"
task hello << {
println 'hello from other script'
}
~~~
執行 gradle -q hello 輸出
~~~
> gradle -q hello
configuring root project 'configureProjectUsingScript'
hello from other script
~~~
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#144-configuring-arbitrary-objects-配置任意對象)14.4\. Configuring arbitrary objects 配置任意對象
您可以用以下非常易理解的方式配置任意對象。
Example 14.4\. Configuring arbitrary objects
build.gradle
~~~
task configure << {
def pos = configure(new java.text.FieldPosition(10)) {
beginIndex = 1
endIndex = 5
}
println pos.beginIndex
println pos.endIndex
}
~~~
執行 gradle -q configure 輸出
~~~
> gradle -q configure
1
5
~~~
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#145-configuring-arbitrary-objects-using-an-external-script-使用外部腳本配置任意對象)14.5\. Configuring arbitrary objects using an external script 使用外部腳本配置任意對象
Example 14.5\. Configuring arbitrary objects using a script
build.gradle
~~~
task configure << {
def pos = new java.text.FieldPosition(10)
// Apply the script
apply from: 'other.gradle', to: pos
println pos.beginIndex
println pos.endIndex
}
~~~
other.gradle
執行 gradle -q configure 輸出
~~~
> gradle -q configure
1
5
~~~
## [](https://github.com/waylau/Gradle-2-User-Guide/blob/master/Chapter%2014.%20Tutorial%20-%20'This%20and%20That'%20%E6%95%99%E7%A8%8B-%E8%BF%99%E4%B8%AA%E9%82%A3%E4%B8%AA.md#146-caching-緩存)14.6\. Caching 緩存
為了提高響應速度,默認情況下 Gradle 會緩存所有已編譯的腳本。這包括所有構建腳本,初始化腳本和其他腳本。你第一次運行一個項目構建時, Gradle 會創建 .gradle 目錄,用于存放已編譯的腳本。下次你運行此構建時, 如果該腳本自它編譯后沒有被修改,Gradle 會使用這個已編譯的腳本。否則該腳本會重新編譯,并把最新版本存在緩存中。如果您通過 --recompile-scripts 選項運行 Gradle ,會丟棄緩存的腳本,然后重新編譯此腳本并將其存在緩存中。通過這種方式,您可以強制 Gradle 重新生成緩存
- 關于
- 第1章 Introduction 介紹
- 第2章 Overview 總覽
- 第3章 Tutorials 教程
- 第4章 Installing Gradle 安裝
- 第5章 Troubleshooting 問題解決
- 第6章 Build Script Basics 構建腳本的基礎識
- 第7章 Java Quickstart 快速開始 Java
- 第8章 Dependency Management Basics 依賴管理的基礎知識
- 第9章 Groovy Quickstart 快速開始 Groovy
- 第10章 Web Application Quickstart 快速開始 Web 應用
- 第11章 Using the Gradle Command-Line 使用 Gradle 命令行
- 第12章 Using the Gradle Graphical User Interface 使用 Gradle 圖形化用戶界面
- 第13章 Writing Build Scripts 編寫構建腳本
- 第14章 Tutorial - 'This and That' 教程-這個那個
- 第15章 More about Tasks 更多關于任務
- 第16章 Working With Files 跟文件工作
- 第17章 Using Ant from Gradle 從 Gradle 使用 Ant
- 第18章 Logging 日志.md
- 第19章 The Gradle Daemon 守護進程
- 第20章 The Build Environment 構建環境
- 第21章 Gradle Plugins 插件
- 第22章 Standard Gradle plugins 標準 Gradle 插件
- 附錄E Existing IDE Support and how to cope without it 支持的 IDE 以及如何應對沒有它