Gradle学习

gradle 经常使用功能

经常使用命令

  • gradle build -b idgenerator/build.gradle : 指定build文件
  • gradle -Pparam=test : 传参数

gradle 属性与命令传值

  • gradle属性
    1. gradle.properties : 定义属性,在build.gradle中能够经过$properties-name获取
    2. ext { propName=value } 中定义的属性能经过propName直接用
    3. gradle properties能够查看属性
    4. gradle build -PpropName=value 能够给properties传值
  • gradle系统属性
    1. gradle.properties 处理定义属性,也能定义系统属性,系统属性就是gradle系统的运行的JVM的环境,不是gradle task执行的任务的属性
    2. systemProp.propName=value, 系统属性在build.gradle中能够经过System.properties['propName']获取
    3. 以ORG_GRADLE_PROJECT.为前缀的环境变量或者以org.gradle.project.为前缀的系统属性均可以看成gradle属性使用,能够用来保存密码
    4. gradle build -DpropName=value 能够给系统属性传值
  • 为gradle task设置系统属性
    1. test { systemProperty "spring.profiles.active", "local" }
  • 为spring指定profiles - 经过传递gradle系统属性给gradle task
    1. test { systemProperty System.properties }
    2. gradle build -Dspring.profiles.active=prod
  • 为spring指定profiles - 经过properties传值
    1. test { systemProperty 'spring.profiles.active', $profiles }
    2. gradle build -Pprofiles=prod

gradle缓存和mavenLocal()

  • gradle可使用maven本地仓库,可是不会将本地仓库做为其余仓库的缓存
  • gradle使用本身的cache代替maven本地仓库
  • 因此相似永远更新0.0.1-SNAPSHOT依赖的状况下,不能使用mavenLocal(),正确作法升级到0.0.2-SNAPSHOT

spring boot 打包

  • apply plugin: 'org.springframework.boot'
  • bootJar { archiveClassifier = "boot" }

Ktlint代码format

  • Dependency : classpath "org.jlleitschuh.gradle:ktlint-gradle:8.1.0"
  • apply plugin: "org.jlleitschuh.gradle.ktlint"
  • 命令
    1. gradle ktlintformat
    2. gradle ktlintApplyToIdea : 须要父项目有repository

publish上传Maven的Repostory

  • apply plugin: 'maven-publish'
  • 父项目添加:html

    publishing {
          repositories {
              mavenLocal()
              maven {
                  def repoKey = 'lib-release/libs-release-local'
                  if (project.version.toString().endsWith("-SNAPSHOT"))
                      repoKey = 'lib-snapshot/libs-snapshot-local'
                  url "https://${xxx}/repository/${repoKey}"
                  credentials {
                      username "${xxx}"
                      password "${xxx}"
                  }
              }
          }
      }
  • 须要publish的项目添加:java

    publishing {
          publications {
              mavenJava(MavenPublication) {
                  groupId "$project.group"
                  version "$project.version"
                  artifactId project.name
                  from components.java
              }
          }
      }

Gradle 父子项目

Parent项目

  • 能够保留全部文件,也能够删除src文件
  • settings.gradle : include "module1", "module2"
  • build.gradle
    1. 可使用subprojects或者allprojects
    2. 将公共都apply/dependencies都放在subprojects

Module项目

  • 只保留src和build.gradle
  • settings.gradle要删除,不然单独build项目不会去找parent的build.gradle
  • build.gradle : Parent的build.gradle中有的它不能再有
  • 单独build
    1. gradle clean build -b module1/build.gradle
    2. 在module1下build
  • module1依赖module2
    1. compile project(":module2")
    2. 单独build module1会去编译module2,单不会测试和打包module2

进度

II. Working with existing buildsspring

  1. Installing Gradle
  2. Using the Gradle Command-Line
  3. The Gradle Wrapper
  4. The Gradle Daemon
  5. Dependency Management Basics
  6. Introduction to multi-project builds
  7. Continuous build
  8. Using the Gradle Graphical User Interface
  9. The Build Environment
  10. Troubleshooting
  11. Embedding Gradle

