Gradle详解-Chapter 4. Using the Gradle Command-Line

来源:互联网 发布:最好企业网络投资理财 编辑:程序博客网 时间:2024/04/30 10:47

Chapter 14 基础脚本构建

Chapter 16. Writing Build Scripts 写脚本

Chapter17. More about Tasks

Chapter 20. The Build Lifecycle 生命周期

第四章:使用Gradle命令行

Chapter 4. Using the Gradle Command-Line

4.1. Executing multiple tasks 多任务执行
格式:gradle task1,task2,task3。。。
Gradle按照顺序执行task,如果task有依赖关系执行依赖task。每个task只执行一次,不管有多少个依赖关系

task compile << {    println 'compiling source'}task compileTest(dependsOn: compile) << {    println 'compiling unit tests'}task test(dependsOn: [compile, compileTest]) << {    println 'running unit tests'}task dist(dependsOn: [compile, test]) << {    println 'building the distribution'}

结果:执行dist task

D:\GradleTest\project2>gradle dist:project2:compilecompiling source:project2:compileTestcompiling unit tests:project2:testrunning unit tests:project2:distbuilding the distributionBUILD SUCCESSFULTotal time: 2.023 secs

4.2. Excluding tasks 执行task过程中除去哪个task
使用 -x 命令:gradle dist -x test
结果:

D:\GradleTest>gradle -q dist -x testcompiling sourcebuilding the distribution

4.3. Continuing the build when a failure occurs
当一个错误发生时,继续执行build。
使用–continue 命令参数

4.4. Task name abbreviation task缩写
保证该build文件下,只有一个该task的缩写。否则命令出错
缩写支持:1,骆驼命名法的每个单词首字母。2 task缩写唯一(字母个数不限)

比如:分别在D:\GradleTest和D:\GradleTest\project2> 执行gradle di命令

project2文件夹下结果是正确的。

D:\GradleTest\project2>gradle di:project2:compilecompiling source:project2:compileTestcompiling unit tests:project2:testrunning unit tests:project2:distbuilding the distributionBUILD SUCCESSFULTotal time: 1.597 secs

**GradleTest文件夹下是错误的。
Candidates are: ‘dist’, ‘distribution’. 两个task都可以用di缩写**

D:\GradleTest>gradle diFAILURE: Build failed with an exception.* What went wrong:Task 'di' is ambiguous in root project 'GradleTest'. Candidates are: 'dist', 'distribution'.* Try:Run gradle tasks to get a list of available tasks. Run with --stacktrace optionto get the stack trace. Run with --info or --debug option to get more log output.BUILD FAILEDTotal time: 1.676 secs

4.5. Selecting which build to execute 选择执行脚本
执行其他project中的task 命令。 使用-b 或者 -p命令参数

执行project2项目中的dist命令:

-b参数
D:\GradleTest>gradle -q -b project2/build.gradle di
compiling source
compiling unit tests
running unit tests
building the distribution


-p参数

D:\GradleTest>gradle -q -p project2 di
compiling source
compiling unit tests
running unit tests
building the distribution

4.6. Forcing tasks to execute
命令参数:–rerun-tasks

D:\GradleTest>gradle –rerun-tasks -q -p project2 di
compiling source
compiling unit tests
running unit tests
building the distribution

4.7. Obtaining information about your build 获取脚本的信息
获取脚本的一些信息,比如项目结构,属性,各种任务命令,任务详情等等。

4.7.1. Listing projects 项目结构(当期工程和子项目)

gradle projects 命令

备注:可以通过 description 属性为每个子项目project添加描述信息。

D:\GradleTest>gradle -q projects------------------------------------------------------------Root project------------------------------------------------------------Root project 'GradleTest'+--- Project ':antLoadfileResources'+--- Project ':javaLib'+--- Project ':project1'\--- Project ':project2' - description information  // 这个是自己添加的描述信息To see a list of the tasks of a project, run gradle <project-path>:tasksFor example, try running gradle :antLoadfileResources:tasks

4.7.2. Listing tasks

获取project的所有任务。 还可以显示任务组group,任务描述信息,如果他们被设置了。

使用gradle tasks 命令

