Gradle learning record 01

1. Introduction to Project Automation

1.1 Mainstream build tools

Ant: Compile, test, and package
Maven: Dependency management, release
Gradle: An open source project automated build tool, based on the concepts of Apache Ant and Apache Maven, and introduces a domain-specific language (DSL) based on Groovy, instead of Then use the XML format to manage the build script. Its essence is a program framework written in Groovy language

1.2 What does Gradle do?

As a build tool, it must have the functions of a build tool, such as dependency management, automated running tests, packaging, and publishing to designated places. Because of its very good flexibility and scalability, it provides a wider space for the program.

Second, the installation of Gradle

Because Gradle is based on JVM, be sure to install JDK on this machine.
step one:
After the download is complete, you can find a file location for decompression at will. Step two:

Third, the directory under the Gradle compressed file

bin: There are two executable files in the bin directory, one is gradle (executable file under Unix-like system), the other is gradle.bat (executable file under windows system)
init.d: ​​All initialization scripts are in Under this directory, the scripts under this directory will be executed every time before the build is executed. If you have this requirement, you can copy the script to the directory
lib: gradle itself depends on the jar package
media: icon file

Explanation of the project's build.gradle

//构建脚本默认都有一个Project实例,构建脚本的所有代码
//作用域都是Project
apply plugin: 'java'  //apply是一个方法,括号省略了。其参数类型为Map集合

version='0.1'  //Project实例上面有一个属性叫version,其值为0.1

//repositories是一个方法,括号省略了。{}及其里面的内容作为一个闭包传递给该方法
repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

//创建一个闭包
def createDir = {
    path ->
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
}

//自定义任务
task makeJavaDir() {
    def paths = ['src/define/file1', 'src/define/file2'];
    //执行阶段执行的代码
    doFirst {
        paths.forEach(createDir)
    }
}

task makeWebDir(){
    //配置阶段就要确定好,任务的依赖,执行阶段修改依赖是错误的做法
    dependsOn 'makeJavaDir'
    def paths=['src/main/webapp','src/test/webapp']
    //执行阶段执行的代码
    doLast{
        paths.forEach(createDir)
    }
}

Four, build script summary

4.1 Building blocks:

4.2 Project

Group, name, and version are the coordinates of a dependency, and they are used to determine the only dependent library
apply: apply is a method on Project, which is used to introduce which plugins are needed for the current project.
dependencies: used to declare the current project dependencies which jar packages or other items in
repositories: used to specify which libraries depend on the current project engineering
task: the current project stated mission of
other configuration attributes: ext, gradle.properties

4.3 Task

depdendsOn: doFirst used to declare task dependencies
: task is a task list, doFirst is to add an action
doLast at the forefront of a task list (you can use "<<" instead): doLast is to add one at the end of a task list action
Note: a task which can perform multiple doFirst, doLast

Five, the life cycle of construction

Initialization phase: initialize major project, determine which items you want to participate in building among the
configuration phase: the main generating map task dependencies and execution of
the execution phase: the implementation of task actions

Six, dependency management

Seven, version conflict

Thank you very much for the learning video provided by Mukenet

Guess you like

Origin blog.csdn.net/Duckdan/article/details/109551045