Gradle 使用命令行操作Gradle

来源:互联网 发布:麦淘网源码 编辑:程序博客网 时间:2024/05/17 22:36


Gradle介绍:现在Gradle已经成为java世界最火的构建工具,风头已经盖过了冗余的ant,落后的maven。Gradle是以Groovy语言编写的一套构建脚本的DSL,由于Groovy语法的优雅,所以导致Gradle天生就有简洁、可读性强、灵活等特性。

Gradle最新稳定版本2.5下载地址https://downloads.gradle.org/distributions/gradle-2.5-all.zip

配置环境变量:右键我的电脑 – 属性 – 高级系统设置 – 高级 – 环境变量 – 新建 – 变量名(GRADLE_USER_HOME) – 变量值(D:\Program Files\gradle-2.5) – 确定 – Path前面添加(%GRADLE_USER_HOME%\bin;) – 确定。

验证环境变量是否配置成功:win+R – cmd+回车 – gradle -v – 显示Gradle版本号(配置成功)

快速打开工程目录cmd:工程目录下 – shift+右键 – 在此处打开命令窗口

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 conjunction 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命令。

Gradle的图形界面:输入命令行 gradle--gui 。通过这个gui界面可以很方面的执行gradle的各种命令,还可以将常用的命令保存为favorites。该gui的配置信息默认被存储在当前项目的gradle-app.setting文件中。注意使用gradle--gui会阻塞当前终端,可以使用gradle--gui&来实现后台运行。

重新编译Gradle脚本:第一次运行Gradle命令,会在项目更目录下生成一个.gradle目录来存放编译后的脚本。只有当构建脚本发生修改时采用重新编译。我们可以使用--recompile-scripts来强行重新编译。

0 1
原创粉丝点击