在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 物聯(lián)網(wǎng)/ 使用 Gradle 命令行
快速開始 Web 應(yīng)用
更多關(guān)于任務(wù)
編寫構(gòu)建腳本
守護進程
問題解決
日志
插件
總覽
快速開始 Java
教程-"This and That"
使用 Gradle 圖形化用戶界面
從 Gradle 使用 Ant
依賴管理的基礎(chǔ)
構(gòu)建環(huán)境
處理文件
構(gòu)建腳本的基本
使用 Gradle 命令行
快速開始 Groovy
安裝
介紹

使用 Gradle 命令行

本章介紹了 Gradle 命令行的基本知識。正如在前面的章節(jié)里你所見到的, 調(diào)用 gradle 命令來執(zhí)行構(gòu)建。

執(zhí)行多 task

同個構(gòu)建可以執(zhí)行多個 task ,通過再命令行 列出每個 task。舉例,命令 gradle compile test 將會執(zhí)行 compile 和 test 兩個 task。 Gradle 將會按順序執(zhí)行 命令行每個列出的 task,并且執(zhí)行每個 task 的依賴。每個任務(wù)僅執(zhí)行一次,不管它是如何被包含在構(gòu)建中:無論是在命令行中指定,或作為另一個 task 的依賴,或兩者都是。讓我們看一個例子。

下面定義了 4 個 task。 dist 和 test 都依賴于 compile 。執(zhí)行 gradle dist test ,compile 將會僅僅被執(zhí)行一次。

Figure 11.1. Task dependencies

http://wiki.jikexueyuan.com/project/gradle-2-user-guide/images/commandLineTutorialTasks.png" alt="" />

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'
    }

執(zhí)行 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

每個 task 只執(zhí)行一次,所以 gradle test testgradle test 執(zhí)行結(jié)果一樣。

排除 task

可以通過 -x 命令行來排除 task 被執(zhí)行。

執(zhí)行 gradle dist -x test 輸出

    > gradle dist -x test
    :compile
    compiling source
    :dist
    building the distribution

    BUILD SUCCESSFUL

Total time: 1 secs

可以看到 ,test 并未執(zhí)行,即使它是 dist 的依賴。同時注意到, test 的依賴,如 compileTest 也未執(zhí)行。這些 test 的依賴如果是被其他 task 所需要的話,如 compile 仍會執(zhí)行。

發(fā)生故障時繼續(xù)構(gòu)建

默認情況下,只要任何 task 失敗,Gradle 將中止執(zhí)行。這使得構(gòu)建更快地完成,但隱藏了其他可能發(fā)生的故障。為了發(fā)現(xiàn)在一個單一的構(gòu)建中多個可能發(fā)生故障的地方,你可以使用 --continue 選項。

通過執(zhí)行 --continue ,Gralde 會執(zhí)行每一個 task ,當那個 task 所有的依賴都無故障的執(zhí)行完成, 而不是一旦出現(xiàn)錯誤就會中斷執(zhí)行。所有故障信息都會在最后報告出來。

一旦某個 task 執(zhí)行失敗,那么所有依賴于該 task 的后面的 task 都不會被執(zhí)行,因為這樣做不安全。例如,在測試時,當編譯代碼失敗則測試不會執(zhí)行。因為 測試 task 將取決于 編譯 task(不管是直接或間接)。

任務(wù)名稱縮寫

當你試圖執(zhí)行某個 task 的時候,無需輸入 task 的全名.只需提供足夠的可以唯一區(qū)分出該 task 的字符即可。例如,上面的例子你也可以這么寫, 用 gradle di 來直接調(diào)用 dist 。

Example 11.3. Abbreviated task name

執(zhí)行 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

同時也可以應(yīng)用在駝峰的 task 名稱。如,執(zhí)行 compileTest 時,運行 gradle compTest 或者 gradle cT 都可以。

Example 11.4. Abbreviated camel case task name

執(zhí)行 gradle cT

    > gradle cT
    :compile
    compiling source
    :compileTest
    compiling unit tests

    BUILD SUCCESSFUL

    Total time: 1 secs

同時,也可以應(yīng)用在 -x 命令選項。

選擇要執(zhí)行的構(gòu)建

調(diào)用 gradle 命令時,默認情況下總是會在當前目錄下尋找構(gòu)建文件(譯者注:首先會尋找當前目錄下的 build.gradle 文件,以及根據(jù)settings.gradle 中的配置尋找子項目的 build.gradle )。 可以使用 -b 參數(shù)選擇其他的構(gòu)建文件,并且當你使用此參數(shù)時 settings.gradle 將不會被使用,看下面的例子:

Example 11.5. Selecting the project using a build file

subdir/myproject.gradle

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

執(zhí)行 gradle -q -b subdir/myproject.gradle hello 輸出

    > gradle -q -b subdir/myproject.gradle hello
    using build file 'myproject.gradle' in 'subdir'.

或者,您可以使用 -p 選項來指定要使用的項目目錄。多 project 的構(gòu)建時應(yīng)使用 -p 選項來代替 -b 選項。

Example 11.6. Selecting the project using project directory

