Gradle构建Java程序:Gradle常用命令

Gradle通用命令格式:

1
gradle [option ...] [task ...]

运行gradle相关命令后,可以看到依次执行的tasks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> gradle build
:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

帮助命令:

1
2
gradle help
gradle --help

执行完整的项目构建(生成class文件、jar文件、执行单元测试等):

1
gradle build

编译源代码,并生成jar文件(不执行单元测试):

1
gradle assemble

删除构建目录:

1
gradle clean

编译源代码:

1
gradle classes

编译测试类的代码:

1
gradle testClasses

生成jar文件:

1
gradle jar

生成Javadoc API文档:

1
gradle javadoc

显示项目的properties

1
gradle properties

显示当前项目的可运行task的完整列表:

1
gradle tasks

通过这种方式,不需要阅读构建脚本,就能对项目进行大致的浏览。由于Java插件在构建中自动加入了很多任务,因此上述命令可以看到build文件中之外的其它一些task。

在某个项目下,上述命令运行后,terminal显示如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
: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.

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
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'testJunit'.
components - Displays the components produced by root project 'testJunit'. [incubating]
dependencies - Displays all dependencies declared in root project 'testJunit'.
dependencyInsight - Displays the insight into a specific dependency in root project 'testJunit'.
help - Displays a help message.
model - Displays the configuration model of root project 'testJunit'. [incubating]
projects - Displays the sub-projects of root project 'testJunit'.
properties - Displays the properties of root project 'testJunit'.
tasks - Displays the tasks runnable from root project 'testJunit'.

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

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>

BUILD SUCCESSFUL

Total time: 3.786 secs