pre commit script for code review using spotbug

120 Views Asked by At

I am trying to write a script for a code review using a pre-commit hook. I am looking for a script that will run on every commit and perform the code review using SpotBugs or a similar tool. I have already tried the SpotBugs command-line spotbugs-4.7.3\lib\spotbugs.jar. My project is in Java 8, and I am using Windows. Thanks.

pre-commit script for code review using 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

After running this I am getting an empty file. if I tried manually it is showing around 600 bugs. looking for correct script which will review and generate outfile.

Error while committing the files:

/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.
1

There are 1 best solutions below

0
Hitesh Kumar On

#Run SpotBugs

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

We have to use java -jar to execute the spotbug jar file.

@knittl you are correct we have to use java -jar, it was taking time because first time it is generating around 31 MB file.

To reduce the file use below, it reduced to 200 kb.

-nested:false

The entire working script:

#!/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