全文速览:
关于企业内部如何管控代码规范、保证应用服务质量是一个基础性的问题,只要是有技术性要求的项目,都应该遵守一套既定的保障应用代码质量的规范。
常规的小成本而有见效的方式就是使用编辑器或者代码规范检测的方式,控制代码的提交。提交的代码再依赖组件,做人工的code review来把关。
常见的规范扫描工具:阿里编码规约扫描、checkStyle。
今天主要描述这两个工具如何在日常编码过程中使用,以及如何与git配合来规范代码的提交。
阿里编码规约扫描是一个成熟的组件,我这边使用的是IDEA:
以上两种方式已经可以成功安装插件,只需要【Restart IDE】就可以生效。
重启后,在某一个类文件中右击或者在顶部菜单栏【Tools -> 阿里编码规约】下就可以发现它,点击即可触发扫描。
如果IDE性能有限,建议关闭实时检测。
注意checkStyle与JAVA版本的对应关系,否则启动检测你将发现提示has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
。
JRE And JDK:Runtime of Checkstyle is limited only by minimal version or JRE.
Checkstyle version | JRE version |
---|---|
10.x | 11 and above |
7.x, 8.x, 9.x | 8 and above |
6.x | 6 and above |
5.x | 5 and above |
官方文档链接:https://checkstyle.sourceforge.io/
通用流程是一致的,两者就是在指令以及输出内容上有所差异,一般pre-commit的配置如下:
cp pre-commit.example pre-commit
生成正式生效的文件 pre-commit以下主要展示两者的demo
指令: java -cp ./p3c-pmd-2.1.1-jar-with-dependencies.jar net.sourceforge.pmd.PMD -d ./Task.java -R rulesets/java/ali-comment.xml,rulesets/java/ali-concurrent.xml,rulesets/java/ali-constant.xml,rulesets/java/ali-exception.xml,rulesets/java/ali-flowcontrol.xml,rulesets/java/ali-naming.xml,rulesets/java/ali-oop.xml,rulesets/java/ali-orm.xml,rulesets/java/ali-other.xml,rulesets/java/ali-set.xml -f text
Available report formats and their configuration properties are:
codeclimate: Code Climate integration.
csv: Comma-separated values tabular format.
problem - Include Problem column default: true
package - Include Package column default: true
file - Include File column default: true
priority - Include Priority column default: true
line - Include Line column default: true
desc - Include Description column default: true
ruleSet - Include Rule set column default: true
rule - Include Rule column default: true
emacs: GNU Emacs integration.
empty: Empty, nothing.
html: HTML format
linePrefix - Prefix for line number anchor in the source file.
linkPrefix - Path to HTML source.
ideaj: IntelliJ IDEA integration.
classAndMethodName - Class and Method name, pass '.method' when processing a directory. default:
sourcePath - Source path. default:
fileName - File name. default:
summaryhtml: Summary HTML format.
linePrefix - Prefix for line number anchor in the source file.
linkPrefix - Path to HTML source.
text: Text format.
textcolor: Text format, with color support (requires ANSI console support, e.g. xterm, rxvt, etc.).
color - Enables colors with anything other than 'false' or '0'. default: yes
textpad: TextPad integration.
vbhtml: Vladimir Bossicard HTML format.
xml: XML format.
encoding - XML encoding format, defaults to UTF-8. default: UTF-8
xslt: XML with a XSL Transformation applied.
encoding - XML encoding format, defaults to UTF-8. default: UTF-8
xsltFilename - The XSLT file name.
yahtml: Yet Another HTML format.
outputDir - Output directory.
样例:
#!/bin/sh
# claire
# From java package
# alicheck version: 2.2.1
# jdk version: 1.8
function print(){
echo "aliCheck>> $*"
}
print "Javacode stylecheck starting, please wait..."
wd=`pwd`
print "Workdir: $wd"
check_jar_path="$wd/.git/jars/p3c-pmd-2.1.1-jar-with-dependencies.jar"
check_xml_path="rulesets/java/ali-comment.xml,rulesets/java/ali-concurrent.xml,rulesets/java/ali-constant.xml,rulesets/java/ali-exception.xml,rulesets/java/ali-flowcontrol.xml,rulesets/java/ali-naming.xml,rulesets/java/ali-oop.xml,rulesets/java/ali-orm.xml,rulesets/java/ali-other.xml,rulesets/java/ali-set.xml"
check_result_file="$wd/temp"
# 清空temp文件
rm -rf $check_result_file
is_err=0
is_warn=0
path=''
for file in `git status --porcelain | sed s/^...// | grep '\.java$' | grep -v 'test'`;
do
path+="$wd/$file "
done
if [ "x${path}" != "x" ];then
print "Check file: $path"
re=`java -cp $check_jar_path net.sourceforge.pmd.PMD -d $path -R $check_xml_path -f text >> $check_result_file`
err=`cat temp | grep "va:"`
err_count=`cat temp | grep "va:" | wc -l`
if [[ $err = *"va:"* ]];then
print "detect error lines count: ${err_count}"
print "${err}"
is_err=1
fi
fi
print "Javacode stylecheck finished. Thank you for your commit!"
rm -rf $check_result_file
if [ $is_err -ne 0 ] || [ $is_warn -ne 0 ]
then
print "Please return and fix stylecheck warnings before code commit!"
exit 1
fi
exit 0
指令:java -jar ./checkstyle-8.45-all.jar -c ./checkStyle.xml /Task.java -f text -o outputTempFile
样例:
#!/bin/sh
# claire
# From java package
# checkStyle version: 8.45
# jdk version: 1.8
function print(){
echo "checkStyle>> $*"
}
print "Javacode stylecheck starting, please wait..."
wd=`pwd`
print "Workdir: $wd"
check_jar_path="$wd/.git/jars/checkstyle-8.45-all.jar"
check_xml_path="$wd/.git/files/checkStyle.xml"
check_result_file="$wd/temp"
# 清空temp文件
rm -rf $check_result_file
is_err=0
is_warn=0
path=''
for file in `git status --porcelain | sed s/^...// | grep '\.java$' | grep -v 'test'`;
do
path+="$wd/$file "
done
if [ "x${path}" != "x" ];then
print "Check file: $path"
re=`java -jar $check_jar_path -c $check_xml_path $path -f plain -o $check_result_file`
err=`cat temp | grep "ERROR"`
err_count=`cat temp | grep "ERROR" | wc -l`
warn=`cat temp | grep "WARN"`
warn_count=`cat temp | grep "WARN" | wc -l`
info=`cat temp`
if [[ $err = *"ERROR"* ]];then
print "detect error lines count: ${err_count}"
print "${err}"
is_err=1
fi
if [[ $warn = *"WARN"* ]];then
print "detect warning lines count: ${warn_count}"
print "${warn}"
is_warn=1
fi
fi
print "Javacode stylecheck finished. Thank you for your commit!"
rm -rf $check_result_file
if [ $is_err -ne 0 ] || [ $is_warn -ne 0 ]
then
print "Please return and fix stylecheck warnings before code commit!"
exit 1
fi
exit 0
以上任意一款git pre commit 配置完成后
提交后就会开启代码检测,如果存在拦截的逻辑,就无法继续commit
aliCheck:
git commit -m "test1"
aliCheck>> Javacode stylecheck starting, please wait...
aliCheck>> Workdir: xxxx
aliCheck>> Check file: xxxx/Task.java
aliCheck>> detect error lines count: 4
aliCheck>> xxx/Task.java:64: 方法名【UUUSE2345789】不符合lowerCamelCase命名风格
aliCheck>> Javacode stylecheck finished. Thank you for your commit!
aliCheck>> Please return and fix stylecheck warnings before code commit!
checkStyle:
git commit -m "test2"
checkStyle>> Javacode stylecheck starting, please wait...
checkStyle>> Workdir: XXX
checkStyle>> Check file: xxx/Task.java
Checkstyle ends with 28 errors.
checkStyle>> detect error lines count: 28
checkStyle>> [ERROR] xxx/Task.java:3: Not allow chinese character ! [RegexpSingleline]
......
[ERROR] xxx/Task.java:71:29: '+' is not preceded with whitespace. [WhitespaceAround]
checkStyle>> Javacode stylecheck finished. Thank you for your commit!
checkStyle>> Please return and fix stylecheck warnings before code commit!