init.gradle
簡介
init.gradle文件在build開始之前執行,可以在這個文件配置一些你想預先加載的操作,例如:build日誌輸出,機器信息(比如jdk安裝目錄),build時所必需的個人信息(比如倉庫或者數據庫的認證信息)
啓用init.gradle文件的方法:
1、在命令行指定文件,例如:gradle –init-script yourdir/init.gradle -q taskName.你可以多次輸入此命令來指定多個init文件
2、把init.gradle文件放到USER_HOME/.gradle/ 目錄下.
3、把以.gradle結尾的文件放到USER_HOME/.gradle/init.d/ 目錄下.
4、把以.gradle結尾的文件放到GRADLE_HOME/init.d/ 目錄下.
如果存在上面的4種方式的2種以上,gradle會按上面的1-4序號依次執行這些文件,如果給定目錄下存在多個init腳本,會按拼音a-z順序執行這些腳本
示例說明
在build執行之前給所有的項目制定maven本地庫,同時在 build.gradle文件指定了maven的倉庫中心。
build.gradle
repositories { mavenCentral() } task showRepos << { println "All repos:" println repositories.collect { it.name } }
init.gradle
allprojects { repositories { mavenLocal() } }
在命令行輸入命令:gradle –init-script init.gradle -q showRepos
執行結果
> gradle --init-script init.gradle -q showRepos All repos: [MavenLocal, MavenRepo]
settings.gradle
其他網址
settings.gradle作用就是用於多項目構建。多項目構成:allProjects = root項目+各子項目
寫法
寫法1:
rootProject.name = 'proj_root'
include 'proj_1'
include 'proj_2'
include 'group_A:proj_3_1'
include 'group_B:proj_4_2'
寫法2:
rootProject.name = 'proj_root'
include 'proj_1', 'proj_2', 'group_A:proj_3_1', 'group_B:proj_4_2'
自定義
假設settings.gradle這樣寫
rootProject.name = 'A'
include 'core', 'web', 'mobile'
運行gradle projects可得到項目結構
Root project 'A'
+--- Project ':core'
+--- Project ':mobile'
\--- Project ':web'
最終的文件以及目錄結構如下所示:
A
--settings.gradle
--build.gradle
--core
--build.gradle
--web
--build.gradle
--mobile
--build.gradle
如果不喜歡這種默認的結構,也可以按照如下方式定義子項目的名稱和物理目錄結構:
settings.gradle
rootProject.name = 'A'
include(':core')
project(':core').projectDir = new File(settingsDir, 'core-xxx')
include(':web')
project(':web').projectDir = new File(settingsDir, 'web-xxx')
include(':mobile')
project(':mobile').projectDir = new File(settingsDir, 'mobile-xxx')
這個例子中,子項目core實際上對應的物理目錄爲A/core-xxx,web實際上對應的是A/web-xxx,mobile也類似。
雖然我們更改了子項目的物理目錄結構,不過由於我們在build.gradle中使用的是類似 “ :<SubProject>”的方式訪問對應的子項目,所以目錄結構的改變,對我們Gradle的構建腳本並不會產生影響。
此時執行gradle projects,結果仍然是
Root project 'A'
+--- Project ':core'
+--- Project ':mobile'
\--- Project ':web'
gradle.properties
其他網址
用途
某些設置如 JVM 內存設置,Java home,守護進程的開/關,可以統一管理。
這些配置將會按以下順序被應用(以防在多個地方都有配置時只有最後一個 生效):
- 位於項目構建目錄的gradle.properties。
- 位於gradle 用戶主目錄的gradle.properties。
- 系統屬性,例如當在命令行中使用 -Dsome.property 時。