码迷,mamicode.com
首页 > 其他好文 > 详细

Gradle 教程说明 用户指南 第11章 使用 Gradle 命令行

时间:2014-11-26 19:03:25      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:gradle

11.1 执行多个任务

每个任务都只执行一次,不管它如何被包含在build:无论是在命令行中指定,或作为一个依赖的另一个任务,或两者兼而有之。

以下四个任务的定义。dist和测试都依赖于 编译任务。运行gradle dist测试这个构建脚本,编译任务将被执行一次。

build.gradle
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‘
}

> gradle dist test
输出:

:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

说明每个任务只被执行一次


11.2 剔除任务

可以使用-x命令行选项,后跟排除任务的名称,以剔除一个任务

> gradle dist -x test

输出:
:compile
compiling source
:dist
building the distribution

发现任务test没有被执行,它所依赖的compileTest也没有执行


11.3 在发生故障(错误)时继续构建

默认情况下,只要任何任务失败,Gradle将中止执行。这使得构建更快地完成,但隐藏了其他可能发生的故障。

为了发现在一个单一的构建中多个可能发生故障的地方,你可以使用--continue选项。

> gradle --continue build


11.4 使用缩写的任务名

在命令行中使用任务时,任务名可以使用任意长度的从头开始的缩写,只要能与目标任务唯一匹配即可

> gradle di
输出:

:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution


也可以使用驼峰式命名,以下cT指代compileTest。当然也要是唯一匹配才行。

> gradle cT
输出:

:compile
compiling source
:compileTest
compiling unit tests

当然缩写名也可以用在-x 选项后


11.5 选择执行哪个构建文件

使用-b选项,决定执行哪个构建文件

创建目录和文件:subdir/myproject.gradle

task hello << {
    println "using build file ‘$buildFile.name‘ in ‘$buildFile.parentFile.name‘."
}

> gradle -q -b subdir/myproject.gradle hello

输出:using build file ‘myproject.gradle‘ in ‘subdir‘.


使用-p选项,决定执行哪个工程

创建目录和文件:subdir/build.gradle,内容为task hello

> gradle -q -p subdir hello

输出:using build file ‘build.gradle‘ in ‘subdir‘.

如要使用其他构建文件名,可与-b 一起使用: < gradle -q -p subdir -b subdir/myproject.gradle hello


11.6 获取构建信息

Gradle提供了几个内置任务来表明构建的具体细节。这可以帮助理解构建的结构、依赖,及调试问题。

除了下面所示的内置任务,还可以使用report plugin,来产生报告。


11.6.1 项目清单

这里使用gradle/samples/java/multiproject

> gradle -q projects

输出:

Root project ‘multiproject‘
+--- Project ‘:api‘
+--- Project ‘:services‘
|    +--- Project ‘:services:shared‘
|    \--- Project ‘:services:webservice‘
\--- Project ‘:shared‘

输出了项目清单。这个+ 可以看成 后面还有 的意思


在api项目下的build.gradle 中添加描述信息:

description = ‘This is API project. ‘ 

> gradle -q projects

输出:

Root project ‘multiproject‘
+--- Project ‘:api‘ - This is API project. 
+--- Project ‘:services‘
|    +--- Project ‘:services:shared‘
|    \--- Project ‘:services:webservice‘
\--- Project ‘:shared‘


11.6.2 任务列表

运行gradle tasks,显示任务列表。对于默认任务还会显示它相关的描述信息。

> gradle 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 classes ‘main‘.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes ‘test‘.
war - Generates a war archive with all the compiled classes, the web-app content and the libraries.

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
components - Displays the components produced by root project ‘multiproject‘. [incubating]
dependencies - Displays all dependencies declared in root project ‘multiproject‘.
dependencyInsight - Displays the insight into a specific dependency in root project ‘multiproject‘.
help - Displays a help message.
projects - Displays the sub-projects of root project ‘multiproject‘.
properties - Displays the properties of root project ‘multiproject‘.
tasks - Displays the tasks runnable from root project ‘multiproject‘ (some of the displayed tasks may belong to subprojects).

IDE tasks
---------
cleanEclipse - Cleans all Eclipse files.
cleanEclipseWtp - Cleans Eclipse wtp configuration files.
eclipse - Generates all Eclipse files.
eclipseWtp - Generates Eclipse wtp configuration files.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
checkProjectDependency


默认情况下,报告里的任务都存在一个组里。可以对任务更改组属性,还可以设置任务描述:

比如在11.5里的例子中,后加上:

hello {
    description = ‘hello file‘
    group = ‘admin‘
}

组属性值可以有:build、build setup等一些Gradle报告中默认的分组名,也可以自定一个名字

进入subdir目录

> gradle -q -b myproject.gradle task

输出

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Admin tasks
-----------
hello - hello file

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
components - Displays the components produced by root project ‘subdir‘. [incubating]
dependencies - Displays all dependencies declared in root project ‘subdir‘.
dependencyInsight - Displays the insight into a specific dependency in root project ‘subdir‘.
help - Displays a help message.
projects - Displays the sub-projects of root project ‘subdir‘.
properties - Displays the properties of root project ‘subdir‘.
tasks - Displays the tasks runnable from root project ‘subdir‘.


再加上--all选项,可以在报告中输出,每个任务的依赖细节。

在上例中,再加上:

task dep(dependsOn: hello) << {
println "dep."
}
dep {
    description = ‘dep task‘
    group = ‘admin‘
}

