05 - Gradle构建任务

前面几篇介绍了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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,576评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,515评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,017评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,626评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,625评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,255评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,825评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,729评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,271评论 1 320
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,363评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,498评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,183评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,867评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,338评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,458评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,906评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,507评论 2 359

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,699评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,848评论 6 342
  • 转载注明出处:http://www.jianshu.com/p/5255b100930e 0. 前言 完全由个人翻...
    王三的猫阿德阅读 2,521评论 0 4
  • 1.介绍 如果你正在查阅build.gradle文件的所有可选项,请点击这里进行查阅:DSL参考 1.1新构建系统...
    Chuckiefan阅读 12,142评论 8 72
  • 美国作家爱默生说:自信是成功的第一秘诀。 可以说,拥有自信,就意味着拥有更多机会。 那么,如何增强自信呢? 《自信...
    人间最美是清欢阅读 362评论 0 2