Project Construction Tool - Gradle Introduction and Basic Groovy Syntax

Gradle is an open source tool for project automation building based on the concepts of Apache Ant and Apache Maven. itUse a Groovy-based domain-specific language (DSL) to declare project settings, and also add a kotlin-based DSL based on the Kotlin language, abandoning various cumbersome configurations based on XML

Gradle itself is actually a java applet, a small program developed by java language and running on jvm

Mainly for Java applications. Currently it supports languages ​​C++, Java, Groovy, Kotlin, Scala and Swift, and plans to support more languages ​​in the future.
The picture below is the little elephant representing Gradle. If you see the configuration file of the little elephant logo in the project in IDEA, it means that the project is managed by Gradle
insert image description here

Gradle installation and environment configuration

jump over.

Gradle syntax: simple syntax for the groovy language

Using gradle to build the project in idea is exactly the same as the project built by maven, mainly for jar package management and project construction

To learn how to use the Gradle tool, the groovy language is the foundation, but you only need to master it without being proficient

Groovy is an agile development language based on JVM (Java Virtual Machine). It combines many powerful features of Python, Ruby and Smalltalk. Groovy code can be well combined with Java code and can also be used to extend existing code. Due to its nature of running on the JVM, Groovy can also use libraries written in other languages ​​than Java.

Next, let's try to use the groovy language:
idea automatically integrates the Groovy environment, click Tools->Groovy Console in the idea, and the groovy console will pop up.
We use code to perform some output
. 1. Define variables

println ("hello groovy")
println ("我是你爸爸真伟大,养你这么大")

output:

> println ("hello groovy")
> println ("我是你爸爸真伟大,养你这么大")
hello groovy
我是你爸爸真伟大,养你这么大

insert image description here
How to define variables using groovy language?

// groovy中如何定义变量
//def是弱类型的,groovy会自动根据情况来给变量赋予对应的类型
def i =18
println i
def s = "IKUN"
println s
//定义一个集合类型,单引号双引号都可以
def list1 = ['a','b']
//往list1里面添加元素
list1 << "c"
//取出list1中的第三个元素
println list1.get(2)
//定义一个map
def map1 =['k1':'v1','k2':'v2']
//往map里面放值,直接设置键值即可
map1.key3='v3'
println map1.get('k2')

Print result:

18
IKUN
c
v2

insert image description here
Two, closure

//什么是groovy的闭包?闭包其实就是一段代码块。在groovy中,我们主要是把闭包当参数来使用
//定义一个闭包
def b1 = {
    
    
    println 'hello b1'
}
//定义一个方法,方法里面需要闭包类型的参数,Closure表示闭包,上面叫我们的b1就是个闭包
def method1(Closure closure){
    
    
    closure()
}
//调用方法method1
method1 (b1)

//定义一个需要传参的闭包
def b2 = {
    
    
    v ->
        println "hello ${v}"
}
//定义一个方法,调用b2这个闭包,方法里面需要闭包类型的参数
def method2(Closure closure){
    
    
    closure("坤坤")
}
//调用方法
method2(b2)

result:

hello b1
hello 坤坤

insert image description here

Guess you like

Origin blog.csdn.net/dayuiicghaid/article/details/126276117