HelloWorld


Zhikang Li's personal blog


basic reference of gradle

LifeCycle

Phase

######Initialization
Gradle determines which projects are going to take part in the build, and creates a Project instance for each of these projects

######Configuration
The build scripts of all projects which are part of the build are executed

Execution

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed

setting files

settings.gradle
  • The settings file is executed during the initialization phase. A multiproject build must have a settings.gradle, for singleproject it’s optional.
  • building tree
    1
    include 'project1', 'project2:child', 'project3:child1'
build.gradle
  • are build scrpit files
  • defines a project in Gradle
  • execute after initialization

language(script)

everything sits on top of two basic concepts: projects and tasks.
You run a Gradle build using the gradle command. The gradle command looks for a file called build.gradle in the current directory

concepts

project

What a project represents depends on what it is that you are doing with Gradle

task

Each project is made up of one or more tasks. A task represents some atomic piece of work which a build performs

task dependencies

build.gradle

1
2
3
4
5
6
7
8
9
10
task taskX(dependsOn: 'taskY') {
doLast {
println 'TaskX'
}
}
task taskY {
doLast {
println 'TaskY'
}
}

cmdline

1
2
3
> gradle -q hello
TaskY
TaskX

with the -q command-line option. This suppresses Gradle’s log messages

task manipulating

via API

add dependency

task0.dependsOn task2, task3

add behaviour

The calls doFirst and doLast can be executed multiple times. They add an action to the beginning or the end of the task’s actions list. When the task executes, the actions in the action list are executed in order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
task hello {
doLast {
println 'Hello Earth'
}
}
hello.doFirst {
println 'Hello Venus'
}
hello.doLast {
println 'Hello Mars'
}
hello {
doLast {
println 'Hello Jupiter'
}
}

1
2
3
4
5
> gradle -q hello
Hello Venus
Hello Earth
Hello Mars
Hello Jupiter

JAVA

CMDS

gradle tasks: list the tasks of a project.
gradle build: Gradle will compile and test your code, and create a JAR file containing your main classes and resources
gradle clean: Deletes the build directory, removing all built files.
gradle assemble: Compiles and jars your code, but does not run the unit tests
gradle check: Compiles and tests your code