Gradle应用简介

1. 存储库

默认情况下,Gradle不定义任何存储库。我们必须至少明确地定义一个存储库。

2. 显示帮助信息

$ gradle --help

  执行 gradle --help 可查看gradle命令语法及各选项意义,如下:

$ gradle --help

USAGE: gradle [option...] [task...]

-?, -h, --help            Shows this help message.
-a, --no-rebuild          Do not rebuild project dependencies. [deprecated]
-b, --build-file          Specify the build file.
--build-cache             Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. [incubating]
-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.
--foreground              Starts the Gradle Daemon in the foreground. [incubating]
-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. [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. [incubating]
--no-build-cache          Disables the Gradle build cache. [incubating]
--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. [incubating]
--no-scan                 Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans. [incubating]
--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. [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.
--recompile-scripts       Force build script recompiling. [deprecated]
--refresh-dependencies    Refresh the state of dependencies.
--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) [incubating]
--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. [incubating]
-u, --no-search-upward    Don't search in parent folders for a settings file.
-v, --version             Print version info.
-w, --warn                Set log level to warn.
-x, --exclude-task        Specify a task to be excluded from execution.

  语法,启动一个构建:

$ gradle <task> ...

  选项,如:
- -continue选项使发生故障时继续执行
- -b选项选择执行哪些构,可以使用选择指定的构建文件的路径。

3. 列出项目下可执行的所有任务

$ gradle tasks
# or
$ gradle -q tasks

  执行gradle -q tasks可看到一个Gradle项目中常用的任务(命令)如下:

$ gradle -q 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 main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
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.
wrapper - Generates Gradle wrapper files.

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

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradleWebApp'.
components - Displays the components produced by root project 'gradleWebApp'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradleWebApp'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradleWebApp'.
dependentComponents - Displays the dependent components of components in root project 'gradleWebApp'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradleWebApp'. [incubating]
projects - Displays the sub-projects of root project 'gradleWebApp'.
properties - Displays the properties of root project 'gradleWebApp'.
tasks - Displays the tasks runnable from root project 'gradleWebApp'.

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

Web application tasks
---------------------
tomcatJasper - Runs the JSP compiler and turns JSP pages into Java source.
tomcatRun - Uses your files as and where they are and deploys them to Tomcat.
tomcatRunWar - Assembles the webapp into a war and deploys it to Tomcat.
tomcatStop - Stops Tomcat.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

4. 查看某一个任务的详细信息

$ gradle help --task <task>

  如执行gradle help --task tomcatRun可看到tomcatRun任务如下:

$ gradle help --task tomcatRun
# 输出:
:help
Detailed task information for tomcatRun

Path
     :tomcatRun

Type
     TomcatRun (com.bmuschko.gradle.tomcat.tasks.TomcatRun)

Description
     Uses your files as and where they are and deploys them to Tomcat.

Group
     web application

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

猜你喜欢

转载自blog.csdn.net/leonliu06/article/details/79015125