D:\GradleTest>gradle -q tasksI'm Gradle------------------------------------------------------------All tasks runnable from root project------------------------------------------------------------Build Setup tasks-----------------init - Initializes a new Gradle build. [incubating]wrapper - Generates Gradle wrapper files. [incubating]Build22 tasks  // 自己的任务组group-------------hello - task description informationHelp tasks----------buildEnvironment - Displays all buildscript dependencies declared in root project 'GradleTest'.components - Displays the components produced by root project 'GradleTest'. [incubating]dependencies - Displays all dependencies declared in root project 'GradleTest'.dependencyInsight - Displays the insight into a specific dependency in root project 'GradleTest'.help - Displays a help message.model - Displays the configuration model of root project 'GradleTest'. [incubating]projects - Displays the sub-projects of root project 'GradleTest'.properties - Displays the properties of root project 'GradleTest'.tasks - Displays the tasks runnable from root project 'GradleTest' (some of thedisplayed tasks may belong to subprojects).Other tasks-----------checksumconfigureconfigure1countdistgetPathhellopintro - task description information  // task 描述信息lib2loadfilemyCopymyTasknotALibprintTaskPropertiesreleasesstask0taskXtaskYupperTo see all tasks and more detail, run gradle tasks --allTo see more detail about a task, run gradle help --task <task>

更详细信息可以使用:gradle -q tasks –all命令

部分code:

Other tasks-----------project1:checksumproject1:configureproject1:configure1project1:countproject2:dist    project2:compile    project2:compileTest    project2:testproject1:getPathproject2:hellopproject1:intro - task description informationproject1:lib2project1:loadfileproject1:myCopyproject1:myTaskproject1:notALibproject1:printTaskPropertiesproject1:release    project1:distributionproject1:ssproject1:task0    project1:task1    project1:task2    project1:task3project1:taskX [project2:taskY]    project1:lib1project2:taskYproject1:upper

4.7.3. Show task usage details 显示单个task详情

使用gradle help –task someTask命令

D:\GradleTest>gradle -q help --task helloI'm GradleDetailed task information for helloPath     :project1:helloType     Task (org.gradle.api.Task)Description     task description informationGroup     build22

4.7.4. Listing project dependencies 查看项目依赖列表

使用gradle dependencies 命令
显示该项目下所有task的依赖关系

查看project1项目的依赖
gradle -q dependencies :project1:dependencies

查看project1和project2两个项目的依赖
gradle -q dependencies :project1:dependencies :project2:dependencies

查看某个task的依赖:通过–configuration命令
gradle -q project1:dependencies –configuration testCompile

GradleDemo中的位置:D:\Program Files\gradle-2.14\samples\userguide\tutorial\projectReports

4.7.5. Listing project buildscript dependencies
Running gradle buildEnvironment visualises the buildscript dependencies of the selected project, similarly to how gradle dependencies visualises the dependencies of the software being built.

4.7.6. Getting the insight into a particular dependency
Running gradle dependencyInsight gives you an insight into a particular dependency (or dependencies) that match specified input

命令:gradle -q webapp:dependencyInsight –dependency groovy –configuration compile

> gradle -q webapp:dependencyInsight --dependency groovy --configuration compileorg.codehaus.groovy:groovy-all:2.4.4\--- project :api     \--- compile

4.7.7. Listing project properties
使用 gradle properties 命令:当前项目的属性

gradle -q properties
gradle -q :project1:properties
gradle -q :project2:properties

------------------------------------------------------------Project :project1------------------------------------------------------------allprojects: [project ':project1']ant: org.gradle.api.internal.project.DefaultAntBuilder@3ce36adfantBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@767d75b3artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@26ba778basDynamicObject: DynamicObject for project ':project1'baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@1bfe5a8abuildDir: D:\GradleTest\project1\buildbuildFile: D:\GradleTest\project1\build.gradlebuildScriptSource: org.gradle.groovy.scripts.UriScriptSource@57ea2861buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@4e01255echecksum: task ':project1:checksum'childProjects: {}

4.7.8. Profiling a build
The –profile command line option will record some useful timing information while your build is running and write a report to the build/reports/profile directory. The report will be named using the time when the build was run.

This report lists summary times and details for both the configuration phase and task execution. The times for configuration and task execution are sorted with the most expensive operations first. The task execution results also indicate if any tasks were skipped (and the reason) or if tasks that were not skipped did no work.

Builds which utilize a buildSrc directory will generate a second profile report for buildSrc in the buildSrc/build directory.

0 0
原创粉丝点击