共计 7157 个字符,预计需要花费 18 分钟才能阅读完成。
本文来自:
杨周 极狐 GitLab 高级解决方案架构师
代码越写越标准是优良开发者的成长之路,但很多人对老我的项目感到有心无力,因为太不标准了,所有人停下来一起修复也要花费很长时间,而且一次改变太多难以确保可靠性,怎么办?
有的语言能够借助 Git diff 把本次批改的代码挑出来,实现增量扫描,但 Java 很难这么做。
有的人在继续集成里配置了标准扫描工具,但报错之后须要在成千上万行 log 里查找,升高了研发效率。
Checkstyle 是业界出名的开源扫描工具,可扫描 Sun、Google 等代码标准,提供 Maven、Gradle 插件。本文以 Java 我的项目配置 Checkstyle 扫描工具为例,分享毁灭这些问题的方法:
1. 将代码标准问题显示在「合并申请页面」,大幅度提高研发效率;
2. 增量代码标准报告。
下文别离介绍 「Java Maven 我的项目」 和「Java Gradle 我的项目」的配置办法。
通过极狐 GitLab CI 平滑落地 Java 增量代码标准 Checkstyle Maven
在我的项目中引入 Checkstyle 插件,并下载代码标准:
$ vi pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<violationSeverity>warning</violationSeverity>
<configLocation>config/checkstyle/checkstyle.xml</configLocation>
</configuration>
</plugin>
$ mkdir -p config/checkstyle/
$ wget https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-9.3/src/main/resources/google_checks.xml -O config/checkstyle/checkstyle.xml
执行全量扫描命令,查看成果:
$ ./mvnw checkstyle:check
[WARN] Demo.java:6:1: 短少 Javadoc。[MissingJavadocType]
[WARN] Demo.java:9:1: 行内含有制表符 tab。[FileTabCharacter]
[WARN] Demo.java:9:9: 'method def modifier' 缩进了 8 个缩进符,应为 2 个。[Indentation]
在极狐 GitLab 继续集成中执行强制扫描:
$ vi .gitlab-ci.yml
checkstyle:
script:
- ./mvnw checkstyle:check
当扫描工具报错,继续集成退出,想看代码标准问题,须要到 log 中查找。很多继续集成产品只做到了这一步,但这升高了开发效率,因而极狐 GitLab 更进一步——采集「代码品质报告」。
在我的项目中引入 violations-maven-plugin,它会将 Checkstyle 报告转换成极狐 GitLab 规范格局。
$ vi pom.xml
<plugin>
<groupId>se.bjurr.violations</groupId>
<artifactId>violations-maven-plugin</artifactId>
<version>1.50.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>violations</goal>
</goals>
<configuration>
<!-- Optional config -->
<!-- 0 is disabled -->
<maxReporterColumnWidth>0</maxReporterColumnWidth>
<maxRuleColumnWidth>0</maxRuleColumnWidth>
<maxSeverityColumnWidth>0</maxSeverityColumnWidth>
<maxLineColumnWidth>0</maxLineColumnWidth>
<maxMessageColumnWidth>30</maxMessageColumnWidth>
<!-- Will create a CodeClimate JSON report. -->
<codeClimateFile>gl-code-quality-report.json</codeClimateFile>
<!-- Global configuration, remove if you dont want to report violations
for the entire repo. -->
<!-- INFO, WARN or ERROR -->
<minSeverity>INFO</minSeverity>
<!-- PER_FILE_COMPACT, COMPACT or VERBOSE -->
<detailLevel>VERBOSE</detailLevel>
<!-- Will fail the build if total number of found violations is higher -->
<maxViolations>99999999</maxViolations>
<!-- Will print violations found in diff -->
<printViolations>true</printViolations>
<!-- Diff configuration, remove if you dont want to report violations
for files changed between specific revisions. -->
<!-- Can be empty (ignored), Git-commit or any Git-reference -->
<diffFrom></diffFrom>
<!-- Same as above -->
<diffTo></diffTo>
<!-- INFO, WARN or ERROR -->
<diffMinSeverity>INFO</diffMinSeverity>
<!-- PER_FILE_COMPACT, COMPACT or VERBOSE -->
<diffDetailLevel>VERBOSE</diffDetailLevel>
<!-- Will fail the build if number of violations, in the diff within
from/to, is higher -->
<diffMaxViolations>99</diffMaxViolations>
<!-- Will print violations found in diff -->
<diffPrintViolations>true</diffPrintViolations>
<!-- Where to look for Git -->
<gitRepo>.</gitRepo>
<!-- This is mandatory regardless of if you want to report violations
between revisions or the entire repo. -->
<violations>
<violation>
<parser>CHECKSTYLE</parser>
<reporter>Checkstyle</reporter>
<folder>.</folder>
<pattern>.*/target/checkstyle-result\.xml$</pattern>
</violation>
</violations>
</configuration>
</execution>
</executions>
</plugin>
在极狐 GitLab CI 中采集代码标准报告:
image: eclipse-temurin:8
cache:
paths:
- /root/.m2
stages:
- lint
- build
checkstyle:
allow_failure: true
stage: lint
script:
- ./mvnw checkstyle:check
artifacts:
when: always
untracked: true
report:
stage: lint
script:
- ./mvnw validate
needs:
- job: checkstyle
artifacts: true
artifacts:
reports:
codequality: gl-code-quality-report.json
compile:
stage: build
script:
- ./mvnw package -Dmaven.test.skip=true
artifacts:
untracked: true
能够看到在开发人员频繁应用的「合并申请」页面,间接显示了「代码标准问题」,供开发人员自助修复以及揭示评审的共事留神,这样能够无效进步研发效率。
老我的项目第一次配置代码标准可能会呈现很多谬误(比方上图有 7746 个),没关系,先将标准合并进入骨干,下次批改代码发动合并申请时,极狐 GitLab 将展现「增量代码品质报告」,而无需任何简单设置。比方下图就只有 5 个新问题了:
通过极狐 GitLab CI 平滑落地 Java 增量代码标准 Checkstyle Gradle
「Java Gradle 我的项目」和「Java Maven 我的项目」的配置办法统一:
在我的项目中引入 Checkstyle 插件,并下载代码标准:
$ vi build.gradle
plugins {id 'checkstyle'}
checkstyle {
toolVersion = '9.3'
maxWarnings = 0
maxErrors = 0
}
$ mkdir -p config/checkstyle/
$ wget https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-9.3/src/main/resources/google_checks.xml -O config/checkstyle/checkstyle.xml
执行全量扫描命令,查看成果:
$ ./gradlew check
[WARN] Demo.java:6:1: 短少 Javadoc。[MissingJavadocType]
[WARN] Demo.java:9:1: 行内含有制表符 tab。[FileTabCharacter]
[WARN] Demo.java:9:9: 'method def modifier' 缩进了 8 个缩进符,应为 2 个。[Indentation]
在极狐 GitLab 继续集成中执行强制扫描:
$ vi .gitlab-ci.yml
checkstyle:
script:
- ./gradlew check
同样,在我的项目中引入 violations-maven-plugin,它会将 Checkstyle 报告转换成极狐 GitLab 规范格局。
$ vi build.gradle
buildscript {
repositories {maven { url 'https://plugins.gradle.org/m2/'}
}
dependencies {classpath "se.bjurr.violations:violations-gradle-plugin:1.52.2"}
}
task violations(type: se.bjurr.violations.gradle.plugin.ViolationsTask) {
//
// Optional config
//
maxReporterColumnWidth = 0 // 0 means "no limit"
maxRuleColumnWidth = 60
maxSeverityColumnWidth = 0
maxLineColumnWidth = 0
maxMessageColumnWidth = 50
codeClimateFile = file('gl-code-quality-report.json') // Will create a CodeClimate JSON report.
violationsFile = file('violations-file.json') // Will create a normalized JSON report.
//
// Global configuration, remove if you dont want to report violations for
// the entire repo.
//
minSeverity = 'INFO' // INFO, WARN or ERROR
detailLevel = 'VERBOSE' // PER_FILE_COMPACT, COMPACT or VERBOSE
maxViolations = 99999999 // Will fail the build if total number of found violations is higher
printViolations = true // Will print violations found in diff
//
// Diff configuration, remove if you dont want to report violations for
// files changed between specific revisions.
//
// diff-properties can be supplied with something like:
//
// ./gradlew violations -PdiffFrom=e4de20e -PdiffTo=HEAD
//
// And in Travis, you could add:
//
// script:
// - 'if ["$TRAVIS_PULL_REQUEST"!="false"]; then bash ./gradlew check -PdiffFrom=$TRAVIS_PULL_REQUEST_BRANCH -PdiffTo=$TRAVIS_BRANCH ; fi'
//
diffFrom = project.properties.diffFrom // Can be empty (ignored), Git-commit or any Git-reference
diffTo = project.properties.diffTo // Same as above
diffMinSeverity = 'INFO' // INFO, WARN or ERROR
diffDetailLevel = 'VERBOSE' // PER_FILE_COMPACT, COMPACT or VERBOSE
diffMaxViolations = 99 // Will fail the build if number of violations, in the diff within from/to, is higher
diffPrintViolations = true // Will print violations found in diff
gitRepo = file('.') // Where to look for Git
//
// This is mandatory regardless of if you want to report violations between
// revisions or the entire repo.
//
// Many more formats available, see: https://github.com/tomasbjerre/violations-lib
violations = [["CHECKSTYLE", buildDir.path, ".*/checkstyle/.*\\.xml\$", "Checkstyle"]
]
}
在极狐 GitLab CI 中采集代码标准报告:
image: eclipse-temurin:8
cache:
paths:
- /root/.m2
stages:
- lint
- build
checkstyle:
allow_failure: true
stage: lint
script:
- ./gradlew check
artifacts:
when: always
untracked: true
report:
stage: lint
script:
- ./gradlew violations
needs:
- job: checkstyle
artifacts: true
artifacts:
reports:
codequality: gl-code-quality-report.json
compile:
stage: build
script:
- ./gradlew build -x check -x test
artifacts:
untracked: true
残余步骤与「Java Gradle 我的项目」统一:开发人员频繁应用的「合并申请」页面,间接显示了「代码标准问题」,供开发人员自助修复以及揭示评审的共事留神,无效进步研发效率。
老我的项目第一次配置代码标准可能会呈现很多谬误(比方上图有 7746 个),没关系,先将标准合并进入骨干,下次批改代码发动合并申请时,极狐 GitLab 将展现「增量代码品质报告」,而无需任何简单设置,比方下图只有 5 个新问题:
这样,咱们就通过极狐 GitLab 平滑落地了 Java 增量代码标准,让我的项目代码越来越优雅。