CICD - Gradle
Gradle简介
Gradle是一个通用灵活的基于JVM构建工具,支持maven, Ivy仓库,支持传递性依赖管理
使用基于Groovy的特定领域语言(DSL)来声明项目设置,build脚本使用Groovy编写,
抛弃了基于XML的各种繁琐配置,不需要远程仓库或者是pom.xml和ivy.xml配置文件,
面向Java应用为主,支持Java、Groovy、Kotlin和Scala等语言
Gradle 具有以下 3 大特性:
- 高度可定制: 能够以最基本的方式可定制和可扩展的方式建模
- 快速 : 通过重新使用以前执行的输出,仅处理已更改的输入以及并行执行任务来快速完成任务。也就是说,第二次构建,只构建修改代码相关的类,不会耗费时间来构建整个项目
- 强大 : 集各家之所长,既吸收了 Maven 的规范化和仓库概念,又吸收了 Ant 的 task 思想,支持许多流行的语言和技术
Gradle与Maven的关系
通常maven包含两个含义,一个是本地的编译工具,一个是服务端的代码仓库。
Gradle是另一个编译工具,但代码仓库还是使用的Maven。
Gradle的配置文件可读性要好于Maven的XML形式。
Gradle 教程
- https://itdashu.com/docs/gradlebase.html
- https://m.imooc.com/wiki/details?a=gradlebase
- https://www.w3cschool.cn/gradle/
脚本基础
- Gradle脚本基础全攻略:https://www.jianshu.com/p/3d0524980e30
- Groovy脚本基础全攻略:https://www.jianshu.com/p/fbfaaecc19ee
创建Gradle工程
安装Gradle
$ gradle -v # 查看版本
------------------------------------------------------------
Gradle 4.7
------------------------------------------------------------
Build time: 2018-04-18 09:09:12 UTC
Revision: b9a962bf70638332300e7f810689cb2febbd4a6c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_281 (Oracle Corporation 25.281-b09)
OS: Windows 10 10.0 amd64
$
$ gradle --help # 帮助信息
USAGE: gradle [option...] [task...]
-?, -h, --help Shows this help message.
-a, --no-rebuild Do not rebuild project dependencies. [deprecated]
-b, --build-file Specify the build file.
--build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
-c, --settings-file Specify the settings file.
--configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
--console Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'.
--continue Continue task execution after a task failure.
-D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug Log in debug mode (includes normal stacktrace).
--daemon Uses the Gradle Daemon to run the build. Starts the Daemon if not running.
--foreground Starts the Gradle Daemon in the foreground. [incubating]
-g, --gradle-user-home Specifies the gradle user home directory.
-I, --init-script Specify an initialization script.
-i, --info Set log level to info.
--include-build Include the specified build in the composite. [incubating]
-m, --dry-run Run the builds with all task actions disabled.
--max-workers Configure the number of concurrent workers Gradle is allowed to use. [incubating]
--no-build-cache Disables the Gradle build cache.
--no-configure-on-demand Disables the use of configuration on demand. [incubating]
--no-daemon Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default.
--no-parallel Disables parallel execution to build projects. [incubating]
--no-scan Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans. [incubating]
--offline Execute the build without accessing network resources.
-P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue).
-p, --project-dir Specifies the start directory for Gradle. Defaults to current directory.
--parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating]
--profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
--project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
-q, --quiet Log errors only.
--recompile-scripts Force build script recompiling. [deprecated]
--refresh-dependencies Refresh the state of dependencies.
--rerun-tasks Ignore previously cached task results.
-S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions.
-s, --stacktrace Print out the stacktrace for all exceptions.
--scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) [incubating]
--status Shows status of running and recently stopped Gradle Daemon(s).
--stop Stops the Gradle Daemon if it is running.
-t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating]
-u, --no-search-upward Don't search in parent folders for a settings file.
-v, --version Print version info.
-w, --warn Set log level to warn.
--warning-mode Specifies which mode of warnings to generate. Values are 'all', 'summary'(default) or 'none'
-x, --exclude-task Specify a task to be excluded from execution.
创建Gradle工程
$ mkdir gradleTest # 新建一个工程目录
$ cd gradleTest/ # 进入工程目录
$ gradle init # 使用Gradle对工程目录进行初始化,将建立一个通过Gradle管理的模板工程
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
BUILD SUCCESSFUL in 15s
2 actionable tasks: 2 executed
$ ls -la # 查看生成的文件和目录
total 22
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:11 ./
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:10 ../
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:11 .gradle/ # 编译时的缓存文件夹
-rw-r--r-- 1 GuowangLi 1049089 207 Mar 11 23:11 build.gradle # 用于管理和配置工程的核心文件(根项目构建文件),配置 Gradle 版本和 Maven 依赖, 添加所有子项目/模块共有的配置选项
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:11 gradle/ # Gradle Wrapper 的 jar 和属性文件所在的文件夹
-rwxr-xr-x 1 GuowangLi 1049089 5296 Mar 11 23:11 gradlew* # Gradle 启动脚本文件,用于*nix环境下的 Gradle Wrapper 文件
-rw-r--r-- 1 GuowangLi 1049089 2260 Mar 11 23:11 gradlew.bat # Gradle 启动脚本文件,用于Windows环境下的 Gradle Wrapper 文件
-rw-r--r-- 1 GuowangLi 1049089 367 Mar 11 23:11 settings.gradle # 配置项目的模块(根项目设置文件),用于多项目 Gradle 项目管理的配置文件,单项目时不必关注
$
$ ls -l gradle
total 0
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:11 wrapper/
$ ls -l gradle/wrapper/
total 57
-rw-r--r-- 1 GuowangLi 1049089 54413 Mar 11 23:11 gradle-wrapper.jar # jar 包,包含 Gradle 运行时的逻辑代码
-rw-r--r-- 1 GuowangLi 1049089 200 Mar 11 23:11 gradle-wrapper.properties # 配置 Gradle wrapper 运行时的属性文件,声明使用的版本
$
Gradle Wrapper
通过 Gradle Wrapper 开发者能够快速的启动并且运行 Gradle 项目,不用再手动安装和配置,避免浪费时间。
实际上 Gradle Wrapper 是一个脚本,对应一个开发中使用或声明的 Gradle 版本,就不用再去手动安装 Gradle 环境了,以确保任何人任何时候得到的结果都是一致的,从而间接的提高了我们的开发效率。
属性文件: gradle/wrapper/gradle-wrapper.properties
$ cat gradle/wrapper/gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME # Gradle解包后存储的父目录
distributionPath=wrapper/dists # 指定目录的子目录,Gradle解包后的存放的具体目录:distributionBase + distributionPath
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip # 指定版本的压缩包下载地址
zipStoreBase=GRADLE_USER_HOME # Gradle压缩包下载后存储父目录
zipStorePath=wrapper/dists # Gradle 压缩包的存放位置: zipStoreBase + zipStorePath
$
执行 gradle wrapper
生成相关文件
$ mkdir gradleTest2
$ cd gradleTest2
$ gradle wrapper --gradle-version 5.6.2 --distribution-type all # 指定版本和类型, 或者直接通过参数 --gradle-distribution-url 指定版本和类型,
BUILD SUCCESSFUL in 3s
1 actionable task: 1 executed
$ ls -la
total 16
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:59 ./
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:57 ../
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:59 .gradle/
drwxr-xr-x 1 GuowangLi 1049089 0 Mar 11 23:59 gradle/
-rwxr-xr-x 1 GuowangLi 1049089 5296 Mar 11 23:59 gradlew*
-rw-r--r-- 1 GuowangLi 1049089 2260 Mar 11 23:59 gradlew.bat
$ cat gradle/wrapper/gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
$
Gradle命令
一些常用命令
gradle <task> ... # To run a build
gradle tasks # To see a list of available tasks
gradle --help # To see a list of command-line options
gradle help --task <task> # To see more detail about a task
-------------------------------------------------------------------------------
gradle tasks --all # 在project目录下查看当前项目可执行的任务
gradle assemble --dry-run # 在project目录下查看当前项目assemble时将要执行的任务
gradle build --dry-run # 在project目录下查看当前项目build时将要执行的任务(build其实是assemble的超集,额外包含了test等任务
gradle build -x test # 在project目录下执行build时,排除test任务
gradle assemble # Assembles the outputs of this project.
gradle build # Assembles and tests this project.
gradle clean # Deletes the build directory.
-------------------------------------------------------------------------------
gradle init # Initializes a new Gradle build.
wrapper # Generates Gradle wrapper files.
gradle buildEnvironment # Displays all buildscript dependencies declared in root project
gradle components # Displays the components produced by root project
gradle dependencies # Displays all dependencies declared in root project
gradle dependencyInsight # Displays the insight into a specific dependency in root project
gradle dependentComponents # Displays the dependent components of components in root project
gradle help # Displays a help message.
gradle model # Displays the configuration model of root project
gradle projects # Displays the sub #projects of root project
gradle properties # Displays the properties of root project
gradle tasks # Displays the tasks runnable from root project
命令:gradle help
$ gradle help
> Task :help
Welcome to Gradle 4.7.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
For troubleshooting, visit https://help.gradle.org
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
命令:gradle task
$ gradle task
> Task :tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradleTest'.
components - Displays the components produced by root project 'gradleTest'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradleTest'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradleTest'.
dependentComponents - Displays the dependent components of components in root project 'gradleTest'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradleTest'. [incubating]
projects - Displays the sub-projects of root project 'gradleTest'.
properties - Displays the properties of root project 'gradleTest'.
tasks - Displays the tasks runnable from root project 'gradleTest'.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
build.gradle文件
// 项目归属和版本号
group 'com.xxx.xxx'
version '1.0-SNAPSHOT'
// buildscript中的声明是gradle脚本自身需要使用的资源,包括依赖项、第三方插件、maven仓库地址等,也就是配置Gradle脚本生成时需要的资源
buildscript {
repositories {
maven { url 'http://maven.oa.com/nexus/content/repositories/central'}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
}
}
allprojects {
/*
* 声明对所有项目(包括根项目)都适用的配置
*/
}
subprojects {
/*
* 声明用于各子项目(不包括根项目)适用的配置
*/
}
project(':子项目名') {
/*
* 对指定子项目的特殊配置,这个子项目依赖性的管理统一在根build.gradle中完成,但子项目更改依赖时会造成根build.gradle的更新
* 也可以将project(':子项目名')放到各个子项目的build.gradle中,分别管理
*/
}
// 声明使用 java 插件/springboot 框架
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management' //此插件会自动导入当前要使用的spring boot版本的依赖清单
// 声明编译后的 jar 包名称和版本信息
jar {
baseName = 'data-qulity-guard'
version = '0.0.1'
}
// 声明源文件和目标编译后的Java版本兼容性
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 指定仓库
repositories {
mavenCentral()
maven { url 'http://xxx.com/nexus/content/repositories/central'}
}
// 声明依赖
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。