5 Gradle build script Getting started

Previous We already know the importance of Gradle build script, to be used to build more complex projects you will need to write build scripts that Gradle build script, what does? Look at the build script introduced.

Build scripts Introduction

Gradle build script two most important concept is that project and Task , any Gradle build a project by one or more, each comprising a number of building part project, the package may be a jar, and may be a web application, can also consolidate multiple jar, you can deploy applications and build environments. Gradle's build-by-convention can specify what specific project to do.

Each project consists of one or more task, each task represents an atomic operation performed in the process of building. Such as compile, package, generate javadoc, posted to the warehouse and other operations.

Project and task relationship as shown below:

807144-5c6367c4b8dd12f5
The relationship between the project and task

FIG: Project1 Project2 dependent, so the need to build Project2, Project2 there are three tasks Task FGH, because dependencies, execute Task G, then execute Task F, then execute Task H. The Task Project1 also be relied upon to perform tasks.

What's Project in the end is it? What's in it? The following we will look at the Project.

Project

Component (jar / war file) is a representative of a construction project, when building starts, based on the Gradle will instantiate a org.gradle.api.Project build.gradle object. And by project variables implicitly calling its members. That members of the Project, what does?

Project Properties

first name Types of Defaults
project Project project examples
group Object Project group: not specified
name String Project directory name
version Object Project Version: not specified
path String Project absolute path
description String project description
projectDir File It contains build scripts directory
buildDir File projectDir/build
ant AntBuilder Examples AntBuilder

There are common attributes project (implicit use), group, name, version.

Other common configuration Project

  • plugins, apply plugin for introducing Plugins
  • dependencies depend on configuration
  • repositories warehouse configuration
  • task tasks writing
  • Other configurations Project attributes in ext, gradle.properties

More than we already have some knowledge of the project, and know Gradle build the project is inseparable from the Project object, the above configuration will be packaged into the Project object, but the same can not be separated in Project task (task), the following we will look at what the next is a task, task What is the role?

task mission briefing

In constructing each task will be executed org.gradle.api.Task encapsulated into objects. The main tasks include action and task dependencies. Task action defines an atomic operation. You may be defined depending on other tasks, actions, and the order of execution conditions. What tasks related operations that have it?

The main task of operating actions

  • dependsOn: related operations rely on
  • doFirst: method performed before task execution
  • doLast, <<: method to perform after the task execution

example:

// 定义一个简单的任务
task t1 {
    println 'hello t1'
}
// t2 执行时依赖 t1
task t2 (dependsOn : 't1'){
    //  t2 执行前执行操作,
    doFirst{
        println 'do first'
    }
    println 'hello t2'
    // t2 执行后执行操作,
    doLast{
        println 'do last'
    }
}

Command: gradle t2 or double IDEA -> right side of Gradle -> Task -> other -> t2

> Configure project :
hello t1
hello t2

> Task :t1

> Task :t2
do first
do last

These results can be seen directly in the task of custom code will be executed when configuring project, other times not executed, even if the dependent is not executed. Only in doFirst or doLast configured to operate only invoked when the task or rely on execution. These are simple to use task, as to define a look at the details of the task: "Use custom task Gradle task."

Reprinted link: https://www.jianshu.com/p/59a747cf2475

Guess you like

Origin blog.csdn.net/weixin_34236497/article/details/91014751