The original post was sent to jzman-blog, a wechat official account. Welcome to follow and exchange.

Gradle: Groovy: Groovy: Gradle: Groovy: Groovy: Gradle: Groovy: Groovy

  • First introduction to Gradle series
  • Gradle: Groovy Basics
  • Gradle series build script basics
  • Gradle series understanding Gradle tasks
  • Gradle plugin for Gradle series
  • Gradle series of Java Gradle plug-ins
  • Gradle series of Android Gradle plugin
  • Gradle series Android Gradle base configuration
  • Android Gradle advanced Configuration
  • The Gradle series builds Maven private server libraries from scratch

In this article, you will learn about Gradle as a whole. You will learn about Tasks, projects and other related concepts. You will also learn about some common operations that you can use to build projects using Gradle.

  1. Settings. Gradle file
  2. The Build file
  3. The Project and the Tasks
  4. Task creation
  5. Task dependent on
  6. Interactions between tasks
  7. Custom attributes
  8. conclusion

Settings. Gradle file

The Gradle settings. Gradle file is used to configure the current project. For example, in Android development, a project may have multiple sub-modules. Gradle file. Of course, only child modules configured in settings.gradle will be built. Settings. Gradle is the default configuration file name for Gradle build projects.

├─ ├─GradleSetting │ ├─4.1│ │ │ ├ ─ fileChanges │ │ │ ├ ─ fileHashes │ │ │ └ ─ taskHistory │ │ └ ─ buildOutputCleanup │ └ ─ test │ └ ─ Method │ └ ─. Gradle │ ├ ─4.1│ │ ├─ ├─ garbage cleanup │ ├─ trash cleanup │ ├─ trash cleanup │ build. Gradle │ SettingsCopy the code

In the project named GradleSetting, there is a Method subproject in the test folder. The Method subproject is built into GradleSetting by configuring settings.gradle. The settings.gradle file contains the following contents:

println "---------test----settings.gradle----------"
// Displays the current project directory
println(rootDir)
// Specify the subprojects to participate in the build
include ':Method'
project(':Method').projectDir = new File(rootDir,'test/Method')

Copy the code

Take a look at the output:

PS E:\Gradle\study\GradleSetting> gradle testGradleSetting
---------test----settings.gradle----------
E:\Gradle\study\GradleSetting

> Configure project :
testGradleSetting

> Configure project :Method
3
3
30Get the result returned by the method:30
1
2
3
4
5


BUILD SUCCESSFUL in 2s
Copy the code

Because in Settings. Gradle file configuration Method, from the output Method is involved in the building, cancel the Settings. The gradle file configuration, does not build Method, the best prove it.

The location of the Method subproject is configured. If this is not specified, the default directory is settings.gradle.

The Build file

If you choose to build a project using Gradle, each project has a build. Gradle file. This file is the entry point to the project’s construction and is effective for the configuration of the entire project. All dependencies in the subproject then point to the JCenter central library for download. Here is the reference code:

// Configure the repository that the subproject depends on
subprojects{
	repositories{
		jcenter()
	}
}

// Configure all projects
allprojects{
	
}

...
Copy the code

In this section, you can learn about the build.gradle file. There will be more detailed configuration policies for different types of projects in actual development.

The Project and the Tasks

There are many projects in Gradle. You can package a Project into a JAR and provide it to another Project. Each Project is a submodule abstracted according to its business requirements, which is finally built into a complete Project through Gradle.

Each Project can have multiple tasks. Tasks are defined as tasks that perform specific function points, such as wrapper tasks that create wrapper files.

Task creation

Now that you’re familiar with task creation, declare a task using task:

1. Create a task
task createTask{
	doFirst{
		println 'doFirst'
	}

	doLast{
		println 'doLast'}}//2. Create a task using TaskContainer, the TaskContainer already defined by Project
tasks.create("createTask1"){
	doFirst{
		println 'doFirst'
	}

	doLast{
		println 'doLast'}}Copy the code

In Groovy, you can omit the parentheses on the method parameters. The content in the curly braces is a closure, which is mainly used to configure the task. DoFirst and doLast are two common methods that are executed at the start and end of a Task, respectively.

Task dependent on

Tasks can be interdependent and the execution sequence of A task can be controlled. For example, B must be run before A, and task A depends on task B. For details, see the following code:

// Single task dependency: Specify a task to depend on by dependsOn
task B(dependsOn: A){
	doFirst{
		println 'B'
	}
}

task C{
	doFirst{
		println 'C'}}// Multi-task dependency
task D{
	dependsOn A, C
	doFirst{
		println 'D'}}Copy the code

Run gradle D with multiple dependencies:

PS E:\Gradle\study\GradleSetting> gradle D

> Task :A
A

> Task :C
C

> Task :D
D

BUILD SUCCESSFUL in 2s
Copy the code

Obviously, when task D is executed, the other two tasks it depends on are executed first, which controls the sequence of task execution.

Note: The script is executed sequentially, and if task A and C are defined after task D, an error is bound to occur when task D is executed.

Interactions between tasks

Each Task has its own name and its type is Task, so we can use the Task API to control the execution of the Task. The principle of using the Task name to operate the Task is as follows: When creating a Task, the Project has declared the corresponding Task as an attribute of the Project object whose type is Task. The test code is as follows:

// Interactions between tasks
task E{
	println 'hello e'
	println "E is not a property of Project:"+project.hasProperty('E')
}

E.doFirst{
	println 'doFirst'
}

E.doLast{
	println 'doLast'
}
Copy the code

The result of the above code is as follows:

PS E:\Gradle\study\GradleSetting>true

> Task :E
doFirst
doLast


BUILD SUCCESSFUL in 1s
Copy the code

Custom attributes

Both Project and Task allow users to add additional custom attributes by applying the corresponding Ext attribute. After adding a custom attribute, you can read and set it by using the Ext attribute. To add multiple custom attributes simultaneously, you can use the Ext block. Refer to the following code to define custom attributes:

apply plugin:"java"

// Customize a single attribute
ext.name1 = "Gradle"
// Customize multiple attributes
ext{
	age = 10
	score = 100
}

// Use custom attributes in SourceSet
sourceSets.all{
	ext.resourceDir = null
}

// Configure custom attributes
sourceSets{
	main{
		resourceDir = "main/res"
	}
	test{
		resourceDir = "test/res"
	}
}

task customProperty{
	println "name=${name1}"
	println "age=${age}"
	println "score=${score}"

	sourceSets.each {
		println "${it.name} resourceDir is ${it.resourceDir}"}}Copy the code

The result of the above code:

PS E:\Gradle\study\GradleSetting> gradle customProperty

> Configure project :

name=Gradle
age=10
score=100
main resourceDir is main/res
test resourceDir is test/res

BUILD SUCCESSFUL in 2s
Copy the code

Compared with local variables, custom attributes have a wider scope and can be accessed across tasks and projects. As long as the object to which these attributes belong can be accessed, these attributes can be accessed. Android development can use custom attributes to define the version number, version name and the version of the third-party library used. Gradle files can be stored in a separate gradle file for each Module. This not only facilitates the management of dependent library versions, but also improves work efficiency to a certain extent.

conclusion

Gradle scripts are based on Grooy and Groovy is fully compatible with Java syntax. Gradle scripts are essentially code that can be used in Gradle to perform related functions. You can follow the official account: Jzman-blog, to communicate and learn together.