本地代码-gitlab-sonar一条龙(3)-[sonar-p3c-pmd]

本地代码-gitlab-sonar一条龙(3)-[sonar-p3c-pmd],第1张

本地代码-gitlab-sonar一条龙(3)-[sonar-p3c-pmd] 1、P3C-PMD

下载地址:

https://github.com/alibaba/p3c

编译:

com.alibaba.p3c
p3c-pmd
2.1.1

 mvn package 产生 p3c-pmd-2.1.1-jar-with-dependencies.jar

用命令测试一下,是否能检查代码:

java -cp p3c-pmd-2.1.1-jar-with-dependencies.jar net.sourceforge.pmd.PMD -d  ./  -R rulesets/java/ali-comment.xml

2、gitlab中设置pre-recive进行push前检查 

进入gitlab中某一个项目的路径:/var/opt/gitlab/git-data/repositories/@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git/custom_hooks/pre-receive.d

 暂时先在目录下放p3c-pmd-2.1.1-jar-with-dependencies.jar和pre-receive:

pre-receive脚本

#!/bin/sh
#
# An example hook script to make use of push options.
# The example simply echoes all push options that start with 'echoback='
# and rejects all pushes when the "reject" push option is used.
#
# To enable this hook, rename this file to "pre-receive".
EJECT=0                 #REJECT变量,初始化为0

JAVA_HOME=/var/opt/gitlab/jdk18    #定义JAVA_HOME变量为当前jdk安装路径  /var/opt/gitlab/jdk18
base_PATH=$(cd `dirname $0`; pwd)     #base_PATH变量中为当前脚本存放的路径,比如当前脚本路径为/usr/local/script/shell.sh,则base_PATH=/usr/local/script/

while read oldrev newrev refname; do    #接收键盘输入的参数,空格分隔,分别赋值给oldrev, newrev, refname(推测这里oldrev,newrev为同一个代码仓库的两个分支名称)
    echo 'old version:'${oldrev}        #输出 oldrev
    echo 'new version:'${newrev}        #输出 newrev
    echo 'branc:'${refname}             #输出 refname

    if [ "$oldrev" = "0000000000000000000000000000000000000000" ];then  #如果oldrev的值是“00000000……”,那么将oldrev的值改成newrev拼接"^"的值
        oldrev="${newrev}^"
    fi

    FILES=`git diff --name-only ${oldrev} ${newrev}  | grep -e ".java$"`    #执行git命令,找出oldrev分支和newrev分支中内容有差异的.java文件,将这些文件名放到FILES变量中

    if [ -n "$FILES" ]; then                                                #如果FILES变量有值,创建TEMPDIR变量,存放当前脚本路径所在目录地址加/tmp的字符串值
        TEMPDIR=$base_PATH/"tmp"
        for FILE in ${FILES}; do                                            #循环FILES列表,在TEMPDIR所指定的路径下,创建当前文件的父目录
            mkdir -p "${TEMPDIR}/`dirname ${FILE}`" >/dev/null              #例如FILE内容是/abc/de/fg/tt.java   TEMPDIR为/usr/local/script/tmp 则会创建/usr/local/script/tmp/abc/de/fg/目录
            git show $newrev:$FILE > ${TEMPDIR}/${FILE}                     #从git上将FILE文件的内容拷贝到TEMPDIR路径下
        done;

        FILES_TO_CHECK=`find $TEMPDIR -name '*.java'`                       #查找TEMPDIR路径下所有.java文件,将文件名放到FILES_TO_CHECK变量

        echo 'Check files:'${FILES_TO_CHECK}                                #输出FILES_TO_CHECK中的内容(TEMPDIR路径下左右java文件名)
        echo 'Aliyun p3c-pmd check starting.....'
        echo 'Current shell Path:' $base_PATH
        echo 'JAVA_HOME:' $JAVA_HOME
        echo 'Root directory for java sources: '$TEMPDIR


        # $JAVA_HOME/bin/java -Dpmd.language=en -cp $base_PATH/p3c-pmd-2.1.1.jar net.sourceforge.pmd.PMD -d $TEMPDIR -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

         $JAVA_HOME/bin/java -Dpmd.language=en -cp $base_PATH/p3c-pmd-2.1.1-jar-with-dependencies.jar net.sourceforge.pmd.PMD -d $TEMPDIR -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

        REJECT=$?                                                           #REJECT变量接收上面的java命令执行的结果返回值

        echo 'Aliyun p3c-pmd check end...'
        echo '返回值为: '$REJECT                                                     #输出返回值

        rm -rf $TEMPDIR                                                  #删除产生的临时文件
    fi
done

exit $REJECT                                                            #退出脚本程序,退出码为REJECT值

另一篇文章中的shell代码也可参考:https://my.oschina.net/u/3991747/blog/4254070

同时要使配置生效:

# gitlab-ctl reconfigure

push代码,出现拒绝提交:

3、eclipase插件

 

4、构建 sonar-p3c plugin插件

构建源码地址:

https://github.com/rhinoceros/sonar-p3c-pmd/

我选择的3.1.3分支,sonarque版本选的是

  • Community Edition
  • 版本 7.9.6 

 PMD是啥意思

一条校验规则对应分别对应3个配置文件:

1、pmd.properties (srcmainresourcesorgsonarl10n)

2、rules.xml(srcmainresourcesorgsonarpluginspmd)

3、pmd-model.xml (srcmainresourcescomsonarsqale)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5574548.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存