gradle使用总结


1. 安装gradle

https://gradle.org/install/ (下载)

2. 常用命令

gradle命令 作用
gradle build trigger the build
gradle properties check the project properties
gradle tasks check the tasks
gradle dependencies list all the dependencies for the projects
gradle dependencies –configuration compile list the compile dependencies
gradle dependencies –configuration testCompile list the test dependencies
gradle -q projects list the project tree (-q quite)
gradle -q clean clean the builds dir
gradle wrapper –gradle-version 3.5 download the wrapper
gradle build –profile profile the build

3. 创建gradle工程

方法1. 通过IDE,例如Intellij创建一个java工程,类型选择gradle
create gradle from intellij

方法2. 通过命令行下输入gradle init

gradle init from cmd line

4. 设置maven url

build.gradle文件里,做如下设置:

repositories {
maven {
url "http://mirror-url-of-maven/xxx/yyy"
}
}

这样在国内也能很快的下载依赖。

5. gradle的使用

5.1. 关于gradle wrapper

通过命令gradle wrapper –gradle-version 3.5 会生成wrapper, 它会去官网下载gradle的jar包。
然后保存在gradle/目录下,所以如果网络很慢的话,应该可以从别处拷贝过来吧(待验证, 似乎不行)

5.2. gradle view tool from Intellij

很方便的显示有哪些project以及各自有多少tasks
双击能执行task。跟maven一样,可以设置成离线模式。

5.3. create new task

在build.gradle文件里,添加如下内容。

task showDate {
// make it rely on build, without it, it is a separate task
dependsOn build
description = 'show current date'
// define the user group name, default it goes to 'other' group
group = 'my tasks'
doLast {
println ""
println 'current date: ' + new Date()
println ""
}
}

5.4. extend gradle task

使用gradle DSL语法

class ShowDate extends DefaultTask {  // demo extend task
String dateMessage = "Date is: "

@TaskAction
void showDate() { // method name can be anything
println dateMessage + new Date()
}
}

task showDate(type: ShowDate)

task customShowDate(type: ShowDate) { // demo override custom task
dateMessage = "Custom time is: "
}

5.5. create gradle plugin

创建一个gradle的项目,里面主要是groovy文件,然后可以定义很多的tasks

// define a gradle module and add groovy code
// ShowDate.groovy
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class ShowDate extends DefaultTask {

String dateMessage = 'Current date: '

@TaskAction
def showDate() {
println ""
println dateMessage + new Date()
println ""
}
}

// ShowDatePlugin.groovy
import org.gradle.api.Plugin
import org.gradle.api.Project

class ShowDatePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.task('showDate', type: ShowDate)
}
}
// and a meta file: show-date-plugin.properties
implementation-class=info.garagesalesapp.plugin.ShowDatePlugin

// plugin's build.gradle
group 'info.garagesalesapp'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'

dependencies {
compile gradleApi()
compile localGroovy()
}

在外层的gradle项目中使用plugin(需要指定gradle plugin的jar包位置)

buildscript {
dependencies {
classpath files('show-date/build/libs/show-date-1.0-SNAPSHOT.jar')
}
}

apply plugin: 'show-date-plugin'

5.6. run profile

gradle build --profile
它会在build的目录下面,生成一个report的html,文件名包含build的时间戳,用浏览器打开即可。

总结:
gradle很强大的一点是,它使用groovy语法,非常灵活,能实现的功能也会比maven更强大。


文章作者: IT神助攻
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IT神助攻 !
  目录