DevOps之Pipeline集成junit、jacoco、SonarQube(二)
一、准备工作
1、准备一个持续集成的代码工程
工程下载地址:
Github地址为:https://github.com/zbbkeepgoing/springboot-demo
2、springboot-demo代码工程介绍
整个Web工程有一个Index页面,上面有两个按钮,分别对应两个接口,其中一个接口直接返回信息,另外一个接口则是内存中请求一次延时1s,最大延时为10s。而对应Index会有一个接口,所以Web工程一共有3个接口。延时接口主要是为了后续性能测试
①工程结构
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dxc
│ │ └── ddccloud
│ │ └── demo
│ │ ├── controller
│ │ │ └── DemoController.java #控制器,接口定义类
│ │ └── DemoApplication.java #启动类
│ └── resources
│ ├── application.properties #配置文件
│ └── templates
│ └── index.html #首页Index
└── test
└── java
└── com
└── dxc
└── ddccloud
└── demo
└── DemoControllerTests.java #单元测试类
②DemoController.java
package com.dxc.ddccloud.demo.controller; import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView; @RestController
public class DemoController { public int tmp = ; @RequestMapping("/") #首页接口
public ModelAndView index(ModelAndView mv) {
mv.setViewName("index");
mv.addObject("requestname","This request is IndexApi");
return mv;
} @RequestMapping("/rightaway") #立即返回接口
public ModelAndView returnRightAway(ModelAndView mv) {
mv.setViewName("index");
mv.addObject("requestname","This request is RightawayApi");
return mv;
} @RequestMapping("/sleep") #延时接口
public ModelAndView returnSleep(ModelAndView mv) throws InterruptedException {
Thread.sleep(tmp*);
if(tmp < ) {
tmp++;
}
mv.setViewName("index");
mv.addObject("requestname","This request is SleepApi"+",it will sleep "+ tmp +"s !");
return mv;
}
}
3、下载代码工程放到公司内部私有仓库GitLab

①安装Junit插件

②安装jacoco插件

③安装SonarQube Scanne
说明:上述插件具体怎么配置,请自行爬楼参考以前的博客
二、新建Pipeline工程springboot-demo
1、Pipeline内容
node('build-slave01') {
gitlabCommitStatus(name:"Commit Build"){
stage('拉取代码'){
git credentialsId: 'd1627e89-1967-458e-84a8-07c9a9599957', url: 'git@10.0.0.56:springboot/springboot-demo.git' #私有仓库地址
}
}
stage('Build'){
dir(env.WORKSPACE){
sh "mvn clean compile" #maven编译
sh "mvn package" #maven打包
}
}
stage('Junit'){
dir(env.WORKSPACE){
junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml' #Junit插件收集单元测试结果
}
}
stage('jacoco'){
dir(env.WORKSPACE){
jacoco exclusionPattern: 'src/main/java', inclusionPattern: '**/classes' #jacoco
}
}
//这里需要注意的配置
// Path to exec files: **/jacoco.exec 可执行文件路径
// Path to class directories: 这个配置的是源代码编译后的字节码目录,也就是classes目录不是test-classes目录,如果有多个可以指定多个
// Path to source directories: 这个配置的是源代码的目录,也就是src/main/java目录,如果有多个可以指定多个
stage('SonarQube') {
withSonarQubeEnv('SonarQube') {
sh "${SONAR_SCANNER_HOME}/bin/sonar-scanner \
-Dsonar.projectKey=springboot-demo \
-Dsonar.projectName=springboot-demo \
-Dsonar.sources=.\
-Dsonar.host.url=SonarQube地址 \
-Dsonar.language=java \
-Dsonar.sourceEncoding=UTF- \
}
}
}
2、Jacoco配置

这里需要注意的配置:
- Path to exec files: **/jacoco.exec 可执行文件路径
- Path to class directories: 这个配置的是源代码编译后的字节码目录,也就是
classes目录不是test-classes目录,如果有多个可以指定多个 - Path to source directories: 这个配置的是源代码的目录,也就是
src/main/java目录,如果有多个可以指定多个
3、执行Pipeline
①Junit执行日志
Tests run: , Failures: , Errors: , Skipped: , Time elapsed: 2.896 sec - in com.dxc.ddccloud.demo.DemoControllerTests
-- ::23.072 INFO --- [ Thread-] o.s.w.c.s.GenericWebApplicationContext : Closing org.springframework.web.context.support.GenericWebApplicationContext@6497b078: startup date [Sun Jun :: CST ]; root of context hierarchy Results : Tests run: , Failures: , Errors: , Skipped:
②JaCoCo执行日志
[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/**.exec;**/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/**.exec: 0
[JaCoCo plugin] Saving matched execfiles:
[JaCoCo plugin] Saving matched class directories for class-pattern: **/classes:
[JaCoCo plugin] - /app/idc/apps/jenkins/work/workspace/springboot-demo/target/classes files
[JaCoCo plugin] Saving matched source directories for source-pattern: **/src/main/java:
[JaCoCo plugin] Source Inclusions: **/*.java
[JaCoCo plugin] Source Exclusions:
[JaCoCo plugin] - /app/idc/apps/jenkins/work/workspace/springboot-demo/src/main/java 2 files
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: [**/classes]
[JaCoCo plugin] exclusions: [src/main/java]
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=, maxClass=, minMethod=, maxMethod=, minLine=, maxLine=, minBranch=, maxBranch=, minInstruction=, maxInstruction=, minComplexity=, maxComplexity=]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: , method: , line: , branch: , instruction:
③SonarQube执行日志
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_11 Oracle Corporation (-bit)
INFO: Linux 3.10.-.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=129ms
INFO: Server id: 5A0D13D1-AWNtCf_MnAfLtVTozgug
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
INFO: User cache: /root/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=54ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: SonarQube server 6.7.
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=46ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=35ms
INFO: Load active rules
INFO: Load active rules (done) | time=328ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=23ms
INFO: Project key: springboot-demo
INFO: ------------- Scan springboot-demo
INFO: Load server rules
INFO: Load server rules (done) | time=47ms
INFO: Base dir: /app/idc/apps/jenkins/work/workspace/springboot-demo
INFO: Working dir: /app/idc/apps/jenkins/work/workspace/springboot-demo/.sonar
4、效果展示
①Junit单元测试

