命令學習比較枯燥,所有是例子~html
假設你的本地gradle已經安裝配置完成。沒有安裝配置的,能夠參考 gradle安裝java
C:\Users\yueling.DANGDANG>gradle -v
------------------------------------------------------------ Gradle 4.5.1 ------------------------------------------------------------
Build time: 2018-02-05 13:22:49 UTC
Revision: 37007e1c012001ff09973e0bd095139239ecd3b3
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_73 (Oracle Corporation 25.73-b02) OS: Windows 7 6.1 amd64 C:\Users\yueling.DANGDANG>
gradle [taskName...] [--option-name...]
gradlew是包裝器,自動下載包裝裏定義好的gradle 版本,保證編譯環境統一,gradle 是用本地的gradle。因此./gradlew、gradle通用,不神祕,使用更方便一些,能夠使用gradle互相替換。其中gradlew的內容能夠在項目的目錄下查看。linux
gradle test deploy
gradle --console=plain test
gradle test --console=plain
記憶技巧:長格式的選項 表示啓用特性的都具備反向操做(關閉特性),關閉特性長選項格式在啓用特性前綴no-web
--build-cache --configure-on-demand --daemon --parallel --scan --no-build-cache --no-configure-on-demand --no-daemon --no-parallel --no-scan
打開控制檯,apache
command_Parameters | introduction |
---|---|
-?, -h, –help | 展現幫助信息 |
-a, –no-rebuild | 忽略構建」項目依賴」,[廢棄] |
-b, –build-file | 使用用-b參數將會忽略setting文件,用指定的.gradle文件進行構建(默認狀況下首先會尋找當前目錄下的 build.gradle文件或依據settings.gradle中的配置尋找子項目中的build.gradle) |
–build-cache | 支持gradle緩存。Gradle將試圖重用之前的版本的輸出 |
-c, –settings-file | 執行指定的*.gralde文件(默認settings.gradle) |
–configure-on-demand | 配置必要的項目。Gradle將試圖減小大型多項目構建的配置時間 |
–console | 指定控制檯輸出類型,可選值有’plain’、’auto’(默認)、’rich’、’verbose’。plain 是生成普通的文本,該選項禁止全部顏色和富文本輸出;auto當構建程序與控制檯相關聯時啓動顏色和富文本輸出,或者不關聯時生成普通文本;rich 啓動顏色和富文本輸出,忽略構建程序是否關聯了控制檯,若是沒有關聯構建輸出將輸出 ANSI 控制字符來生產富文本輸出;verbose會輸出更多的詳細信息(感受很亂,調試問題可能會用到) |
–continue | 構建時遇到錯誤忽略報錯,繼續構建,默認報錯後終止構建 |
-D, –system-prop | -D屬性會被傳送給啓動Gradle的jvm,做爲一個系統屬性被jvm使用(例如:-Dmyprop=myvalue) |
-d, –debug | 指定日誌輸出級別爲debug,可打印通常堆棧信息 |
–daemon | 使用Gradle守護進程執行構建,若是沒有守護進程則啓動一個守護進程 |
–foreground | 以foreground形式啓動守護進程 |
-g, –gradle-user-home | 指定默認的指定Gradle 用戶home目錄.默認在」用戶目錄/.gradle」 |
-I, –init-script | 運行指定初始化腳本,腳本會在build以前執行.查找init.gradle的路徑順序:./,USER_HOME/.gradle/ ,USER_HOME/.gradle/init.d/,GRADLE_HOME/init.d/ |
-i, –info | 指定日誌級別爲info |
–include-build | 在組合中包含指定的構建 |
-m, –dry-run | 模擬任務執行過程並將過程打印出來,並不是真正執行,目的是展現執行的過程 |
–max-workers | 設定參與構建的cpu的核數 |
–no-build-cache | 禁用構建緩存 |
–no-configure-on-demand | 根據須要禁用配置 |
–no-daemon | 禁用守護進程,不使用守護進程執行構建. 若是你已經配置了Gradle總會使用守護進程運行,此選項有幫助 |
–no-parallel | 禁止使用並行模式構建項目 |
–no-scan | 禁用建立構建掃描,更多信息能夠參考https://gradle.com/build-scans |
–offline | 採用離線模式構建項目,不使用網路資源(此時頗有可能出現構建失敗的狀況慎用) |
-P, –project-prop | 設置Gradle的項目參數,會被直接加載到Gradle領域對象上(例如:-Pmyprop=myvalue) |
-p, –project-dir | 爲gradle指定起始目錄。默認爲當前目錄 |
–parallel | 採用並行模式構建。gradle採用配置的線程數進行構建 |
–profile | 存儲一份執行報告到<build_dir>/reports/profile,包括總時間和在配置和任務執行階段的細節。並以時間大小倒序排列,而且記錄了任務的執行狀況.Gradle會根據構建時間命名這些報告文件 |
–project-cache-dir | 指定項目的緩存目錄。默認在項目目錄中。 |
-q, –quiet | 靜默方式,只輸出error信息 |
–recompile-scripts | 強制構建腳本從新編譯[廢棄] |
–refresh-dependencies | 刷新依賴的狀態 |
–rerun-tasks | 忽略先前緩存的任務結果 |
-S, –full-stacktrace | 打印所有詳細信息,全部異常的堆棧信息 |
-s, –stacktrace | 打印全部異常的堆棧信息 |
–scan | 建立一個構建掃描。若是構建掃描插件沒有被應用,那麼gradle會發出警告(https://gradle.com/build-scans) |
–status | 統計信息,展現正在運行和最近中止的gradle進程 |
–stop | 若是守護進程正在運行,中止gradle的守護進程 |
-t, –continuous | 連續構建模式,採用此模式後會監聽全部構建文件變化,文件發生變化後會自動從新構建 |
-u, –no-search-upward | 不使用父目錄中的settings.gradle文件,若是不添加此項,父目錄的setting.gradle會覆蓋子目錄下的配置 |
-v, –version | 打印gradle版本信息 |
-w, –warn | 指定日誌級別爲warn |
–warning-mode | 指定要生成的警告模式。值是’all’, ‘summary’(默認) or ‘no’ |
-x, –exclude-task | 任務排除,跳過執行指定任務 |
使用eclipse建立一個gradle的項目:gradleDemo
該項目不寫任何代碼,只配置build.gradle,只爲了演示命令的基本用法及效果
build.gradlewindows
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:23.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
//有依賴
task base {
doLast {
println "i'm base!"
}
}
task extend(dependsOn: base) {
doLast {
println "I'm extend!"
}
}
//使用<<代替dolast
task dolast <<{
println "show me! dolast"
println ("show me! dolast")
println ("show me! dolast");
}
//多字節名稱使用間歇eEE
task extendExpExp() {
doLast {
println "I'm extendExpExp"
}
}
挑一些經常使用的命令用實例說明用法,首先切換到項目所在目錄api
Microsoft Windows [版本 6.1.7601]
版權全部 (c) 2009 Microsoft Corporation。保留全部權利。
C:\Users\yueling.DANGDANG>cd /d d:\eclipsWS\gradleWS\gradleDemo
d:\eclipsWS\gradleWS\gradleDemo>
d:\eclipsWS\gradleWS\gradleDemo>gradle
:help
Welcome to Gradle 4.5.1.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle extend
:base
i'm base! :extend I'm extend!
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
多模塊項目中,能夠在root項目中指定項目名稱來執行,此項目時單模塊的,不在此演示,格式爲 gradle projectName:taskName緩存
d:\eclipsWS\gradleWS\gradleDemo>gradle base dolast
:base
i'm base!
:dolast
show me! dolast
show me! dolast
show me! dolast
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
>d:\eclipsWS\gradleWS\gradleDemo>gradle base dolast -x dolast
:base
i'm base!
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle base --rerun-tasks
:base
i'm base!
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle base --continue
:base
i'm base!
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
固然目前該項目沒有錯誤^_^ruby
base.doLast {
println "addtion end......"
}
base.doFirst {
println "addtion start......"
}
那麼輸入就會改變,在任務開始和結束打印的內容隨之變化bash
d:\eclipsWS\gradleWS\gradleDemo>gradle base
:base
addtion start......
i'm base!
addtion end......
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle -q base
addtion start......
i'm base!
addtion end......
d:\eclipsWS\gradleWS\gradleDemo>
d:\eclipsWS\gradleWS\gradleDemo>gradle -w base
addtion start......
i'm base!
addtion end......
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
d:\eclipsWS\gradleWS\gradleDemo>gradle -i base
Initialized native services in: C:\Users\yueling.DANGDANG\.gradle\native
The client will now receive all logging from the daemon (pid: 59652). The daemon log file: C:\Users\yueling.DANGDANG\.gradle\daemon\4.5.1\daemon-59652.out.log
Starting 33rd build in daemon [uptime: 3 hrs 53 mins 31.583 secs, performance: 100%, no major garbage collections]
Using 4 worker leases.
Starting Build
Settings evaluated using settings file 'D:\eclipsWS\gradleWS\gradleDemo\settings.gradle'.
Projects loaded. Root project using build file 'D:\eclipsWS\gradleWS\gradleDemo\build.gradle'.
Included projects: [root project 'gradleDemo']
Evaluating root project 'gradleDemo' using build file 'D:\eclipsWS\gradleWS\gradleDemo\build.gradle'.
All projects evaluated.
Selected primary task 'base' from project :
Tasks to be executed: [task ':base']
:base (Thread[Daemon worker Thread 17,5,main]) started.
:base
Task ':base' is not up-to-date because:
Task has not declared any outputs.
addtion start......
i'm base!
addtion end......
:base (Thread[Daemon worker Thread 17,5,main]) completed. Took 0.0 secs.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
VCS Checkout Cache (D:\eclipsWS\gradleWS\gradleDemo\.gradle\vcsWorkingDirs) has not been cleaned up in 0 days
d:\eclipsWS\gradleWS\gradleDemo>gradle base --console plain
:base
addtion start......
i'm base!
addtion end......
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle base --console rich
[1A[1m<[0;32;1;0;39;1m-------------> 0% CONFIGURING [0s][m[35D[1B[1A[0K
[1m> Task :base[m[33m [39m
addtion start......
i'm base!
addtion end......
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
[32;1mBUILD SUCCESSFUL[0;39m in 0s
1 actionable task: 1 executed
windows下顯示有點問題,不支持顏色的控制~尊駕能夠移步到linux下執行,顏色等都顯示出來了
d:\eclipsWS\gradleWS\gradleDemo>gradle base --status
PID STATUS INFO
59652 IDLE 4.5.1
Only Daemons for the current Gradle version are displayed. See https://docs.gradle.org/4.5.1/userguide/gradle_daemon.html#sec:status
d:\eclipsWS\gradleWS\gradleDemo>gradle base --scan
:base
addtion start......
i'm base!
addtion end......
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
Publishing a build scan to scans.gradle.com requires accepting the Terms of Service defined at https://scans.gradle.com/terms-of-service. Do you accept these terms? [yes, no]
yes
Gradle Cloud Services license agreement accepted.
Publishing build scan...
https://gradle.com/s/pmnnhqgqmv4zw
rootProject.name = 'gradleDemooooooooooooo'
d:\eclipsWS\gradleWS\gradleDemo>gradle base -c d:\settings.gradle -i
Initialized native services in: C:\Users\yueling.DANGDANG\.gradle\native
The client will now receive all logging from the daemon (pid: 59652). The daemon log file: C:\Users\yueling.DANGDANG\.gradle\daemon\4.5.1\daemon-59652.out.log
Starting 45th build in daemon [uptime: 4 hrs 20 mins 27.835 secs, performance: 100%, no major garbage collections]
Using 4 worker leases.
Starting Build
Settings evaluated using settings file 'D:\settings.gradle'.
Projects loaded. Root project using build file 'D:\build.gradle'.
Included projects: [root project 'gradleDemooooooooooooo']
Evaluating root project 'gradleDemooooooooooooo' using build file 'D:\build.gradle'.
All projects evaluated.
Selected primary task 'base' from project :
Tasks to be executed: [task ':base']
:base (Thread[Task worker for ':',5,main]) started.
:base
Task ':base' is not up-to-date because:
Task has not declared any outputs.
addtion start......
i'm base!
addtion end......
:base (Thread[Task worker for ':',5,main]) completed. Took 0.001 secs.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
VCS Checkout Cache (D:\.gradle\vcsWorkingDirs) has not been cleaned up in 0 days
task baseadd {
doLast {
println "build.gradle not in dir!"
}
}
d:\eclipsWS\gradleWS\gradleDemo>gradle baseadd -b d:\build.gradle
:baseadd
build.gradle not in dir!
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle build
:compileJava
:processResources NO-SOURCE
:classes
:jar
:assemble
:compileTestJava
:processTestResources NO-SOURCE
:testClasses
:test
:check
:build
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 18s
4 actionable tasks: 4 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle check
:compileJava
:processResources NO-SOURCE
:classes
:compileTestJava
:processTestResources NO-SOURCE
:testClasses
:test
:check
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 2s
3 actionable tasks: 3 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle clean
:clean
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle projects
:projects
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'gradleDemo'
No sub-projects
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
d:\eclipsWS\gradleWS\gradleDemo>gradle tasks
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradleDemo'.
components - Displays the components produced by root project 'gradleDemo'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradleDemo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradleDemo'.
dependentComponents - Displays the dependent components of components in root project 'gradleDemo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradleDemo'. [incubating]
projects - Displays the sub-projects of root project 'gradleDemo'.
properties - Displays the properties of root project 'gradleDemo'.
tasks - Displays the tasks runnable from root project 'gradleDemo'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
gradle tasks –all
d:\eclipsWS\gradleWS\gradleDemo>gradle -q help --task base
Detailed task information for base
Path
:base
Type
Task (org.gradle.api.Task)
Description
-
Group
-
d:\eclipsWS\gradleWS\gradleDemo>gradle dependencies
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
api - API dependencies for source set 'main'. (n)
\--- org.apache.commons:commons-math3:3.6.1 (n)
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts.
No dependencies
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
No dependencies
compileClasspath - Compile classpath for source set 'main'.
+--- org.apache.commons:commons-math3:3.6.1
\--- com.google.guava:guava:23.0
+--- com.google.code.findbugs:jsr305:1.3.9
+--- com.google.errorprone:error_prone_annotations:2.0.18
+--- com.google.j2objc:j2objc-annotations:1.1
\--- org.codehaus.mojo:animal-sniffer-annotations:1.14
compileOnly - Compile only dependencies for source set 'main'.
No dependencies
……
d:\eclipsWS\gradleWS\gradleDemo>gradle model
:model
------------------------------------------------------------
Root project
------------------------------------------------------------
+ binaries
| Type: org.gradle.platform.base.BinaryContainer
| Creator: BinaryBasePlugin.Rules#binaries(BinaryContainer)
| Rules:
? JavaBasePlugin.Rules#attachBridgedBinaries(BinaryContainer, JavaBasePlugin.BridgedBinaries)
+ main
| Type: org.gradle.api.internal.jvm.DefaultClassDirectoryBinarySpec_Decorated
| Value: Classes 'main'
| Creator: JavaBasePlugin.Rules#attachBridgedBinaries(BinaryContainer, JavaBasePlugin.BridgedBinaries) > put()
| Rules:
? BinaryBasePlugin.Rules#defineBuildLifecycleTask(BinarySpecInternal, ITaskFactory)
? BinaryBasePlugin.Rules#addSourceSetsOwnedByBinariesToTheirInputs(BinarySpecInternal)
+ test
| Type: org.gradle.api.internal.jvm.DefaultClassDirectoryBinarySpec_Decorated
| Value: Classes 'test'
| Creator: JavaBasePlugin.Rules#attachBridgedBinaries(BinaryContainer, JavaBasePlugin.BridgedBinaries) > put()
| Rules:
? BinaryBasePlugin.Rules#defineBuildLifecycleTask(BinarySpecInternal, ITaskFactory)
? BinaryBasePlugin.Rules#addSourceSetsOwnedByBinariesToTheirInputs(BinarySpecInternal)
+ components
| Type: org.gradle.platform.base.ComponentSpecContainer
| Creator: ComponentBasePlugin.PluginRules#components(ComponentSpecContainer)
……
還有一些沒有演示的命令,根據幫助文檔都能嘗試一下