Gradle 运行构建



  • 运行构建

    Gradle提供了一个命令行来执行构建脚本。它一次可以执行多个任务。本章说明如何使用不同的选项执行多个任务。
  • 执行多个任务

    您可以从一个构建文件中执行多个任务。Gradle可以使用gradle命令处理该构建文件。此命令将按照列出的顺序编译每个任务,并使用不同的选项执行每个任务以及相关性。
    示例 - 有四个任务- Task1,Task2,Task3和Task4。Task3和Task4依赖于Task1和Task2。看下图。
    gradle tasks
    在上面的4个任务中,彼此依赖,用箭头符号表示。看一下下面的代码。复制可以将其粘贴到build.gradle文件中。
    
    task task1  {
        doLast{ 
            println 'compiling source'
        }
    }
    
    task task2(dependsOn: task1)  {
        doLast{ 
            println 'compiling unit tests'
        }
    }
    
    task task3(dependsOn: [task1, task2])  {
        doLast{ 
            println 'running unit tests'
        }
    }
    
    task task4(dependsOn: [task1, task3])  {
        doLast{ 
            println 'building the distribution'
        }
    }
    
    
    使用以下代码执行上述脚本。
    
    C:\> gradle -q task4
    
    
    输出:
    
    compiling source
    compiling unit tests
    running unit tests
    building the distribution
    
    
  • 排除任务

    从执行中排除任务时,可以将–x选项与gradle命令一起使用,并提及要排除的任务名称。使用以下命令从上述脚本中排除task1。
    
    gradle -x task1 task4
    
    
    输出:
    
    > Task :task2
    compiling unit tests
    
    > Task :task3
    running unit tests
    
    > Task :task4
    building the distribution
    
    BUILD SUCCESSFUL in 1s
    3 actionable tasks: 3 executed
    
    
  • 发生故障时继续构建

    Gradle将中止执行,并在任何任务失败后立即使构建失败。即使发生故障,您也可以继续执行。为此,您必须在gradle命令中使用–continue选项。它分别处理每个任务及其依赖性。最重要的一点是它将捕获每个遇到的故障并在构建执行结束时报告。假设如果某个任务失败,那么相关的后续任务也将不会执行。
  • 选择要执行的构建

    运行gradle命令时,它将在当前目录中查找构建文件。您可以使用–b选项选择特定的构建文件以及绝对路径。下面的示例从位于subdir/中的myproject.gradle文件中选择项目问候。
    
    task hello  {
        doLast{
            println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
        }
    }
    
    
    您可以使用以下命令执行上述脚本。
    
    C:\> gradle -q -b subdir/myproject.gradle hello
    
    
    输出:
    
    using build file 'myproject.gradle' in 'subdir'.
    
    
  • 获取构建信息

    Gradle提供了一些内置任务,用于检索有关任务和项目的信息详细信息。这对于了解构建的结构和依赖性以及调试问题很有用。您可以使用项目报告插件将任务添加到项目中,这将生成这些报告。
    项目列表
    您可以使用gradle –q projects命令列出所选项目及其子项目的项目层次结构。这是示例,使用以下命令列出构建文件中的所有项目。
    
    C:\> 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
    
    
    如果指定,该报告将显示每个项目的描述。您可以使用以下命令指定描述。将其粘贴到build.gradle文件中。
    
    description = 'The shared API for the application'
    
    
    列出任务
    您可以使用以下命令列出属于多个项目的所有任务。
    
    C:\> 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
    ----------
    buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'.
    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>
    
    
    您可以使用以下命令显示所有任务的信息。
    
    C:\> 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
    ----------
    buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'.
    api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'.
    webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'.
    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'.    
    
    
    这是表中的一些命令列表,描述了不同的选项。
    • gradle –q help –task <task name> - 提供有关特定任务或多个任务的使用信息(例如路径,类型,描述,组)。
    • gradle –q dependencies - 提供所选项目的依赖项列表。
    • gradle -q api:dependencies --configuration <task name> - 提供与配置有关的受限依赖项列表。
    • gradle –q buildEnvironment - 提供构建脚本依赖项列表。
    • gradle –q dependencyInsight - 提供对特定依赖项的见解。
    • gradle –q properties - 提供所选项目的属性列表。
    可以用gradle –h 查看所有可用的选项很命令
    
    > C:\>gradle -h
    
    USAGE: gradle [option...] [task...]
    
    -?, -h, --help                     Shows this help message.
    -a, --no-rebuild                   Do not rebuild project dependencies.
    -b, --build-file                   Specify the build file.
    --build-cache                      Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
    -c, --settings-file                Specify the settings file.
    --configure-on-demand              Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
    --console                          Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'.
    --continue                         Continue task execution after a task failure.
    -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.
    --export-keys                      Exports the public keys used for dependency verification. [incubating]
    -F, --dependency-verification      Configures the dependency verification mode (strict, lenient or off) [incubating]
    --foreground                       Starts the Gradle Daemon in the foreground.
    -g, --gradle-user-home             Specifies the gradle user home directory.
    -I, --init-script                  Specify an initialization script.
    -i, --info                         Set log level to info.
    --include-build                    Include the specified build in the composite.
    -M, --write-verification-metadata  Generates checksums for dependencies used in the project (comma-separated list) [incubating]
    -m, --dry-run                      Run the builds with all task actions disabled.
    --max-workers                      Configure the number of concurrent workers Gradle is allowed to use.
    --no-build-cache                   Disables the Gradle build cache.
    --no-configure-on-demand           Disables the use of configuration on demand. [incubating]
    --no-daemon                        Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default.
    --no-parallel                      Disables parallel execution to build projects.
    --no-scan                          Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans.
    --offline                          Execute the build without accessing network resources.
    -P, --project-prop                 Set project property for the build script (e.g. -Pmyprop=myvalue).
    -p, --project-dir                  Specifies the start directory for Gradle. Defaults to current directory.
    --parallel                         Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use.
    --priority                         Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low' [incubating]
    --profile                          Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
    --project-cache-dir                Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
    -q, --quiet                        Log errors only.
    --refresh-dependencies             Refresh the state of dependencies.
    --refresh-keys                     Refresh the public keys used for dependency verification. [incubating]
    --rerun-tasks                      Ignore previously cached task results.
    -S, --full-stacktrace              Print out the full (very verbose) stacktrace for all exceptions.
    -s, --stacktrace                   Print out the stacktrace for all exceptions.
    --scan                             Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans)
    --status                           Shows status of running and recently stopped Gradle Daemon(s).
    --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.
    --update-locks                     Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating]
    -v, --version                      Print version info.
    -w, --warn                         Set log level to warn.
    --warning-mode                     Specifies which mode of warnings to generate. Values are 'all', 'fail', 'summary'(default) or 'none'
    --write-locks                      Persists dependency resolution for locked configurations, ignoring existing locking information if it exists [incubating]
    -x, --exclude-task                 Specify a task to be excluded from execution.