②SonarQube展示


DevOps之Pipeline集成junit、jacoco、SonarQube(二)的更多相关文章
- DevOps之持续集成SonarQube代码质量扫描
一.SonarQube介绍 SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具.SonarQube 并不是简单地把不同的代码检查 ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- 研发过程及工具支撑 DevOps 工具链集成
https://mp.weixin.qq.com/s/NYm63nkCymIV3DbL4O01dg 腾讯重新定义敏捷 |Q推荐 小智 InfoQ 2020-09-03 敏捷开发奠基人 Robert C ...
- Spring完全基于Java配置和集成Junit单元测试
要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...
- 李洪强iOS之集成极光推送二iOS 证书 设置指南
李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...
- springboot集成junit测试与javamail测试遇到的问题
1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...
- Docker+GitLab+Jenkins+kubernetes实现DevOps 持续化集成和持续化部署概念图
Docker+GitLab+Jenkins+kubernetes实现DevOps 持续化集成和持续化部署概念图 转载自:原创 IT综合 作者:百联达 时间:2017-05-09 15:48:08 41 ...
- SpringBoot 集成JUnit
项目太大,不好直接测整个项目,一般都是切割成多个单元,单独测试,即单元测试. 直接在原项目上测试,会把项目改得乱七八糟的,一般是单独写测试代码. 进行单元测试,这就需要集成JUnit. (1)在pom ...
- 集成JUnit测试
集成JUnit测试 既然使用了Spring,那么怎么集成到JUnit中进行测试呢,首先大家能够想到的肯定是: public class TestMain { @Test public void tes ...
随机推荐
- 关于绕过cookie 同源策略,和同时向前台返回图片和脚本的解决方案
绕过cookie的同源策略 向前端写入脚本时使用domain来绕过同源策略. 比如 domain= baidu.com .次脚本生成的cookie可以在 *.baidu.com中使用 /// < ...
- 再谈MV*(MVVM MVP MVC)模式的设计原理—封装与解耦
精炼并增补于:界面之下:还原真实的MV*模式 图形界面的应用程序提供给用户可视化的操作界面,这个界面提供给数据和信息.用户输入行为(键盘,鼠标等)会执行一些应用逻辑,应用逻辑(application ...
- 中间消息件之 Solace(一) Solace的简介
什么是Solace? Solace是一家加拿大注册的公司,总部设在安大略省渥太华. 成立于2001年. 这家公司一直致力于获取信息的艺术和科学,并提供世界上最灵活,最强大的数据移动平台. 背景 在信息 ...
- SpringCloud之Zuul 自定义filter
实现过滤器很简单,只需要继承ZuulFilter,并实现ZuulFilter中的抽象方法. filterType():定义过滤器的类型,它有4种类型,分别是pre.post.routing和error ...
- 2019.11.9 csp-s 考前模拟
2019.11.9 csp-s 考前模拟 是自闭少女lz /lb(泪奔 T1 我可能(呸,一定是唯一一个把这个题写炸了的人 题外话: 我可能是一个面向数据编程选手 作为一个唯一一个写炸T1的人,成功通 ...
- Luogu P5068 [Ynoi2015]我回来了
题目 Ynoi难得的水题. 首先我们可以\(O(n^2)\)地求出任意两点之间的距离. 然后我们可以\(O(n^3)\)地求出对于任意一个点\(u\),跟它距离\(\le d\)的点的集合. 然后对于 ...
- 分层最短路(牛客第四场)-- free
题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...
- php 内存原理
1.内存结构: 栈区:保存变量名,对CPU,读写速度很快 堆区:保存复杂的数据结构,想对象,数组,复杂的字符串等. 数据段:数据段分为数据段全局区(简单的数据,整型和布尔类型)和数据段静态区(存储静态 ...
- python面向对象反射-框架原理-动态导入-元类-自定义类-单例模式-项目的生命周期-05
反射 reflect 反射(reflect)其实是反省,自省的意思 反省:指的是一个对象应该具备可以检测.修改.增加自身属性的能力 反射:通过字符串获取对象或者类的属性,进行操作 设计框架时需要通过反 ...
- Docker 构建私有镜像仓库
在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便,另外有时候只是希望在内部用户之间进行分享,不希望暴露出去.这种情况下,就有必要搭建一个本地 ...