gradle 常用功能
常用命令
- gradle build -b idgenerator/build.gradle : 指定build文件
- gradle -Pparam=test : 传参数
gradle 属性与命令传值
- gradle属性
- gradle.properties : 定义属性,在build.gradle中可以通过$properties-name获取
- ext { propName=value } 中定义的属性能通过propName直接用
- gradle properties可以查看属性
- gradle build -PpropName=value 可以给properties传值
- gradle系统属性
- gradle.properties 处理定义属性,也能定义系统属性,系统属性就是gradle系统的运行的JVM的环境,不是gradle task执行的任务的属性
- systemProp.propName=value, 系统属性在build.gradle中可以通过System.properties['propName']获取
- 以ORG_GRADLE_PROJECT.为前缀的环境变量或者以org.gradle.project.为前缀的系统属性都可以当作gradle属性使用,可以用来保存密码
- gradle build -DpropName=value 可以给系统属性传值
- 为gradle task设置系统属性
- test { systemProperty "spring.profiles.active", "local" }
- 为spring指定profiles - 通过传递gradle系统属性给gradle task
- test { systemProperty System.properties }
- gradle build -Dspring.profiles.active=prod
- 为spring指定profiles - 通过properties传值
- test { systemProperty 'spring.profiles.active', $profiles }
- 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"
- 命令
- gradle ktlintformat
- gradle ktlintApplyToIdea : 需要父项目有repository
publish上传Maven的Repostory
- apply plugin: 'maven-publish'
父项目添加:
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的项目添加:
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
- 可以使用subprojects或者allprojects
- 将公共都apply/dependencies都放在subprojects
Module项目
- 只保留src和build.gradle
- settings.gradle要删除,否则单独build项目不会去找parent的build.gradle
- build.gradle : Parent的build.gradle中有的它不能再有
- 单独build
- gradle clean build -b module1/build.gradle
- 在module1下build
- module1依赖module2
- compile project(":module2")
- 单独build module1会去编译module2,单不会测试和打包module2
进度
II. Working with existing builds
- Installing Gradle
- Using the Gradle Command-Line
- The Gradle Wrapper
- The Gradle Daemon
- Dependency Management Basics
- Introduction to multi-project builds
- Continuous build
- Using the Gradle Graphical User Interface
- The Build Environment
- Troubleshooting
- Embedding Gradle
III. Writing Gradle build scrip
- Build Script Basics
- Build Init Plugin
- Writing Build Scripts
- More about Tasks
- Working With Files
- Using Ant from Gradle
- The Build Lifecycle
- Wrapper Plugin
- Logging
- Dependency Management
- Multi-project Builds
- Gradle Plugins
- Standard Gradle plugins
- The Project Report Plugin
- The Build Dashboard Plugin
- Comparing Builds
- Publishing artifacts
- The Maven Plugin
- The Signing Plugin
- Ivy Publishing (new)
- Maven Publishing (new)
- The Distribution Plugin
- The Announce Plugin
- The Build Announcements Plugin
IV. Extending the build
- Writing Custom Task Classes
- Writing Custom Plugins
- The Java Gradle Plugin Development Plugin
- Organizing Build Logic
- Initialization Scripts
- The Gradle TestKit
V. Building JVM projects
- Java Quickstart
- The Java Plugin
- Web Application Quickstart
- The War Plugin
- The Ear Plugin
- The Jetty Plugin
- The Application Plugin
- The Java Library Distribution Plugin
- Groovy Quickstart
- The Groovy Plugin
- The Scala Plugin
- The ANTLR Plugin
- The Checkstyle Plugin
- The CodeNarc Plugin
- The FindBugs Plugin
- The JDepend Plugin
- The PMD Plugin
- The JaCoCo Plugin
- The Sonar Plugin
- The SonarQube Runner Plugin
- The OSGi Plugin
- The Eclipse Plugins
- The IDEA Plugin
VI. The Software model - Next generation Gradle builds
- Rule based model configuration
- Software model concepts
- Implementing model rules in a plugin
- Building Java Libraries
- Building Play applications
- Building native software
- Extending the software model
VII. Appendix
A. Gradle Samples
B. Potential Traps
C. The Feature Lifecycle
D. Gradle Command Line
Glossary
安装 Gradle
- Gradle 官网
- JDK1.6以上
- 设置Env,启用cmd gradle命令
- GRADLE_HOME : 不带;
- PATH: %GRADLE_HOME%/bin
- 测试 cmd : gradle -v
Gradle HelloWorld
build.gradle
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' }
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
- Gradle API : To be find out
Project
Gradle Wrapper
生成wrapper文件
- gradle wrapper
生成当前gradle版本wrapper文件
gradlew gradlew.bat gradle/wrapper/ gradle-wrapper.jar gradle-wrapper.properties
自定义wrapper所用gradle版本 task
task wrapper(type: Wrapper) { gradleVersion = '2.11' }
使用本地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运行速度
- 默认关闭Daemon,gradle建议Dev机子都开启Daemon
- 开启Daemon
- C:\Users\xxxx\.gradle目录新建文件 gradle.properties
- gradle.properties文件添加 org.gradle.daemon=true
- 当org.gradle.daemon=true并且Daemon未开启时,运行gradle命令会自动开启Daemon
- 关闭Daemon
- gradle --stop
- 闲置3小时的Daemon进程会自动关闭
支持task名字缩
Java plugin
启用Java plugin
- apply plugin: 'java'
- 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目录
- sourceSets : SourceSetContainer对象,SourceSet对象的集合
- main : SourceSet对象
- java : SourceDirectorySet对象
- srcDir() : SourceDirectorySet的方法
直观写法
sourceSets { main { output.resourcesDir = "$buildDir/target" java { srcDirs = ['src'] } resources { srcDirs = ['ds'] } } }
依赖包设置
dependencies { compile fileTree('lib') }
Java plugin 常用Tasks
- compileJava : 编译class
- 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'
}