gradle学习总结

Posted 覃会程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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'



以上是关于gradle学习总结的主要内容,如果未能解决你的问题,请参考以下文章