III. Writing Gradle build scripapi

  1. Build Script Basics
  2. Build Init Plugin
  3. Writing Build Scripts
  4. More about Tasks
  5. Working With Files
  6. Using Ant from Gradle
  7. The Build Lifecycle
  8. Wrapper Plugin
  9. Logging
  10. Dependency Management
  11. Multi-project Builds
  12. Gradle Plugins
  13. Standard Gradle plugins
  14. The Project Report Plugin
  15. The Build Dashboard Plugin
  16. Comparing Builds
  17. Publishing artifacts
  18. The Maven Plugin
  19. The Signing Plugin
  20. Ivy Publishing (new)
  21. Maven Publishing (new)
  22. The Distribution Plugin
  23. The Announce Plugin
  24. The Build Announcements Plugin

IV. Extending the build缓存

  1. Writing Custom Task Classes
  2. Writing Custom Plugins
  3. The Java Gradle Plugin Development Plugin
  4. Organizing Build Logic
  5. Initialization Scripts
  6. The Gradle TestKit

V. Building JVM projectsapp

  1. Java Quickstart
  2. The Java Plugin
  3. Web Application Quickstart
  4. The War Plugin
  5. The Ear Plugin
  6. The Jetty Plugin
  7. The Application Plugin
  8. The Java Library Distribution Plugin
  9. Groovy Quickstart
  10. The Groovy Plugin
  11. The Scala Plugin
  12. The ANTLR Plugin
  13. The Checkstyle Plugin
  14. The CodeNarc Plugin
  15. The FindBugs Plugin
  16. The JDepend Plugin
  17. The PMD Plugin
  18. The JaCoCo Plugin
  19. The Sonar Plugin
  20. The SonarQube Runner Plugin
  21. The OSGi Plugin
  22. The Eclipse Plugins
  23. The IDEA Plugin

VI. The Software model - Next generation Gradle buildsmaven

  1. Rule based model configuration
  2. Software model concepts
  3. Implementing model rules in a plugin
  4. Building Java Libraries
  5. Building Play applications
  6. Building native software
  7. Extending the software model

VII. Appendixide

A. Gradle Samples
B. Potential Traps
C. The Feature Lifecycle
D. Gradle Command Line
Glossaryspring-boot

安装 Gradle

  • Gradle 官网
  • JDK1.6以上
  • 设置Env,启用cmd gradle命令
    1. GRADLE_HOME : 不带;
    2. PATH: %GRADLE_HOME%/bin
    3. 测试 cmd : gradle -v

Gradle HelloWorld

  1. build.gradlepost

    task compile << {
         println 'compiling source'
     }
    
     task compileTest(dependsOn: compile) << {
         println 'compiling unit tests'
     }
     task test(dependsOn: [compile, compileTest]) << {
         println 'running unit tests'
     }
    
     task dist(dependsOn: [compile, test]) << {
         println 'building the distribution'
     }
  2. cmd cd build.gradle所在目录

    gradle compile compileTest

Gradle Command-line

  • Gradle Command-line Links
  • gradle dist -x test
    • 排除test任务
  • gradle test dist --continue
    • 当test失败,dist还会继续,但若是dist依赖test则再也不继续
  • gradle -b build2.gradle test
    • 选择build文件
  • gradle -q dist
    • 输出省略任务名称
  • gradle dist --profile
    • 输出任务报表 输出目录:build/reports
  • gradle dependencies
    • 输出项目依赖
  • gradle -m dist
    • 测试任务是否能够正常运行

其余

  • gradle tasks --all
    • 显示任务信息
  • gradle help --task taskName
    • 显示某个任务Detail信息
  • gradle project
    • 显示项目信息
  • gradle properties
    • 显示项目properties

Gradle 脚本编写之: 基础篇

<< 等于 doLast