> gradle -q -b myproject.gradle task --all

... ...

Admin tasks
-----------
dep - dep task [hello]
hello - hello file

... ...


11.6.3 显示任务的使用细节

从这节开始使用 gradle/samples/userguide/tutorial/projectReports 示例

gradle help --task taskName,显示任务所在路径,类型,可能有描述。

> gradle -q help --task libs

输出:

Detailed task information for libs

Paths
     :api:libs
     :webapp:libs

Type
     Task (org.gradle.api.Task)

Description
     Builds the JAR



11.6.4  项目依赖清单

运行命令 gradle dependencies project:dependencies ,多个项目以空格分隔,显示项目的依赖列表,树状显示。

>  gradle -q dependencies api:dependencies webapp:dependencies

输出:

------------------------------------------------------------
Root project
------------------------------------------------------------

No configurations

------------------------------------------------------------
Project :api - The shared API for the application
------------------------------------------------------------

compile
\--- org.codehaus.groovy:groovy-all:2.3.6

testCompile
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3

------------------------------------------------------------
Project :webapp - The Web application implementation
------------------------------------------------------------

compile
+--- project :api
|    \--- org.codehaus.groovy:groovy-all:2.3.6
\--- commons-io:commons-io:1.2

testCompile
No dependencies


使用--configuration 过滤任务依赖报告。

 gradle -q api:dependencies --configuration testCompile

输出:

------------------------------------------------------------
Project :api - The shared API for the application
------------------------------------------------------------

testCompile
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3


11.6.5 观察一个特定的依赖

运行命令 gradle -q project:dependencyInsight --dependency dependname --configuration taskName

显示--configuration 过滤任务的 名为dependname的详细依赖信息

> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile

输出:

org.codehaus.groovy:groovy-all:2.3.6
\--- project :api
     \--- compile

> gradle -q api:dependencyInsight --dependency j --configuration testCompile

输出:
junit:junit:4.11
\--- testCompile

发现,--dependency 后的 依赖名称可以缩写


11.6.6 项目属性清单

运行命令 gradle properties,输出所有项目属性列表

> gradle -q api:properties

指定api项目,输出属性:

allprojects: [project ‘:api‘]
ant: org.gradle.api.internal.project.DefaultAntBuilder@794e1b66
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@76e903c1
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@3bf5b0e4
asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@7f30677c
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@42da3f20
buildDir: /Users/stone/Desktop/builds/projectReports/api/build
buildFile: /Users/stone/Desktop/builds/projectReports/api/build.gradle
buildScriptSource: org.gradle.groovy.scripts.UriScriptSource@666702a
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@2f4dd8ae
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@2eb1d72d
clean: task ‘:api:clean‘
compile: task ‘:api:compile‘
components: []
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@1da5e34e
configurations: [configuration ‘:api:compile‘, configuration ‘:api:testCompile‘]
convention: org.gradle.api.internal.plugins.DefaultConvention@75ee2516
defaultTasks: []
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@65c503d6
depth: 1
description: The shared API for the application
displayName: root project ‘projectReports‘
ext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@697f0acc
extensions: org.gradle.api.internal.plugins.DefaultConvention@75ee2516
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@139e0d30
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@6d73d45f
gradle: build ‘projectReports‘
group: projectReports
inheritedScope: org.gradle.api.internal.ExtensibleDynamicObject$InheritedDynamicObject@4f7be11c
libs: task ‘:api:libs‘
logger: org.gradle.api.logging.Logging$LoggerImpl@4df81e08
logging: org.gradle.logging.internal.DefaultLoggingManager@5c7bc735
mayImplementMissingMethods: false
mayImplementMissingProperties: false
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@42539802
module: org.gradle.api.internal.artifacts.ProjectBackedModule@9746661
name: api
parent: root project ‘projectReports‘
parentIdentifier: root project ‘projectReports‘
path: :api
plugins: [org.gradle.api.plugins.HelpTasksPlugin@2f2379f2]
processOperations: org.gradle.api.internal.file.DefaultFileOperations@139e0d30
project: project ‘:api‘
projectDir: /Users/stone/Desktop/builds/projectReports/api
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@7e20cd52
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@1b88032f
properties: {...}
repositories: [org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@7e8910c7]
resources: org.gradle.api.internal.resources.DefaultResourceHandler@58acb9b7
rootDir: /Users/stone/Desktop/builds/projectReports
rootProject: root project ‘projectReports‘
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@33d4cadc
scriptPluginFactory: org.gradle.configuration.DefaultScriptPluginFactory@57524c19
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$5@5c022a18
services: ProjectScopeServices
standardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager@5c7bc735
state: project state ‘EXECUTED‘
status: release
subprojects: []
tasks: [task ‘:api:clean‘, task ‘:api:compile‘, task ‘:api:libs‘, task ‘:api:properties‘]
version: 1.0-SNAPSHOT


11.6.7 构建分析

--profile 选项会在build/reports/profile 生成html文件,该html文件就是一份构建的配置报告。

如果有 buildSrc目录,则还会在它下面生成一份profile

> gradle --profile

生成的文件,如下图:

bubuko.com,布布扣


11.7 dry run

-m 选项, 不执行某任务

> gradle -m hello1 hello2 hello3



Gradle 教程说明 用户指南 第11章 使用 Gradle 命令行

标签:gradle

原文地址:http://blog.csdn.net/jjwwmlp456/article/details/41512289

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!