jenkins+Warnings Next Generation Plugin构建代码自动化检测
背景现在大多数企业,都会选择使用自动化的方式去构建代码.UT,打包,部署等等,一条龙服务,为了产出优质的代码,代码检测当然是必不可少的.这个代码检测当然不是由我们手动检查,当然是由工具帮我们自动完成,下面就是我们要介绍的重点咯,jenkins+Warnings Next Generation Plugin实现自动化代码检测.Warnings Next Generation PluginW...
背景
现在大多数企业,都会选择使用自动化的方式去构建代码.UT,打包,部署等等,一条龙服务,为了产出优质的代码,代码检测当然是必不可少的.这个代码检测当然不是由我们手动检查,当然是由工具帮我们自动完成,下面就是我们要介绍的重点咯,
jenkins+Warnings Next Generation Plugin
实现自动化代码检测.
Warnings Next Generation Plugin
Warnings Next Generation Plugin
是个什么东西?下面简单的介绍一下,Warnings Next Generation Plugin
是一款jenkins
的插件,它的出现是为了简化检测工具的安装,说白了就是一个静态代码分析工具的集合.它内置了很多的静态代码检测工具,有了这个我们可以很方便的实现代码检测.
下面来看下它的支持:
那么首先贴出github地址:https://github.com/jenkinsci/warnings-ng-plugin
上面展示的是用的比较多的.具体的详细信息,有个列表
https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md
总而言之,这是一款很强大的工具,在内部集成了很多的分析工具,然后通过这个工具的整合,可以将这些信息收集起来,使用chars
的图表形式,很方便的寻找我们代码的问题.
整合的最终效果图如下:
看起来是不是很炫酷...
接下来就是看看怎么去实现这些东西..,
这里呢,我们不采用UI的配置功能,而是采取脚本(Jenkinsfile
),使用pipeline
构建,这样的好处就是,写了这个脚本后,可以拿在任何地方复用,而不是每次在UI上进行重复的劳动.
checkstyle
如何构建checkstyle,请参考我前面的一篇记载文章git下使用checkstyle构建代码风格检查.
上面的写的是进行git hook
进行代码检测,但是为了某些童鞋越狱,所以还是要在jenkins
上设置一道关卡的.
如何设置呢?
我们采取maven的配置方式:
- 首先需要在pom文件中做如下配置,这个是用来编译的
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>${checkstyle.config.location}</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
- 还要提供输出的检测报告,让jenkins能读取解析,下面的配置就是进行输出报告的.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<configuration>
<configLocation>${checkstyle.config.location}</configLocation>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</pligins>
</reporting>
- 配置
Jenkinsfile
,
pipeline {
agent {
label 'master'
}
stages {
stage('单元测试') {
steps {
sh "./release/unittest.sh"
}
}
stage('镜像编译') {
steps {
sh "./release/build-image.sh ${SERVICE}"
recordIssues enabledForFailure: true, tool: checkStyle(), sourceCodeEncoding: 'UTF-8',referenceJobName:'sso-service/master'
}
}
}
post {
failure {
sendNotification('FAILED')
}
}
}
recordIssues enabledForFailure: true, tool: checkStyle(), sourceCodeEncoding: 'UTF-8',referenceJobName:'sso-service/master'
这条语句就是启用jenkins
的checksyle.
有了这个,哪里有问题,我们可以直接定位到源码:
findbugs
findbugs也是一款代码检测工具,不过这款工具专注于字节码层面的检测,可以检测NEP,资源没释放等等一系列的问题.
所以在适应findbugs时,必须要确保你的代码穿上到jenkins后,是经过编译的.可以先执行mvn test
.
- 设置pom文件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs.version}</version>
<!--在compile后自动执行check,必须事先compile编译过,不然findbugs不能发现bug-->
<executions>
<execution>
<id>findbugs-check</id>
<phase>compile</phase>
<!--<goals>-->
<!--<goal>check</goal>-->
<!--</goals>-->
</execution>
</executions>
</plugin>
注意:
在上传到jenkins上时,需要把goa<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>${findbugs.version}</version> </plugin>ls
给注释掉,不然当findbugs找到错误,jenkins的构建过程将直接终止.这样jenkins就无法收集报告日志.无法展现我们的bug图表,只能去console
上看日志输出信息.
- 同样也需要提供输出报告,这样jenkins就可以进行收集解析.
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs.version}</version>
</plugin>
</plugins>
</reporting>
- 配置
jenkinsfile
pipeline {
agent {
label 'master'
}
stages {
stage('单元测试') {
steps {
sh "./release/unittest.sh"
}
}
stage('镜像编译') {
steps {
sh "./release/build-image.sh ${SERVICE}"
recordIssues enabledForFailure: true, tool: findBugs(), sourceCodeEncoding: 'UTF-8',referenceJobName:'sso-service/master'
}
}
}
post {
failure {
sendNotification('FAILED')
}
}
}
注意
findbugs一定是要通过编译之后,才可以进行代码检测的.
PMD&&CPD
PMD也是一款代码静态代码分析工具,这个与上面的有些类似,但是它们之间并补冲突,各个侧重点不一样,这个主要侧重于.没有引用的变量,多余的包等等一系列信息.
CPD是专注于代码重复性检测,会检测项目中存在的代码,可以很好的处理掉我们的垃圾代码.
CDP
被集成在PMD
中了,是需要配置一下就可以启用CPD
- 设置pom文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<!--开启增量分析 -->
<!--加快PMD的执行速度,同时保持分析的质量。-->
<analysisCache>true</analysisCache>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<!--false禁用此选项以使用0退出并只编写报告-->
<failOnViolation>false
</failOnViolation>
<printFailingErrors>true</printFailingErrors>
</configuration>
<executions>
<execution>
<id>verify</id>
<!--verify阶段可以使用cpm的功能-->
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
- 输出report报告
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
</plugin>
- 配置
jenkinsfile
pipeline {
agent {
label 'master'
}
stages {
stage('单元测试') {
steps {
sh "./release/unittest.sh"
}
}
stage('镜像编译') {
steps {
sh "./release/build-image.sh ${SERVICE}"
recordIssues enabledForFailure: true, tool: cpd(), sourceCodeEncoding: 'UTF-8',referenceJobName:'sso-service/master'
recordIssues enabledForFailure: true, tool: pmdParser(), sourceCodeEncoding: 'UTF-8',referenceJobName:'sso-service/master'
}
}
}
post {
failure {
sendNotification('FAILED')
}
}
}
更多推荐
所有评论(0)