build.gradle file Java Tips (02) Gradle project configuration explained

一、build.gradle

1.0 Groovy simple grammar

1.0.1 declare variables

// 动态类型,千万别记成弱类型!
def var = "123"
// 可以省略 def关键字。这样定义的变量作用域更大
var2 = "123str"

1.0.2 Method and closures

Simple closures without parameters

//  闭包
//  无参数简单闭包
def myClosure = {
    println "闭包输出"
}
// 声明方法
def method(Closure closure) {
    closure()
}
// 将闭包传入方法中
method(myClosure)

If you omit the parentheses, you can write some of it becomes elegant configuration as the wording in gradle. + Method name to call closure

method{闭包内容}  等价于 method(){闭包} 等价于  method({闭包内容})

Closure parameters


//  参数闭包
def myClosure2 = {
    str -> println "闭包输出$str"
}

// 声明方法
def method2(Closure closure) {
    closure("abc")
}

method2(myClosure2)

1.1 File

// !!!!!注意,别看这花里胡哨的语法,其实都是Groovy的语法。都是省略括号的形式和闭包形式

plugins {
    id 'java' // 使用插件
}

apply plugin: 'war' // 打成war包
 
group 'com.sjx' // 每个jar包都有一个坐标,这是域名。
version '1.0-SNAPSHOT' // 版本

// 兼容版本:1.8
sourceCompatibility = 1.8

// 指定仓库的路径
repositories {
    // 需要配置GRADLE_USER_HOME 这个环境变量。设置为本地maven仓库即可
    mavenLocal() // 使用本地仓库
    mavenCentral() // 默认使用中央仓库
}

/**
 * 每个jar包有三个信息组成group、name、version
 * 可省略括号,可用语法糖形式——域名:项目名:版本号
 */
dependencies {
    compile ('org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE')
    compile 'org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

1.2 gradle role

Use Gradl build the project, management jar package. Like maven same. In gradle, the configuration is in build.gradlethe configuration file. For example, we need Spring jar package. You can dependenciesprocess incoming closure - call complie method and passing in a closure jar package coordinates:

dependencies {
    compile ('org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE')
    compile 'org.springframework.boot:spring-boot-starter-web:2.1.4.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
Published 17 original articles · won praise 7 · views 358

Guess you like

Origin blog.csdn.net/weixin_44074551/article/details/104779300