gradle学习总结



gradle的特性

  • 是一种通用的灵活的构建工具
  • 对已有的 maven 和 ivy 仓库的全面支持
  • 基于 groovy,其 build 脚本使用 groovy dsl 编写
  • Gradle 的构建脚本是采用 Groovy 写的,而不是用 XML。


groovy语言的简单使用

println "hello word!"

//定义变量
def a = 12
println a

def s = "asdfasf"
println s

//定义一个集合
def list = ['a', 'b']
//向集合里添加元素
list << 'c'
//取出list中的第三个元素
println list.get(2)

//定义一个map
def map = ["key1":"value1", "key2":"value2"]
//向map中添加键值对
map.key3 = "value3"
//根据key取出value的值
println map.get("key3")

//grovvy中的闭包
//什么是闭包?闭包其实就是一段代码块。在gradle中,我们主要是把闭包当参数来使用。
// 定义一个闭包
def b1 = {
    println "hello b1"
}

//定义一个方法,方法里面需要闭包类型的参数
def method1(Closure closure){
    closure()
}

//调用方法method1
method1(b1)

//定义一个带参数的闭包
def b2 = {
    v ->
        println "hello ${v}"
}

//定义一个方法,方法里需要闭包类型的参数
def method2(Closure closure){
    closure("canshu")
}

//调用method2
method2(b2)

执行结果:

hello word!
12
asdfasf
c
value3
hello b1
hello canshu


build.gradle 文件说明

// 项目归属和版本号
group 'com.tencent.pcg'
version '1.0-SNAPSHOT'

// buildscript中的声明是gradle脚本自身需要使用的资源。
// 可以声明的资源包括依赖项、第三方插件、maven仓库地址等
buildscript {
    repositories {
        maven { url 'http://maven.oa.com/nexus/content/repositories/central'}
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
    }
}

// 使用 java 插件
apply plugin: 'java'
// 使用 springboot 框架
apply plugin: 'org.springframework.boot'
// 使用 Scala 插件
apply plugin: 'scala'

// 打的 jar 包的名称和版本信息
jar {
    baseName = 'data-qulity-guard'
    version = '0.0.1'
}

// demo main程序入口
task run(type: JavaExec, dependsOn: classes) {
    main = 'com.tencent.pcg.ReadApiDemo'
    classpath = sourceSets.main.runtimeClasspath
}

sourceCompatibility = 1.8

// 指定本地仓库 / 远程仓库  优先级从上往下
repositories {
    mavenCentral()
    // 使用公司内部的远程仓库
    maven { url 'http://maven.oa.com/nexus/content/repositories/central'}
    maven { url 'http://maven.oa.com/nexus/content/repositories/mqq/'}
    maven { url 'http://maven.oa.com/nexus/content/groups/public/'}
}

// 依赖配置,引入依赖的 kafka,scala
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.5.4.RELEASE")
    compile("org.springframework.boot:spring-boot-starter:1.5.4.RELEASE")
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    compile("mysql:mysql-connector-java:5.1.21")
    compile("org.scala-lang:scala-library:2.11.4")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    compile("org.apache.kafka:kafka_2.11:0.8.2.1")
    compile("qq-central:msg_fetch_api_3.0:1.0.0-SNAPSHOT"){
        exclude group: 'org.apache.kafka', module: 'kafka_2.10'
    }
    testCompile group: 'junit', name: 'junit', version: '4.12'
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥自在”

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值