简介
- 官网
- Gradle 是一个通用的构建工具,它能构建任何基于你的构建脚本的东西。如果构建Java项目,引入构建java的插件(添加 task 到 project,如编译、打包、生成javadoc等)即可
- 在 Gradle 中两个顶级概念:project 项目和 task 任务 ^1
安装和命令
安装
1 | # mac |
gradle-wrapper
- 类似maven wrapper,idea需要配置使用wrapper的gradle版本(不能设置成使用本地gradle版本)
- 项目目录下,配置
gradle/wrapper/gradle-wrapper.properties
- distributionUrl下载慢可使用腾讯镜像,参考:https://blog.csdn.net/youngwah292/article/details/110734407
1 | distributionBase=GRADLE_USER_HOME |
镜像
- 对所有项目有效:创建
用户目录/.gradle/init.gradle
文件
1 | allprojects{ |
- 对单个项目有效:在项目的
build.gradle
文件中添加以下内容,配置好后使用Sync同步依赖
1 | buildscript { |
命令
- 运行 Gradle 是使用
gradle
命令行,命令行会寻找项目的根目录下build.gradle
的文件,运行完后会在项目目录创建.gradle
文件夹保存构建信息
1 | gradle -v # 查看版本 |
脚本
Hello World
- 创建文件
build.gradle
,然后执行gradle hello
,命令行会打印Hello world!
1 | // build.gradle 基于 groovy 语法 |
Task
- 创建文件
build.gradle
(见下文),然后执行gradle -q run
1 | # 案例一打印结果如下 |
- build.gradle
1 | // https://docs.gradle.org/current/userguide/tutorial_using_tasks.html |
多项目构建
编译Java项目
1 | apply plugin: 'java' // 引入java构建插件 |
参考文章