gradle入门

是什么

一款构建工具(类似maven)

为什么要用发(优劣分析、竞品对比)

支持多种方式管理依赖,如maven、lvy、本地文件系统等

据说对java支持很好

支持脚本构建任务,更灵活,相对maven来说,maven只有编译,而gradle可以通过脚本做任何事。

怎么用

脚本功能测试:

// hello world
// task hello {
//     doLast{
//         println 'Hello Earth'
//     }
// }

// 支持 groovy
// task upper  {
//     doLast{
//         String someString = "my_NamE"
//         println "Original: " + someString
//         println "Upper case: " + someString.toUpperCase()
//     }
// }

// task count {
//     doLast{
//         4.times {print "$it "}
//     }
// }


// 任务依赖
// task hello{
//     doLast{
//         println "Hello world!"
//     }
// }
// task intro(dependsOn: hello){
//     doLast{
//         println "I'm Gradle"
//     }
// }

// 动态任务
// 4.times { counter ->
//     task "task$counter"{
//         doLast{
//             println "I'm task number $counter"
//         }
//     }

// }

// // 任务间通信-增加依赖
// task0.dependsOn task2,task3

// 任务间通信-增加任务行为
// task hello {
//     doLast{
//         println "Hello earth"
//     }
// }
// hello.doFirst {
//     println "Hello Venus"
// }
// hello.doLast{
//     println "Hello Mars"
// }
// hello  {
//     doLast{
//         println "Hello Jupiter"
//     }
// }

// 每个任务都是一个脚本的属性,以属性的方式访问任务
// task hello {
//     doLast{
//         println "Hello world!"
//     }
// }
// hello.doLast{
//     println "Greetings fro the $hello.name task."
// }

// 增加属性
// task myTask{
//     ext.myProperty = "myValue"
// }
// task printTaskProperties {
//     println myTask.myProperty
// }

// 调用ant任务
// task loadfile {
//     def files = file("../antLoadfileResources").listFiles().sort()
//     files.each { File file  ->
//         if (file.isFile()){
//             ant.loadfile(srcFile: file, property: file.name)
//             println "*** $file.name ***"
//             println "${ant.properties[file.name]}"
//         }
//     }
// }

// 定义默认任务
// defaultTasks 'clean','run'
// task clean {
//     println "Deafult Cleaning"
// }
// task run {
//     println "Default Running"
// }
// task other {
//     println "I'm not a default task! "
// }

// 依赖任务的不同输出 测试未生效,全部执行,且version unspecified
// task distribution {
//     println "We build the zip with version=$version"
// }
// task release (dependsOn: 'distribution'){
//     println "We release now"
// }
// gradle.taskGraph.whenReady {taskGraph ->
//     if (taskGraph.hasTask(release)){
//         version = "1.0"
//     }else {
//         version = "1.0-SNAPSHOT"
//     }
// }

gradle java

常用命令

gradle build 项目构建
gradle clean 删除build目录以及所有构建完成的文件
gradle assemble 编译并打jar包,不执行单元测试
gardle check 编译并测试代码

maven仓库

//maven仓库
repositories {
    mavenCentral()
}

// 添加依赖
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

// 为test添加系统属性
test {
    systemProperties 'property': 'value'
}

// 发布jar包到指定目录活指定远程仓库  执行 gradle uploadArchives 以发布 jar 包
uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

多项目构建

//setting.gradle
// 包含子项目:api/  services/webservice/  shared/
include "shared", "api", "services:webservice", "services:shared"


//build.gradle
// 公共配置
subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse-wtp'
    repositories {
       mavenCentral()
    }
    dependencies {
        testCompile 'junit:junit:4.11'
    }
    version = '1.0'
    jar {
        manifest.attributes provider: 'gradle'
    }
}

//api/build.gradle
// 工程依赖
dependencies {
    compile project(':shared')
}
//打包发布  这步没太明白
task dist(type: Zip) {
    dependsOn spiJar
    from 'src/dist'
    into('libs') {
        from spiJar.archivePath
        from configurations.runtime
    }
}
artifacts {
   archives dist
}

参考文档:

https://www.w3cschool.cn/gradle/3miy1htt.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值