dependsOn/Lazy dependsOn

  • task dist(dependsOn: test) << {}
  • task dist(dependsOn: 'test') << {}

Dynamic tasks

  • gradle -q task0

    4.times { i ->
          task "task$i" << {
              println "I'm task number $i"
          }
      }
      task0.dependsOn task2, task3
  • gradle -q hello

    task hello << {
          println 'Hello World'
      }
      hello.doFirst {
          println 'Hello doFirst'
      }
      hello.doLast {
          println 'Hello doLast'
      }
      hello << {
          println 'Hello Jude'
      }

task定义 属性/方法

  • Code

    task test(dependsOn:'dist'){
          ext.myName = "Jude"
          version = "1.0"
      }
      task dist << {
          setTestName('Jude Sheng')
          println test.myName + "$version"
      }        
      String setTestName(String myName) {
          test.ext.myName = myName
      }

默认Tasks

  • defaultTasks 'dist','test'
  • gradle -q

Gradle 脚本编写之: Gradle方法

  • taskGraph.hasTask / taskGraph.whenReady
    • 当前执行的task中是否有某个task

      gradle.taskGraph.whenReady {taskGraph ->
            if (taskGraph.hasTask(release)) {
                version = '1.0'
            } else {
                version = '1.0-SNAPSHOT'
            }
        }

Gradle 脚本编写之: API

Project

Gradle Wrapper

生成wrapper文件

  1. gradle wrapper
    • 生成当前gradle版本wrapper文件

      gradlew
          gradlew.bat
          gradle/wrapper/
            gradle-wrapper.jar
            gradle-wrapper.properties
  2. 自定义wrapper所用gradle版本 task

    task wrapper(type: Wrapper) {
         gradleVersion = '2.11'
     }
  3. 使用本地gradle zip文件代替gradle远程下载,zip包须要放在 gradle\wrapper目录下

    task wrapper(type: Wrapper) {
         distributionPath = 'gradle-2.11-all.zip'
     }

使用gradlew命令代替gradle命令

  • gradlew dist
    • 第一次会下载而且解压gradle zip文件至目录 C:\Users\xxxx.gradle\wrapper,以后不会下载
    • C:\Users\xxxx.gradle文件夹是在你安装gradle后跑第一个gradle命令自动生成
  • 使用wrapper的gradle版本代替,而不会用本地gradle版本
  • gradle推荐使用wrapper模式,并将生成的wrapper文件加入版本控制

其余

Daemon使用缓存解决gradle运行速度

  1. 默认关闭Daemon,gradle建议Dev机子都开启Daemon
  2. 开启Daemon
    • C:\Users\xxxx\.gradle目录新建文件 gradle.properties
    • gradle.properties文件添加 org.gradle.daemon=true
      • org.gradle.daemon=true而且Daemon未开启时,运行gradle命令会自动开启Daemon
  3. 关闭Daemon
    • gradle --stop
    • 闲置3小时的Daemon进程会自动关闭

支持task名字缩

Java plugin

启用Java plugin

  1. apply plugin: 'java'
  2. https://docs.gradle.org/3.5/userguide/java_plugin.html

Java plugin 经常使用properties

  • sourceCompatibility = 1.6 : 编译java version
  • archivesBaseName='ratesds' : jar包等的名字
  • sourceSets.main.java.srcDir("src") :设置java source目录
    1. sourceSets : SourceSetContainer对象,SourceSet对象的集合
    2. main : SourceSet对象
    3. java : SourceDirectorySet对象
    4. srcDir() : SourceDirectorySet的方法
    5. 直观写法

      sourceSets {
           main {
               output.resourcesDir = "$buildDir/target"
               java {
                   srcDirs = ['src']
               }
               resources {
                   srcDirs = ['ds']
               }
           }
       }
  • 依赖包设置

    dependencies {
          compile fileTree('lib') 
      }

