1. 程式人生 > >Gradle 使用命令列操作Gradle

Gradle 使用命令列操作Gradle

USAGE: gradle [option...] [task...] -?, -h, --幫助顯示這個幫助資訊。 -a, --no-rebuild Do not rebuild project dependencies. --all 顯示task之間的依賴關係。眾所周知,使用gradle tasks可以列出當前所有可被使用的task,但是並沒有顯示task之間的依賴關係。我們可以加上--all來顯示 task的依賴關係。$ gradle tasks --all -b, 使用指定的gradle檔案呼叫task。預設情況下,如果你呼叫gradle task,那麼首先會尋找當前目錄下的build.gradle檔案,以及根據settings.gradle中的配置尋找子專案的build.gradle。但是有時候我們想指定使用某個gradle檔案,那麼可以使用-
b
命令。 比如當前目錄有個子目錄subproject,裡面有個叫hello.gradle。$ gradle -b subproject/hello.gradle helloWorld -c, --settings-file Specifies the settings file. --configure-on-demand Only relevant projects are configured in this build run. This means faster build for large multi-project builds. [incubating] --console Specifies which type of console output to generate. Values are 'plain', 'auto' (default) or 'rich'. --continue 繼續執行task而忽略前面失敗的task。
預設情況下,如果有某個task失敗,後續的task就不會繼續執行。但是有時候我們想執行所有的task來一次性得到所有的構建錯誤,那麼我們可以使用--continue命令。使用--continue命令後即使遇到某些task失敗也不會停止後續task的執行。但是需要注意的是如果某個task失敗了,那麼依賴於這個task的其他task依舊不會執行,因為這會帶來不安全的因素。 -D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue). -d, --debug Log in debug mode (includes normal stacktrace). --daemon Uses the Gradle daemon to run the build. Starts the daemon if not running. dependencyInsight --檢視指定dependency的依賴情況。
假如我想檢視專案中有沒有引入junit,那些階段引入了junit,那麼可以使用dependecyInsight來檢視。$ gradle dependencyInsight --dependency junit --configuration testCompile 注意dependencyInsight預設只會檢視compile階段的依賴,如果要檢視其他階段可以使用--configuration來指定。 --foreground Starts the Gradle daemon in the foreground. [incubating] -g, --gradle-user-home Specifies the gradle user home directory. --gui Launches the Gradle GUI. -I, --init-script Specifies an initialization script. -i, --info Set log level to info. -m, --試執行build。如果你想知道某個task執行時那些task會被一起執行,但是你又不想真正的執行這些task,可以使用-m來試執行。$ gradle -m build --max-workers Configure the number of concurrent workers Gradle is allowed to use. [incubating] --no-color Do not use color in the console output. [deprecated - use --console=plain instead] --no-daemon Do not use the Gradle daemon to run the build. --offline The build should operate without accessing network resources. -P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue). -p, --使用指定的專案目錄呼叫task。前面已經說過,執行gradle的task預設會在當前目錄尋找build.gradle及settings.gradle檔案。如果我們想在任何地方執行某個專案的task,那麼可以使用-p來指定使用的專案。gradle -q -p learnGradle helloWorld 這條命令是呼叫learnGradle這個專案下的helloWorld task。 --parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating] --parallel-threads Build projects in parallel, using the specified number of executor threads. [deprecated - Please use --parallel, optionally in conjuncti on with --max-workers.] [incubating] --profile --使用--profile命令列可以產生build執行時間的報告。該報告儲存在build/report/profile目錄,名稱為build執行的時間。 $ gradle build --profile --project-cache-dir Specifies the project-specific cache directory. Defaults to .gradle in the root project directory. -q, --靜日誌錯誤. -q dependencies 檢視指定階段的依賴關係。使用gradle dependencies 可以檢視專案中包的依賴關係。不過是列出了所有階段的依賴,如果專案中依賴複雜的話看起來有點頭痛。$ gradle -q dependencies -q dependencies --configuration --使用--configuration來檢視指定階段的依賴情況。 $ gradle -q dependencies --configuration testCompile 使用gradle -q dependencies --configuration testCompile可以只檢視testComiple的依賴。 --recompile-scripts Force build script recompiling. --refresh-dependencies Refresh the state of dependencies. --rerun-tasks Ignore previously cached task results. -S, --使用-S(或--full-stacktrace)來輸出全部堆疊資訊,不過一般不推薦這樣做,因為gradle是基於groovy語言,而groovy作為一門動態語言可能會輸出與你的錯誤程式碼毫不相關的資訊。 -s, --堆疊跟蹤。如果執行gradle task失敗時,如果想得到更詳細的錯誤資訊,那麼就可以使用-s(或--stacktrace)來輸出詳細的錯誤堆疊。 --stop Stops the Gradle daemon if it is running. -t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating] -u, --no-search-upward Don't search in parent folders for a settings.gradle file. -v, --version Print version info. -x, --跳過指定的測試。如果你在執行build的時候想跳過test task,那麼可以使用-x命令。