|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +# set com.example.demo and all chid packages (.- means all children, .* this package only) |
| 4 | +# PACKAGES="com.example.demo.-" |
| 5 | +# source path to prepend to the class path |
| 6 | +# BASEPATH="src/main/java" |
| 7 | +# DEPENDENCIES_PATH="~/.m2" |
| 8 | +# OUTPUT_TYPE="sarif" |
| 9 | + |
3 | 10 | # Check whether to use latest version of PMD |
4 | | -if [ "$SPOTBUGS_VERSION" == 'latest' ]; then |
| 11 | +if [ "$SPOTBUGS_VERSION" == 'latest' ] || [ "$SPOTBUGS_VERSION" == "" ]; then |
5 | 12 | LATEST_TAG="$(curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/spotbugs/spotbugs/releases/latest | jq --raw-output '.tag_name')" |
6 | 13 | SPOTBUGS_VERSION=$LATEST_TAG |
7 | 14 | fi |
8 | 15 |
|
9 | 16 | # Download SpotBugs |
10 | | -wget https://github.com/spotbugs/spotbugs/releases/download/"${SPOTBUGS_VERSION}"/spotbugs-"${SPOTBUGS_VERSION}".zip |
11 | | -unzip spotbugs-"${SPOTBUGS_VERSION}".zip |
| 17 | +wget -q -N https://github.com/spotbugs/spotbugs/releases/download/"${SPOTBUGS_VERSION}"/spotbugs-"${SPOTBUGS_VERSION}".zip |
| 18 | +unzip -q -o spotbugs-"${SPOTBUGS_VERSION}".zip |
12 | 19 |
|
13 | 20 | # Run SpotBugs |
14 | 21 | SPOTBUGS_HOME=spotbugs-"${SPOTBUGS_VERSION}" |
15 | 22 | SPOTBUGS=${SPOTBUGS_HOME}/bin/spotbugs |
16 | | -sh $SPOTBUGS -textui -output "${OUTPUT}" "${ARGUMENTS}" "${TARGET}" |
| 23 | + |
| 24 | +#sh $SPOTBUGS -textui -output "${OUTPUT}" "${ARGUMENTS}" "${TARGET}" |
| 25 | + |
| 26 | +# Take care of parameter order, sometimes does not work if you change it |
| 27 | + |
| 28 | +CMD="java -Xmx1900M -Dlog4j2.formatMsgNoLookups=true \ |
| 29 | + -jar ${SPOTBUGS_HOME}/lib/spotbugs.jar -textui " |
| 30 | + |
| 31 | +if [ "$PACKAGES" != "" ]; then |
| 32 | + CMD="$CMD -onlyAnalyze ${PACKAGES}" |
| 33 | +fi |
| 34 | + |
| 35 | +CMD="$CMD -quiet -effort:max -low -noClassOk" |
| 36 | + |
| 37 | +case $OUTPUT_TYPE in |
| 38 | + "xml") |
| 39 | + if [ "$OUTPUT" == "" ]; then |
| 40 | + OUTPUT="results.xml" |
| 41 | + fi |
| 42 | + CMD="$CMD -xml:withMessages=./$OUTPUT" |
| 43 | + ;; |
| 44 | + "html") |
| 45 | + if [ "$OUTPUT" == "" ]; then |
| 46 | + OUTPUT="results.html" |
| 47 | + fi |
| 48 | + CMD="$CMD -html:withMessages=./$OUTPUT" |
| 49 | + ;; |
| 50 | + "emacs") |
| 51 | + if [ "$OUTPUT" == "" ]; then |
| 52 | + OUTPUT="results.emacs" |
| 53 | + fi |
| 54 | + CMD="$CMD -emacs:withMessages=./$OUTPUT" |
| 55 | + ;; |
| 56 | + "xdocs") |
| 57 | + if [ "$OUTPUT" == "" ]; then |
| 58 | + OUTPUT="results.xdocs" |
| 59 | + fi |
| 60 | + CMD="$CMD -xdoc:withMessages=./$OUTPUT" |
| 61 | + ;; |
| 62 | + *) |
| 63 | + OUTPUT_TYPE="sarif" |
| 64 | + if [ "$OUTPUT" == "" ]; then |
| 65 | + OUTPUT="results.sarif" |
| 66 | + fi |
| 67 | + CMD="$CMD -sarif:withMessages=./resultspre.sarif" |
| 68 | + ;; |
| 69 | +esac |
| 70 | + |
| 71 | +if [ "$DEPENDENCIES_PATH" != "" ]; then |
| 72 | + DEP_CMD="find ${DEPENDENCIES_PATH} -name \"*.jar\" -type f > /tmp/jardependencies.txt" |
| 73 | + echo "Scanning jars with: ${DEP_CMD}" |
| 74 | + eval ${DEP_CMD} |
| 75 | + CMD="$CMD -auxclasspathFromFile /tmp/jardependencies.txt" |
| 76 | + echo "Found dependencies: " |
| 77 | + cat /tmp/jardependencies.txt |
| 78 | +fi |
| 79 | + |
| 80 | +if [ "$PROGRESS" == "true"]; then |
| 81 | + CMD="$CMD -progress" |
| 82 | +fi |
| 83 | + |
| 84 | +if [ "$BASE_PATH" != "" ]; then |
| 85 | + if [[ "$BASE_PATH" != */ ]]; then |
| 86 | + BASE_PATH="$BASE_PATH/" |
| 87 | + fi |
| 88 | + # using sourcepath does not work for GitHub's sarif parser |
| 89 | + # but keeping there just in case |
| 90 | + CMD="$CMD -sourcepath ${BASE_PATH}" |
| 91 | +fi |
| 92 | + |
| 93 | +if [ "$ARGUMENTS" != "" ]; then |
| 94 | + CMD="$CMD ${ARGUMENTS}" |
| 95 | +fi |
| 96 | + |
| 97 | +if [ "$TARGET" != "" ]; then |
| 98 | + CMD="$CMD ${TARGET}" |
| 99 | +else |
| 100 | + CMD="$CMD ." |
| 101 | +fi |
| 102 | + |
| 103 | +echo "Running SpotBugs with command: $CMD" |
| 104 | + |
| 105 | +eval ${CMD} |
| 106 | + |
| 107 | +if [ "$OUTPUT_TYPE" == "sarif" ] && [ "$BASE_PATH" != "" ]; then |
| 108 | + # prepend the pyhsical path |
| 109 | + echo "Transform sarif file to include the physical path" |
| 110 | + jq -c "(.runs[].results[].locations[].physicalLocation.artifactLocation.uri) |=\"$BASE_PATH\"+." resultspre.sarif > "$OUTPUT" |
| 111 | +fi |
| 112 | + |
0 commit comments