【转载】Gradle学习 第十一章:使用Gradle命令行
轉載地址:http://ask.android-studio.org/?/article/94
?
This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.
<翻譯>?這一章介紹Gradle基礎命令行。你可以使用gradle進行構建,就像上一章節中你看到的那樣。
11.1.?Executing multiple tasks 多任務執行
You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example, the command gradle compile test will execute the compile and test tasks. Gradle will execute the tasks in the order that they are listed on the command-line, and will also execute the dependencies for each task. Each task is executed once only, regardless of how it came to be included in the build: whether it was specified on the command-line, or as a dependency of another task, or both. Let's look at an example.
<翻譯>?你可以通過在命令行中列出每個任務來執行多任務。例如,‘gradle compile test’ 將執行compile和test兩個任務。Gradle將執行在命令行中列出的任務,以及每個任務所依賴的任務。每個任務僅執行一次,不論它是在命令行中被指定的或者另一個任務的依賴。請看一個例子。
Below four tasks are defined. Both dist and test depend on the compile task. Running gradle dist test for this build script results in the compile task being executed only once.
<翻譯>?下面定義了得個任務。都依賴compile任務。
Figure?11.1.?Task dependencies 任務依賴
Example?11.1.?Executing multiple tasks 多任務執行
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'
}
Output?of?gradle?dist?test
>?gradle?dist?test
:compile
compiling?source
:compileTest
compiling?unit?tests
:test
running?unit?tests
:dist
building?the?distribution
BUILD?SUCCESSFUL
Total?time:?1?secs
Each task is executed only once, so gradle test test is exactly the same as gradle test.
<翻譯>?每個任務僅執行一次,所以'gradle test test'相當于'gradle test'。
11.2.?Excluding tasks 排隊任務
You can exclude a task from being executed using the -x command-line option and providing the name of the task to exclude. Let's try this with the sample build file above.
<翻譯>?你可以使用'-x 任務名'選項來排除一個任務。看看下面。
Example?11.2.?Excluding tasks 排除任務
Output?of?gradle?dist?-x?test
>?gradle?dist?-x?test
:compile
compiling?source
:dist
building?the?distribution
BUILD?SUCCESSFUL
Total?time:?1?secs
You can see from the output of this example, that the test task is not executed, even though it is a dependency of the dist task. You will also notice that the test task's dependencies, such as compileTest are not executed either. Those dependencies of test that are required by another task, such as compile, are still executed.
<翻譯>?你可以看到輸出內容,test任務沒有執行,盡管它是dist任務的依賴。你同樣也注意到了,test的依賴compileTest也沒有執行。但是另一個依賴compile卻執行了。
11.3.?Continuing the build when a failure occurs 發生錯誤時繼續構建
By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the --continue option.
<翻譯>?默認情況下,只要有一個任務失敗Gradle將停止執行。這樣的結果就是我們無法發現其它的的錯誤。這個時候,我們就可以使用'--continue'選項來幫助我們發現更多的錯誤。
When executed with --continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.
<翻譯>?當我們執行的時候帶上‘--continue’選項,Gradle將執行每一個要執行的任務,所有的依賴,無故障的完成任務。在構建完成后會顯示每一個錯誤。
If a task fails, any subsequent tasks that were depending on it will not be executed, as it is not safe to do so. For example, tests will not run if there is a compilation failure in the code under test; because the test task will depend on the compilation task (either directly or indirectly).
<翻譯>?如果一個任務失敗了,接下來任何一個依賴它的任務都不會執行,因為這樣是不安全的。
11.4.?Task name abbreviation 任務名縮寫
When you specify tasks on the command-line, you don't have to provide the full name of the task. You only need to provide enough of the task name to uniquely identify the task. For example, in the sample build above, you can execute task dist by running gradle d:
<翻譯>?在你指定任務的時候,不用提供任務全名。你只需要提供足夠的任務唯一標識。例如,下面的例子中,‘gradle d’就可以執行dist任務:
Example?11.3.?Abbreviated task name 縮寫任務名
Output?of?gradle?di
>?gradle?di
:compile
compiling?source
:compileTest
compiling?unit?tests
:test
running?unit?tests
:dist
building?the?distribution
BUILD?SUCCESSFUL
Total?time:?1?secs
You can also abbreviate each word in a camel case task name. For example, you can execute task compileTest by running gradle compTest or even gradle cT
<翻譯>?你還可以使用駝峰任務名。例如,你可以使用‘gradle compTest’或‘gradle cT’來執行compileTest任務
Example?11.4.?Abbreviated camel case task name 使用駝峰任務名
Output?of?gradle?cT
>?gradle?cT
:compile
compiling?source
:compileTest
compiling?unit?tests
BUILD?SUCCESSFUL
Total?time:?1?secs
You can also use these abbreviations with the -x command-line option.
<翻譯>?你也可以帶上‘-x’選項來使用這些縮寫。
11.5.?Selecting which build to execute 選擇執行
When you run the gradle command, it looks for a build file in the current directory. You can use the -b option to select another build file. If you use -b option then settings.gradle file is not used. Example:
<翻譯>?當你執行一個gradle命令,它看上去會是當前目錄的build文件。你可以使用‘-b’選項來選擇另一個文件,這樣settings.gralde文件不會被使用。例如:
Example?11.5.?Selecting the project using a build file 選擇項目使用build文件
subdir/myproject.gradle
task?hello?<<?{
println?"using?build?file?'$buildFile.name'?in?'$buildFile.parentFile.name'."
}
Output?of?gradle?-q?-b?subdir/myproject.gradle?hello
>?gradle?-q?-b?subdir/myproject.gradle?hello
using?build?file?'myproject.gradle'?in?'subdir'.
Alternatively, you can use the -p option to specify the project directory to use. For multi-project builds you should use -p option instead of -b option.
<翻譯>?另外,你可以使用‘-p’選項來替代‘-b’指定多個項目目錄。
Example?11.6.?Selecting the project using project directory 使用項目目錄選擇項目
Output?of?gradle?-q?-p?subdir?hello
>?gradle?-q?-p?subdir?hello
using?build?file?'build.gradle'?in?'subdir'.
11.6.?Obtaining information about your build 獲取構建信息
Gradle provides several built-in tasks which show particular details of your build. This can be useful for understanding the structure and dependencies of your build, and for debugging problems.
<翻譯>?Gradle提供了幾個顯示構建詳情的內置任務。這個可以用來理解構建的結構,依賴,以及調試問題。
In addition to the built-in tasks shown below, you can also use the project report plugin to add tasks to your project which will generate these reports.
<翻譯>?除了下面顯示的內置任務,你還可以使用項目報告插件來添加任務到你的項目。
11.6.1.?Listing projects 列出項目
Running gradle projects gives you a list of the sub-projects of the selected project, displayed in a hierarchy. Here is an example:
<翻譯>?運行'gradle projects'可以列出所選項目的子模塊,看下面:
Example?11.7.?Obtaining information about projects 獲取項目信息
Output?of?gradle?-q?projects
>?gradle?-q?projects
------------------------------------------------------------
Root project 父項目
Root?project?'projectReports'
+---?Project?':api'?-?The?shared?API?for?the?application
\---?Project?':webapp'?-?The?Web?application?implementation
To?see?a?list?of?the?tasks?of?a?project,?run?gradle?<project-path>:tasks
**<翻譯>**?運行'gradle?項目路徑:tasks'可以查看該項目的任務列表
For?example,?try?running?gradle?:api:tasks?比如運行'gradle?:api:tasks'
The report shows the description of each project, if specified. You can provide a description for a project by setting the description property:
<翻譯>?如果你指定相關內容,可以顯示每一個項目的描述
Example?11.8.?Providing a description for a project 提供一個項目的描述
build.gradle
description?=?'The?shared?API?for?the?application'
11.6.2.?Listing tasks 列出任務
Running gradle tasks gives you a list of the main tasks of the selected project. This report shows the default tasks for the project, if any, and a description for each task. Below is an example of this report:
<翻譯>?運行'gralde tasks'能夠列出所選項目的主任務。一般會顯示項目的默認任務及描述。看下面這個例子:
Example?11.9.?Obtaining information about tasks 獲取任務信息
Output?of?gradle?-q?tasks
>?gradle?-q?tasks
------------------------------------------------------------
All tasks runnable from root project
Default?tasks:?dists
Build tasks
clean?-?Deletes?the?build?directory?(build)
**<翻譯>**?刪除build目錄
dists?-?Builds?the?distribution
**<翻譯>**?
libs?-?Builds?the?JAR
**<翻譯>**?
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?'projectReports'.?[incubating]
**<翻譯>**?
dependencies?-?Displays?all?dependencies?declared?in?root?project?'projectReports'.
**<翻譯>**?
dependencyInsight?-?Displays?the?insight?into?a?specific?dependency?in?root?project?'projectReports'.
**<翻譯>**?
help?-?Displays?a?help?message.
**<翻譯>**?
model?-?Displays?the?configuration?model?of?root?project?'projectReports'.?[incubating]
**<翻譯>**?
projects?-?Displays?the?sub-projects?of?root?project?'projectReports'.
**<翻譯>**?
properties?-?Displays?the?properties?of?root?project?'projectReports'.
**<翻譯>**?
tasks?-?Displays?the?tasks?runnable?from?root?project?'projectReports'?(some?of?the?displayed?tasks?may?belong?to?subprojects).
**<翻譯>**?
To?see?all?tasks?and?more?detail,?run?gradle?tasks?--all
**<翻譯>**?
To?see?more?detail?about?a?task,?run?gradle?help?--task?<task>
**<翻譯>**?
By default, this report shows only those tasks which have been assigned to a task group. You can do this by setting the group property for the task. You can also set the description property, to provide a description to be included in the report.
<翻譯>?
Example?11.10.?Changing the content of the task report
build.gradle
dists?{
description?=?'Builds?the?distribution'
group?=?'build'
}
You can obtain more information in the task listing using the --all option. With this option, the task report lists all tasks in the project, grouped by main task, and the dependencies for each task. Here is an example:
<翻譯>?
Example?11.11.?Obtaining more information about tasks
Output?of?gradle?-q?tasks?--all
>?gradle?-q?tasks?--all
------------------------------------------------------------
All?tasks?runnable?from?root?project
<翻譯>
Default?tasks:?dists
Build tasks
clean?-?Deletes?the?build?directory?(build)
**<翻譯>**?
api:clean?-?Deletes?the?build?directory?(build)
**<翻譯>**?
webapp:clean?-?Deletes?the?build?directory?(build)
**<翻譯>**?
dists?-?Builds?the?distribution?[api:libs,?webapp:libs]
**<翻譯>**?
docs?-?Builds?the?documentation
**<翻譯>**?
api:libs?-?Builds?the?JAR
**<翻譯>**?
api:compile?-?Compiles?the?source?files
**<翻譯>**?
webapp:libs?-?Builds?the?JAR?[api:libs]
**<翻譯>**?
webapp:compile?-?Compiles?the?source?files
**<翻譯>**?
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?'projectReports'.?[incubating]
**<翻譯>**?
api:components?-?Displays?the?components?produced?by?project?':api'.?[incubating]
**<翻譯>**?
webapp:components?-?Displays?the?components?produced?by?project?':webapp'.?[incubating]
**<翻譯>**?
dependencies?-?Displays?all?dependencies?declared?in?root?project?'projectReports'.
**<翻譯>**?
api:dependencies?-?Displays?all?dependencies?declared?in?project?':api'.
**<翻譯>**?
webapp:dependencies?-?Displays?all?dependencies?declared?in?project?':webapp'.
**<翻譯>**?
dependencyInsight?-?Displays?the?insight?into?a?specific?dependency?in?root?project?'projectReports'.
**<翻譯>**?
api:dependencyInsight?-?Displays?the?insight?into?a?specific?dependency?in?project?':api'.
**<翻譯>**?
webapp:dependencyInsight?-?Displays?the?insight?into?a?specific?dependency?in?project?':webapp'.
**<翻譯>**?
help?-?Displays?a?help?message.
**<翻譯>**?
api:help?-?Displays?a?help?message.
**<翻譯>**?
webapp:help?-?Displays?a?help?message.
**<翻譯>**?
model?-?Displays?the?configuration?model?of?root?project?'projectReports'.?[incubating]
**<翻譯>**?
api:model?-?Displays?the?configuration?model?of?project?':api'.?[incubating]
**<翻譯>**?
webapp:model?-?Displays?the?configuration?model?of?project?':webapp'.?[incubating]
**<翻譯>**?
projects?-?Displays?the?sub-projects?of?root?project?'projectReports'.
**<翻譯>**?
api:projects?-?Displays?the?sub-projects?of?project?':api'.
**<翻譯>**?
webapp:projects?-?Displays?the?sub-projects?of?project?':webapp'.
**<翻譯>**?
properties?-?Displays?the?properties?of?root?project?'projectReports'.
**<翻譯>**?
api:properties?-?Displays?the?properties?of?project?':api'.
**<翻譯>**?
webapp:properties?-?Displays?the?properties?of?project?':webapp'.
**<翻譯>**?
tasks?-?Displays?the?tasks?runnable?from?root?project?'projectReports'?(some?of?the?displayed?tasks?may?belong?to?subprojects).
**<翻譯>**?
api:tasks?-?Displays?the?tasks?runnable?from?project?':api'.
**<翻譯>**?
webapp:tasks?-?Displays?the?tasks?runnable?from?project?':webapp'.
**<翻譯>**?
11.6.3.?Show task usage details
Running gradle help --task someTask gives you detailed information about a specific task or multiple tasks matching the given task name in your multiproject build. Below is an example of this detailed information:
<翻譯>?
Example?11.12.?Obtaining detailed help for tasks
Output?of?gradle?-q?help?--task?libs
>?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
Group
?build
This information includes the full task path, the task type, possible commandline options and the description of the given task.
<翻譯>?
11.6.4.?Listing project dependencies
Running gradle dependencies gives you a list of the dependencies of the selected project, broken down by configuration. For each configuration, the direct and transitive dependencies of that configuration are shown in a tree. Below is an example of this report:
<翻譯>?
Example?11.13.?Obtaining information about dependencies
Output?of?gradle?-q?dependencies?api:dependencies?webapp: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.10
testCompile
\---?junit:junit:4.12
?\---?org.hamcrest:hamcrest-core:1.3
------------------------------------------------------------
Project?:webapp?-?The?Web?application?implementation
<翻譯>
compile
+---?project?:api
|????\---?org.codehaus.groovy:groovy-all:2.3.10
\---?commons-io:commons-io:1.2
testCompile
No?dependencies
Since a dependency report can get large, it can be useful to restrict the report to a particular configuration. This is achieved with the optional --configuration parameter:
<翻譯>?
Example?11.14.?Filtering dependency report by configuration
Output?of?gradle?-q?api:dependencies?--configuration?testCompile
>?gradle?-q?api:dependencies?--configuration?testCompile
------------------------------------------------------------
Project :api - The shared API for the application
testCompile
\---?junit:junit:4.12
?\---?org.hamcrest:hamcrest-core:1.3
11.6.5.?Getting the insight into a particular dependency
Running gradle dependencyInsight gives you an insight into a particular dependency (or dependencies) that match specified input. Below is an example of this report:
<翻譯>?
Example?11.15.?Getting the insight into a particular dependency
Output?of?gradle?-q?webapp:dependencyInsight?--dependency?groovy?--configuration?compile
>?gradle?-q?webapp:dependencyInsight?--dependency?groovy?--configuration?compile
org.codehaus.groovy:groovy-all:2.3.10
\---?project?:api
?\---?compile
This task is extremely useful for investigating the dependency resolution, finding out where certain dependencies are coming from and why certain versions are selected. For more information please see the DependencyInsightReportTask class in the API documentation.
<翻譯>?
The built-in dependencyInsight task is a part of the 'Help' tasks group. The task needs to configured with the dependency and the configuration. The report looks for the dependencies that match the specified dependency spec in the specified configuration. If Java related plugin is applied, the dependencyInsight task is pre-configured with 'compile' configuration because typically it's the compile dependencies we are interested in. You should specify the dependency you are interested in via the command line '--dependency' option. If you don't like the defaults you may select the configuration via '--configuration' option. For more information see the DependencyInsightReportTask class in the API documentation.
<翻譯>?
11.6.6.?Listing project properties
Running gradle properties gives you a list of the properties of the selected project. This is a snippet from the output:
<翻譯>?
Example?11.16.?Information about properties
Output?of?gradle?-q?api:properties
>?gradle?-q?api:properties
------------------------------------------------------------
Project :api - The shared API for the application
allprojects:?[project?':api']
ant:?org.gradle.api.internal.project.DefaultAntBuilder@12345
antBuilderFactory:?org.gradle.api.internal.project.DefaultAntBuilderFactory@12345
artifacts:?org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@12345
asDynamicObject:?org.gradle.api.internal.ExtensibleDynamicObject@12345
baseClassLoaderScope:?org.gradle.api.internal.initialization.DefaultClassLoaderScope@12345
buildDir:?/home/user/gradle/samples/userguide/tutorial/projectReports/api/build
buildFile:?/home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle
11.6.7.?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.
<翻譯>?
11.7.?Dry Run
Sometimes you are interested in which tasks are executed in which order for a given set of tasks specified on the command line, but you don't want the tasks to be executed. You can use the -m option for this. For example, if you run “gradle -m clean compile”, you'll see all the tasks that would be executed as part of the clean and compile tasks. This is complementary to the tasks task, which shows you the tasks which are available for execution.
<翻譯>?
11.8.?Summary
In this chapter, you have seen some of the things you can do with Gradle from the command-line. You can find out more about the gradle command in Appendix?D, Gradle Command Line.
<翻譯>
轉載于:https://www.cnblogs.com/wust221/p/5427760.html
總結
以上是生活随笔為你收集整理的【转载】Gradle学习 第十一章:使用Gradle命令行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初识HTML流水笔记
- 下一篇: Eclipse中设置在创建新类时自动生成