執(zhí)行 gradle -q -p subdir hello 輸出

    > gradle -q -p subdir hello
    using build file 'build.gradle' in 'subdir'.

獲取構(gòu)建信息

Gradle 提供了許多內(nèi)置 task 來收集構(gòu)建信息。這些內(nèi)置 task 對于了解依賴結(jié)構(gòu)以及解決問題都是很有幫助的。

項目列表

執(zhí)行 gradle projects 會為你列出選中項目的子項目列表。如下例。

Example 11.7. Obtaining information about projects

執(zhí)行 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
    For example, try running gradle :api:tasks

這份報告展示了每個項目的描述信息。當然你可以在項目中用 description 屬性來指定這些描述信息。。

Example 11.8. Providing a description for a project

build.gradle

    description = 'The shared API for the application'

任務(wù)列表

執(zhí)行 gradle tasks 會列出項目中所有 task。這份報告顯示項目中所有的默認 task 以及每個 task 的描述。如下

Example 11.9. Obtaining information about tasks

執(zhí)行 gradle -q tasks 輸出

    > gradle -q tasks
    ------------------------------------------------------------
    All tasks runnable from root project
    ------------------------------------------------------------

    Default tasks: dists

    Build tasks
    -----------
    clean - Deletes the build directory (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.
    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 with --all.

默認情況下,這只會顯示那些被分組的 task.你可以通過為 task 設(shè)置group 屬性和 description 來把這些信息展示到報告中

Example 11.10. Changing the content of the task report

build.gradle

    dists {
        description = 'Builds the distribution'
        group = 'build'
    }

當然你也可以用 --all 參數(shù)來收集更多 task 信息。這報告列出項目中所有被主 task 的分組的 task 以及 task 之間的依賴關(guān)系。下面是示例

Example 11.11. Obtaining more information about tasks

執(zhí)行 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.
    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'.

顯示 task 使用細節(jié)

執(zhí)行 gradle help --task someTask 可以獲取到 task 的詳細信息, 或者多項目構(gòu)建中相同 task 名稱的所有 task 的信息,如下

Example 11.12. Obtaining detailed help for tasks

執(zhí)行 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

這些結(jié)果包含了完整的 task 的路徑、類型、可能的命令行選項以及描述信息等.

賴列表

執(zhí)行 gradle dependencies 會列出項目的依賴列表,所有依賴會根據(jù)任務(wù)區(qū)分,以樹型結(jié)構(gòu)展示出來。如下

Example 11.13. Obtaining information about dependencies

執(zhí)行 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.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 可選參數(shù)來實現(xiàn)。

Example 11.14. Filtering dependency report by configuration

執(zhí)行 gradle -q api:dependencies --configuration testCompile 輸出為

    > 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

查看特定依賴

執(zhí)行 gradle dependencyInsight 可以查看指定的依賴情況,如下

Example 11.15. Getting the insight into a particular dependency

執(zhí)行 gradle -q webapp:dependencyInsight --dependency groovy --configuration compile 輸出

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

這對于分辨依賴、了解依賴關(guān)系、了解為何選擇此版本作為依賴十分有用。了解更多請參閱 DependencyInsightReportTask 類的 API

內(nèi)建的 dependencyInsight 是'Help' task 分組中的一個。這項 task 需要進行依賴和配置文件的配置才可以。該報告尋找那些與定依賴規(guī)范指定的配置匹配的的依賴。如果應(yīng)用了 Java 相關(guān)的插件,該dependencyinsight task 是預(yù)先經(jīng)過 'compile' 配置,因為它通常依賴我們感興趣的編譯。你應(yīng)該指定您感興趣的依賴,通過命令行 '--dependency'選項。如果你不喜歡默認的,你可以選擇通過 '--configuration' 選項來配置。更多信息見DependencyInsightReportTask 類的API文檔。

項目屬性列表

執(zhí)行 gradle properties 可以獲取項目所有屬性列表,如下

Example 11.16. Information about properties

執(zhí)行 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

Profiling a build

--profile 命令選項可以記錄一些構(gòu)建期間的信息并保存到 build/reports/profile 目錄下并且以構(gòu)建時間命名這些文件

該報告列出總時間和在配置和 task 的執(zhí)行 階段的細節(jié)。并以時間大小倒序排列,并且記錄了任務(wù)的執(zhí)行情況

如果采用了 buildSrc 構(gòu)建,那么在 buildSrc/build 下同時也會給 buildSrc 生成一份日志記錄

http://wiki.jikexueyuan.com/project/gradle-2-user-guide/images/profile.png" alt="" />

執(zhí)行

有時可能你只想知道某個 task 在一個 task 集中按順序執(zhí)行的結(jié)果,但并不想實際執(zhí)行這些 task 。那么你可以用 -m 選項。例如 執(zhí)行 gradle -m clean compile 將會看到所有的作為 clean 和 compile 一部分的 task 會被執(zhí)行。這與 task 可以形成互補,讓你知道哪些 task 可以用于執(zhí)行。

總結(jié)

本章看到了命令行的一部分。更多詳見 Appendix D. Gradle Command Line 命令行

上一篇:守護進程下一篇:日志