Gradle- build script

mind Mapping

Construction of Language

Gradle provides a domain-specific language, currently supports both Groovy and Kotlin.

In Groovy build script (.gradle) Groovy you can use any element.

In Kotlin build script (.gradle.kts) you can use any Kotlin elements.

Project (Project) and tasks (Task)

All Gradle build is based on two concepts: projects and tasks;

A building is composed of one or more items. Abstract concept project, you can create a Project for generating a jar, you can also define a project for generating war package, you can also define a project for publishing upload your war and so on.

A project that is in your business, you are abstracted from an independent module, you can abstract categorized according to the actual situation of the project, all of the final composition of the entire project Gradle build.

A project also contains a number of tasks, each project is composed of one or more tasks. A task is operating, an atomic operation. For example, make a jar package, a copy of the document, compiled a java code, etc., this is a task.

build.gradle & Project API

Each project has a build.gradle file, which is the entrance of the building project, the project can be configured in this this file, such as configuration version, which require plug-ins, which libraries dependence.

We build our description of the configuration file, which is actually a configuration script.

Each script will be executed when linked to a Project instance.

In the initialization phase of the build lifecycle, Gradle creates an instance of Project for each project, and configure the instance based on the contents of build.gradle.

That build.gradle each configuration will be configured to the Project instance.

Indeed, build.gradle almost all of the top and the properties of the Project class code blocks are API,

Below verify by visiting Project.name property.

在 build.gradle

println "name is $name"
println "project.name is ${project.name}"

Execute build tasks, you'll get the name of the output value of the output of the following items are
Verify output

The first statement using the Project's top properties.

The second project using the property statement can be accessed anywhere in the script, it represents the current Project Object script.

Only when you define the members of the Project and the (methods, properties) only need to use the same name project, other times you can access directly use the name, for example, the first statement.

A construct is composed of a plurality Project, the project is in the form of a tree representation.

Some configuration can be unified configuration of all the items in the root project tree. For example, application plug-ins rely on Maven central repository and so on.

For all subprojects configured warehouse jcenter

subprojects{
    repositories {
        jcenter()
    }
}

All sub-projects can also be configured to use the Java plug-in

subprojects{
    apply plugin:'java'
    repositories {
        jcenter()
    }
}

In addition to subprojects there are allprojects, it can be seen from the name this is not only the configuration of subprojects but the configuration of all projects.

These two configuration actually two methods accept a closure parameters, the project traversal, traverse the call of our custom closure, so we can close the bag configuration, print, export or modify the properties of the Project .

Project properties

Project object attributes in the global script can all be used.

Here are some commonly used properties, properties can be more comprehensive in the Project API query.

first name Types of Defaults
project Project Project examples
name String Project name
path String Absolute path of the project
description String project description
projectDir File The directory where the configuration script
buildDir File projectDir / build output directory
group Object Unspecified
version Object Unspecified
ant AntBuilder Examples AntBuilder

settings.gradle

This is a setup file, used to configure and initialize the project tree. The default settings file name is settings.gradle, down on the root project directory.

On building life cycle and settings.gradle more detailed you can see my article Build lifecycle

script API

When Gradle Groovy script execution (.gradle), will compile the script to achieve the Script class.

That is, all the properties and methods Script interface can be used in the script.

When Gradle script execution Kotlin (.gradle.kts), will compile the script to KotlinBuildScript subclass.

That is, all the properties and methods KotlinBuildScript class can be used in the script.

More detailed reference may KotlinSettingsScript and KotlinInitScript classes, and scripts are used to set init script.

That script codes

While we write the script in a file Gradle, but we are writing the code, it must be very clear.

We wrote the script really is, but it is not simple script. Script can be defined in the Class, inner class, import the package, define methods, constants, and other interfaces.

Do not treat it as a simple script that we have the flexibility to use Java, Groovy, Kotlin and Gradle.

For example, define a method of obtaining the current date

def buildTime(){
    def date = new Date()
    def formattedDate = date.format('yyyyMMdd')
    return formattedDate
}

# Variable & additional custom attributes

Gradle supports two types of variables: local variables and custom properties

## local variables

Def keyword to declare local variables, local variables are visible only within the stated range.

def myName = '佛系编码'

Additional custom properties

Gradle domain model all objects can add additional custom properties.

Achieve the operation of custom properties added, access, provided by the value of the object property ext.

After reading the property ext can add custom properties and settings, you may be added simultaneously a plurality of custom attributes.

//为 project 添加一个 age 属性 并赋值 20
ext.age = 20

//为 project 添加两个属性
ext{
    phone =110
    address = '404'
}

task myTask {
    //为 myTask 任务添加属性
    ext.myProperty = "myValue"
}
task extra{
     doLast{
         println "project : age= ${project.ext.age},phone= ${project.ext.phone} , address = ${project.ext.address}"
         println "myTask :  ${myTask.myProperty}"
     }

}

# Create a task

task hello {
    doLast {
        println 'Hello world!'
    }
}

Here's task looked like a keyword, is actually a method, the prototype of this approach is TaskContainer.create ()

Create a task it is to use this method to add a Task type attribute to the Project; so in order to use the task name refers to some of the API, such as adding additional attributes for the task.

# Task dependencies and task sequencing

A task can depend on other tasks or perform other tasks to perform before.

Gradle task dependencies and ensure compliance with all collation in carrying out its mandate, in order to perform the task, after all dependencies and any "must run" of task execution.

Gradle dependency and provides several methods for controlling the sorting task for us is that in a few below

These methods can receive tasks, task name, path, etc., specific parameters may Task document View Lane

task hello {
    doLast {
        println 'Hello world!'
    }
}

task taskX {
    dependsOn hello
    doLast{
        println "I'm  $name."
    }
}

task taskY {
    doFirst {
        println "I'm $name."
    }
}

If taskX to rely taskY words and can not be directly referenced as taskY is defined in the following taskX.

task taskX {
    dependsOn 'taskY'
    doLast{
        println "I'm  $name."
    }
}

Default Task

In the absence of specified tasks, you can define a default task in the script, using the method defaultTasks

This method accepts a string argument, you can pass in the name of the task ·

defaultTasks 'hello','taskY'

External dependencies

By adding external dependencies, you must add a dependency where the warehouse. For example jcenter, maven, google etc.

Currently it supports many types of warehouse, basically listed here, you can view warehouse type

Add google warehouse

allprojects {
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
}

In the Android project, which is placed in the root project allprojects in the method, the unified configuration for all projects

Adding external dependencies

dependencies {
    implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
}

Add in Android dependent on the individual module, the added demand, which module you need to add in the build script in which the module.

Dependent properties into three parts

  • group: This attribute is used to identify an organization, company or project, may be separated by a dot, such as the above io.reactivex.rxjava2
  • name: name attribute uniquely describe this dependence, such as the above rxjava
  • version: a library can have many versions. Such as the above 2.1.2

Where implementation is configuration items, configuration, there are many types, posted a below from Google's Note: For more information please see this dependency configuration

Dependency Configuration

Finally, the DSL API address https://docs.gradle.org/current/dsl/index.html

Guess you like

Origin www.cnblogs.com/skymxc/p/buildscript.html
Recommended