使用 Spotbug 进行代码审查的预提交脚本

问题描述 投票:0回答:1

我正在尝试使用预提交挂钩编写代码审查脚本。我正在寻找一个将在每次提交时运行并使用 SpotBugs 或类似工具执行代码审查的脚本。我已经尝试过 SpotBugs 命令行

spotbugs-4.7.3\lib\spotbugs.jar
。我的项目是Java 8,我使用的是Windows。谢谢。

使用 SpotBug 进行代码审查的预提交脚本:

#!/bin/bash
# Run SpotBugs on Java files to check for issues before committing.
# SpotBugs executable location
SPOTBUGS_PATH=/spotbug/path

# your project's source code location
PROJECT_PATH=/your/project
    
echo "SPOTBUGS_PATH: $SPOTBUGS_PATH"
echo "PROJECT_PATH: $PROJECT_PATH"

#Run SpotBugs
$SPOTBUGS_PATH -textui -effort:max -xml=outReport.xml $PROJECT_PATH


# Check for any issues
if [ $? -eq 0 ]; then

  echo "No bugs found."

  exit 0

else

  echo "Bugs found . Commit aborted."

  exit 1

fi

运行此命令后,我得到一个空文件。如果我手动尝试,会显示大约 600 个错误。寻找正确的脚本来检查并生成输出文件。

提交文件时出错:

/spotbugs.jar: line 1: $'PK\003\004': command not found
/spotbugs.jar: line 2: $'\b\bA\002': command not found
/spotbugs.jar: line 3: syntax error near unexpected token `)'
/spotbugs.jar: line 3: `A���META-INF/MANIFEST.MFmRۮ�0|����+�};!�D����&�|�|A��ٸ  ͑x�gdz3#�ɪ�~I���C�UY�IY��)��A    �P�*;\��+%+��y�n������,^��|*u�۲�2��t:P0 �1�8b�7�KQ�7Y��ڳ��` z)���EH��*_oB�,b]���m�:��X���fh3�=2��V�k�c-��:�Awg��'�Y�^܍�28�[z�k���@��h�qZI{�1+=�}�n@��e�ce�]\��gʃ1����&�;+I�96�ڍ,'��K��VP�m�s���O�c��b��&?�aj��f�    ?1�,ewXջY)��A���~�0�m@�Y��  g�j|W/):�y�ݐ�{}##O�,�*N�[5�eQPK'
SpotBugs found issues. Commit aborted.
java githooks pre-commit-hook pre-commit spotbugs
1个回答
0
投票

#运行 SpotBugs

**java -jar** $SPOTBUGS_PATH -textui -effort:max -xml=outReport.xml $PROJECT_PATH

我们必须使用java -jar 来执行spotbug jar 文件。

@knittl 你是对的,我们必须使用 java -jar,这需要时间,因为第一次它生成大约 31 MB 的文件。

为了减少下面的文件使用,它减少到200 kb。

-嵌套:false

整个工作脚本:

#!/bin/sh

# Define the location of your project's source code
PROJECT_PATH=/project/location

# Define the location of the SpotBugs executable
SPOTBUGS_PATH=/spotbug/path

#current Date
NOW=$(date +"%m%d%Y%H%M%S")

echo "SPOTBUGS_PATH: $SPOTBUGS_PATH"
echo "PROJECT_PATH: $PROJECT_PATH"
echo "Current_Date: $NOW"

#Run SpotBugs
java -jar $SPOTBUGS_PATH -textui -effort:max -xml=bugReport_$NOW.xml -nested:false $PROJECT_PATH


# Check if SpotBugs found any issues
if [ $? -eq 0 ]; then

  echo "No SpotBugs issues found."

  exit 0

else

  echo "Found issues"

  exit 1

fi
© www.soinside.com 2019 - 2024. All rights reserved.