命令学习比较枯燥,全部是例子~
假设你的本地gradle已经安装配置完成。没有安装配置的,可以参考 gradle安装
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的内容可以在项目的目录下查看。
gradle test deploy
gradle --console=plain test
gradle test --console=plain
记忆技巧:长格式的选项 表示启用特性的都具有反向操作(关闭特性),关闭特性长选项格式在启用特性前缀no-
--build-cache
--configure-on-demand
--daemon
--parallel
--scan
--no-build-cache
--no-configure-on-demand
--no-daemon
--no-parallel
--no-scan
#命令列表
打开控制台,
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 | 存储一份执行报告到 |
--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.gradle
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"
}
}
挑一些常用的命令用实例说明用法,首先切换到项目所在目录
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 ...
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
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
当然目前该项目没有错误_
base.doLast {
println "addtion end......"
}
base.doFirst {
println "addtion start......"
}
那么输入就会改变,在任务开始和结束打印的内容随之变化
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 :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: Cleans the output files of a task.
Pattern: build: Assembles the artifacts of a configuration.
Pattern: upload: 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
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)
……
还有一些没有演示的命令,根据帮助文档都能尝试一下