前面几篇介绍了CI基础环境的部署,但是缺少灵魂,这里的灵魂,我们选用Gradle
- 基础框架
- 基础概念
- 常用命令
- 新建项目
- 编写构建脚本
- 任务分解
- 构建任务
- 单元测试
- 代码审查
- 构件发布
- 持续集成
- 持续部署
- 等等
基础框架
基础概念
Gradle 插件
参考资料:https://docs.gradle.org/2.13/userguide/plugins.html
- 扩展Gradle模型
- 插件类型(Script & Binary)
- 插件引入
- 通过buildscript代码块引入插件
依赖管理
参考资料:
https://docs.gradle.org/2.13/userguide/artifact_dependencies_tutorial.html
- 声明依赖
- 依赖配置
Gradle常用命令
- --help or -h:打印其它命令的帮助信息
- -Dproperty=value:定义一个系统变量
- --info or -i:日志级别改为INFO
- --debug or -d:调试模式
- --dry-run or -m:评估并执行构建文件,不执行Task Action
- --quiet or -q:省略大部分输出,只显示错误
- --gui:启动Gradle的GUI
- --stacktrace or -s:打印精简版的构建错误堆栈信息
- --full-stacktrace or -s:打印完整版的构建错误堆栈信息
- properties:打印构建项目的所有属性
- tasks:列出当前构建的所有任务清单,由于引入的插件本身往往自带任务,因此列出的会比自己定义的Task多出很多
新建项目
开始了哟
Paste_Image.png
Paste_Image.png
Paste_Image.png
Gradle的文件结构和Maven基本一致:
-
src/main/java:存放java代码;
-
src/main/resources:存放资源和配置文件;
-
src/test/java:存放测试用的java代码;
-
src/test/resources:存放测试用的资源和配置文件;
-
src/main/webapp:存放WEB的代码和资源(如果是web项目的话);
编译任务
前面提到,只需引入 Java 插件即可实现Java项目的clean、build、test、Jar等工作,下面我们看一下具体的任务有哪些:
>gradlew tasks
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-demo'.
components - Displays the components produced by root project 'gradle-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-demo'.
help - Displays a help message.
model - Displays the configuration model of root project 'gradle-demo'. [incubating]
projects - Displays the sub-projects of root project 'gradle-demo'.
properties - Displays the properties of root project 'gradle-demo'.
tasks - Displays the tasks runnable from root project 'gradle-demo'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Scope,引入的Java插件包含以下几个Scope:
- compile
- default
- testCompile
- testRuntime
- archives
- runtime
不同于Maven中固定的Scope,Gradle中的Scope是动态的,引入不同的插件可引入新的Scope,如引入Groovy插件,即进入了Groovy Scope
查看依赖情况
$ gradle dependencies
archives - Configuration for the default artifacts.
No dependencies
compile - Classpath for compiling the sources.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
\--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]
default - Configuration for the default artifacts and their dependencies.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
\--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]
groovy - The groovy libraries to be used for this Groovy project.
\--- mule:mule-extras-groovy:1.1.1 [default]
runtime - Classpath for running the compiled sources.
+--- mule:mule-extras-groovy:1.1.1 [default]
\--- commons-beanutils:commons-beanutils:1.8.3 [default]
\--- commons-logging:commons-logging:1.1.1 [compile,master,runtime]
单元测试
编译单元测试
$ gradle testClasses
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses
BUILD SUCCESSFUL
执行单元测试
$ gradle test
:compileJava
:processResources
:classes
:compileTestJava
:processTestResources
:testClasses
:test
执行后,在 build/report/test 下面有测试报告
Paste_Image.png
多源码路径
如下语句添加了源码路径:srcAdditional/main/java
apply plugin: 'java'
sourceSets.main.java.srcDirs =
["src/main/java", "srcAdditional/main/java"]
定义默认任务
apply plugin: 'java'
defaultTasks 'clean', 'build'
Maven 插件
当我们要发布Jar包供其他人使用,尤其是使用Maven构建的项目,此时我们需要引入 maven 插件生产 pom.xml 文件,如下所示 archivesBaseName 原来是Java插件使用的,现在也可以被Maven插件使用,用来生产 pom.xml 文件中的 artifactId:
构建脚本
apply plugin: 'java'
apply plugin: 'maven'
group = 'com.gradleware.samples'
// archivesBaseName 如果没有设置,默认使用 project.name (缺点,不能更改)
archivesBaseName = 'gradle-demo'
version = '1.0-SNAPSHOT'
description ="A sample project that uses the Maven plug-in and defines many attributes."
产生 pom.xml 文件
- install - Installs the 'archives' artifacts into the local Maven repository
$ gradle install
:compileJava
:processResources
:classes
:jar
:install
产生的 pom 文件
构件发布:uploadArchives
build.gradle 文件中声明构件属性
// 构件属性
group 'com.itc.demo'
archivesBaseName = 'gradle-demo'
version = '1.0-SNAPSHOT'
description = "A sample project that uses the Maven plug-in and defines many attributes."
紧接着声明发布属性
// 发布构件
uploadArchives {
repositories {
mavenDeployer {
repository(url: repositoriesUploadReleasesUrl) {
authentication(userName: nexusDeployUsername, password: nexusDeployPassword)
}
snapshotRepository(url: repositoriesUploadSnapshotsUrlDev) {
authentication(userName: nexusDeployUsername, password: nexusDeployPassword)
snapshotTimeout = 0
}
uniqueVersion = false
}
}
}
代码审查
先来看看Gradle构建脚本的配置:
gradle.properties中声明SonarQube属性
# SonarQube服务地址
systemProp.sonar.host.url=http://sonar.inspur.com:9000
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
# SonarQube 中用户的Token
systemProp.sonar.login=1df1b3143013f43640b036ef160724935edd3c27
build.gradle 中引入SonarQube插件
// 代码审查
plugins {
id "org.sonarqube" version "2.2.1"
}
执行代码审查
gradlew sonarqube
任务结束后,审查后的数据就到了SonarQube的WebConsol里面
Paste_Image.png
网友评论