Java plugin 经常使用Tasks

  • compileJava : 编译class
    1. sourceSets.main.java.srcDir("src")
  • processResources : 拷贝resources
  • jar : 打成jar包
  • javadoc : 生成document html
  • build : 全套服务
  • clean : 删除build文件夹

Copy Task

task copyLib << {
    File scooby = file("$buildDir/target/scooby")
    int scoobySize = scooby.listFiles().length
    scoobySize.times { i ->
        String path = scooby.listFiles()[i].getPath() + "/lib"
        println 'start to copy libs into ' + path
        copy {
            from "lib"
            into path
        }
        copy {
            from "$buildDir/libs"
            into path
        }
    }
}

Zip Task

task zip(type: Zip) {
    from "$buildDir/target/RatesDSCommonConfigs"
    archiveName "RatesDSCommonConfigs.zip"
    doFirst {
        println "Start to create the zip file for RatesDSCommonConfigs"
    }
}
task zipDS {
    File scooby = file("$buildDir/target/scooby")
    int scoobySize = scooby.listFiles().length
    scoobySize.times { i ->
        String scoobyDS = scooby.listFiles()[i].getPath()
        String scoobyDSName = scooby.listFiles()[i].getName()
        String tarFileName = scoobyDSName + ".zip"
        task "zip_$scoobyDSName" (type: Zip) {
            from scoobyDS
            archiveName tarFileName
            doFirst {
                println "Start to create the zip file for $scoobyDSName"
            }
        }
        zip.dependsOn "zip_$scoobyDSName"
    }
}

RatesDS2.0

apply plugin: 'java'

archivesBaseName = 'cv-ratesds'

sourceCompatibility = 1.6

sourceSets {
    main {
        output.resourcesDir = "$buildDir/target/RatesDSCommonConfigs"
        java {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['resources']
        }
    }
}


dependencies {
    compile fileTree('lib') 
}

task copyDS(dependsOn: [build]) <<{
    println 'start to copy DS processes'
    copy {
        from 'ds'
        into "$buildDir/target"
    }
}

task copyLib(dependsOn: [copyDS]) << {
    File scooby = file("$buildDir/target/scooby")
    int scoobySize = scooby.listFiles().length
    scoobySize.times { i ->
        String path = scooby.listFiles()[i].getPath() + "/lib"
        println 'start to copy libs into ' + path
        copy {
            from "lib"
            into path
            exclude "scooby"
        }
        copy {
            from "$buildDir/libs"
            into path
        }
    }
}

task tar(type: Tar) {
    File install = file("$buildDir/install")
    from "$buildDir/target/RatesDSCommonConfigs"
    destinationDir install
    archiveName "RatesDSCommonConfigs.tar"
    doFirst {
        println "Start to create the tar file for RatesDSCommonConfigs"
    }
    tar.dependsOn copyLib
}

task tarScoobyConfig(type: Tar) {
    File install = file("$buildDir/install")
    from "$buildDir/target/RatesDSCommonConfigs/ScoobyConfig"
    destinationDir install
    archiveName "ScoobyConfig.tar"
    doFirst {
        println "Start to create the tar file for ScoobyConfig"
    }
    tar.dependsOn tarScoobyConfig
}

task tarDS {
    File install = file("$buildDir/install")
    File scooby = file("ds/scooby")
    int scoobySize = scooby.listFiles().length
    scoobySize.times { i ->
        String scoobyDSName = scooby.listFiles()[i].getName()
        String tarFileName = scoobyDSName + ".tar"
        String scoobyPath = "$buildDir/target/scooby/" + scoobyDSName
        task "tar_$scoobyDSName" (type: Tar) {
            from scoobyPath
            destinationDir install
            archiveName tarFileName
            doFirst {
                println "Start to create the tar file for $scoobyDSName"
            }
        }
        tar.dependsOn "tar_$scoobyDSName"
    }
}

task install(dependsOn:[tar]) {
    println 'start to install RatesDS2.0'
}
相关文章
相关标签/搜索