Mac 集成sonar 遇到的坑

安装教程参考下面的文章:
https://www.jianshu.com/p/b41262fca5b8
https://www.jianshu.com/p/7f3b08e3a479
https://www.jianshu.com/p/25db46ec10b3

本文只记录自己在安装中遇到的一些坑

1、根据以上两篇文章,在下载好各个工具时,启动sonar 报错can not run elasticsearch as root
我是使用sudo /usr/local/sonarqube-7.1/bin/macosx-universal-64/sonar.sh start 这种方法启动的
后来改成 cd /usr/local/sonarqube-7.1/bin/macosx-universal-64 然后再执行 ./sonar.sh start ,报错就没有了

2、 遇到 Mysql8 报错 authentication plugin 'caching_sha2_password
小白一个不知道怎么解决,按照网上的方法也不行,后来直接安装了Mysql 5.7,没有这个错误了
3、 执行 mysql -u root -p 报 mysql: command not found
在.bash_profile 中加入 export PATH=$PATH:/usr/local/mysql/bin
4、没有.bash_profile 文件
启动终端 cd ~
输入touch .bash_profile

5、在项目下执行 sh
报错:ERROR: Error during Sonar runner execution
ERROR: Fail to download libraries from server
解决方法:我自己的sonar-runner-2.4/conf 目录下的sonar-runner.properties
修改snoar 登录名和密码也是修改这个文件‼️

sonar-runner.properties 文件代码

#snoar 登录名修改 
sonar.login=admin
sonar.password=admin
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

#sonar.host.url=http://localhost:9000/sonarqubesonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=11111111

image.png

6、我自己本地各个工具版本


image.png

7、run-sonar.sh 文件内容

#### run-sonar.sh ####
#!/bin/bash
## INSTALLATION: script to copy in your Xcode project in the same directory as the .xcodeproj file
## USAGE: ./run-sonar.sh
## DEBUG: ./run-sonar.sh -v
## WARNING: edit your project parameters in sonar-project.properties rather than modifying this script
#

# Global parameters
XCODEBUILD_CMD=xcodebuild
SLATHER_CMD=slather
XCPRETTY_CMD=xcpretty
LIZARD_CMD=lizard

trap "echo 'Script interrupted by Ctrl+C'; stopProgress; exit 1" SIGHUP SIGINT SIGTERM

function startProgress() {
    while true
    do
        echo -n "."
        sleep 5
    done
}

function stopProgress() {
    if [ "$vflag" = "" -a "$nflag" = "" ]; then
        kill $PROGRESS_PID &>/dev/null
    fi
}

function testIsInstalled() {

    hash $1 2>/dev/null
    if [ $? -eq 1 ]; then
        echo >&2 "ERROR - $1 is not installed or not in your PATH"; exit 1;
    fi
}

function testIsXcodeMinMajorVersionAvailable() {    
    XCODE_VERSION="$($XCODEBUILD_CMD -version | grep -a -A 1 "Xcode" | head -n1 | sed "s/Xcode \([0-9]*\)\..*/\1/")"
    if (( "$1" <= "$XCODE_VERSION" )); then
        return 0
    else
        return 1
    fi
}

function readParameter() {

    variable=$1
    shift
    parameter=$1
    shift

    eval $variable="\"$(sed '/^\#/d' sonar-project.properties | grep $parameter | tail -n 1 | cut -d '=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')\""
}

# Run a set of commands with logging and error handling
function runCommand() {

    # 1st arg: redirect stdout
    # 2nd arg: command to run
    # 3rd..nth arg: args
    redirect=$1
    shift

    command=$1
    shift

    if [ "$nflag" = "on" ]; then
        # don't execute command, just echo it
        echo
        if [ "$redirect" = "/dev/stdout" ]; then
            if [ "$vflag" = "on" ]; then
                echo "+" $command "$@"
            else
                echo "+" $command "$@" "> /dev/null"
            fi
        elif [ "$redirect" != "no" ]; then
            echo "+" $command "$@" "> $redirect"
        else
            echo "+" $command "$@"
        fi

    elif [ "$vflag" = "on" ]; then
        echo

        if [ "$redirect" = "/dev/stdout" ]; then
            set -x #echo on
            $command "$@"
            returnValue=$?
            set +x #echo off
        elif [ "$redirect" != "no" ]; then
            set -x #echo on
            $command "$@" > $redirect
            returnValue=$?
            set +x #echo off
        else
            set -x #echo on
            $command "$@"
            returnValue=$?
            set +x #echo off
        fi

        if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
            stopProgress
            echo "ERROR - Command '$command $@' failed with error code: $returnValue"
            exit $returnValue
        fi
    else

        if [ "$redirect" = "/dev/stdout" ]; then
            $command "$@" > /dev/null
        elif [ "$redirect" != "no" ]; then
            $command "$@" > $redirect
        else
            $command "$@"
        fi

        returnValue=$?
        if [[ $returnValue != 0 && $returnValue != 5 ]] ; then
            stopProgress
            echo "ERROR - Command '$command $@' failed with error code: $returnValue"
            exit $?
        fi


        echo
    fi
}

## COMMAND LINE OPTIONS
vflag=""
nflag=""
unittests="on"
oclint="on"
fauxpas="on"
lizard="on"
sonarscanner=""

while [ $# -gt 0 ]
do
    case "$1" in
    -v) vflag=on;;
    -n) nflag=on;;
    -nounittests) unittests="";;
    -nooclint) oclint="";;
    -nofauxpas) fauxpas="";;
    -usesonarscanner) sonarscanner="on";;
    --) shift; break;;
    -*)
        echo >&2 "Usage: $0 [-v]"
        exit 1;;
    *)  break;;     # terminate while loop
    esac
    shift
done

# Usage OK
echo "Running run-sonar.sh..."

## CHECK PREREQUISITES

# xctool, oclint installed
testIsInstalled $XCODEBUILD_CMD
testIsInstalled oclint

# sonar-project.properties in current directory
if [ ! -f sonar-project.properties ]; then
    echo >&2 "ERROR - No sonar-project.properties in current directory"; exit 1;
fi

## READ PARAMETERS from sonar-project.properties

# Read destination simulator
destinationSimulator=''; readParameter destinationSimulator 'sonar.objectivec.simulator'

# Your .xcworkspace/.xcodeproj filename
workspaceFile=''; readParameter workspaceFile 'sonar.objectivec.workspace'
projectFile=''; readParameter projectFile 'sonar.objectivec.project'

# Count projects
projectCount=$(echo $projectFile | sed -n 1'p' | tr ',' '\n' | wc -l | tr -d '[[:space:]]')
if [ "$vflag" = "on" ]; then
    echo "Project count is [$projectCount]"
fi

# Source directories for .h/.m files
srcDirs=''; readParameter srcDirs 'sonar.sources'
# The name of your application scheme in Xcode
appScheme=''; readParameter appScheme 'sonar.objectivec.appScheme'

# The name of your test scheme in Xcode
testScheme=''; readParameter testScheme 'sonar.objectivec.testScheme'
# The file patterns to exclude from coverage report
excludedPathsFromCoverage=''; readParameter excludedPathsFromCoverage 'sonar.objectivec.excludedPathsFromCoverage'
# Read coverage type
coverageType=''; readParameter coverageType 'sonar.objectivec.coverageType'


# Check for mandatory parameters
if [ -z "$projectFile" -o "$projectFile" = " " ]; then

    if [ ! -z "$workspaceFile" -a "$workspaceFile" != " " ]; then
        echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties. You must specify which projects (comma-separated list) are application code within the workspace $workspaceFile."
    else
        echo >&2 "ERROR - sonar.objectivec.project parameter is missing in sonar-project.properties (name of your .xcodeproj)"
    fi
    exit 1
fi
if [ -z "$srcDirs" -o "$srcDirs" = " " ]; then
    echo >&2 "ERROR - sonar.sources parameter is missing in sonar-project.properties. You must specify which directories contain your .h/.m source files (comma-separated list)."
    exit 1
fi
if [ -z "$appScheme" -o "$appScheme" = " " ]; then
    echo >&2 "ERROR - sonar.objectivec.appScheme parameter is missing in sonar-project.properties. You must specify which scheme is used to build your application."
    exit 1
fi
if [ -z "$destinationSimulator" -o "$destinationSimulator" = " " ]; then
    echo >&2 "ERROR - sonar.objectivec.simulator parameter is missing in sonar-project.properties. You must specify which simulator to use."
    exit 1
fi

if [ "$vflag" = "on" ]; then
    echo "Xcode workspace file is: $workspaceFile"
    echo "Xcode project file is: $projectFile"
    echo "Xcode application scheme is: $appScheme"
    echo "Xcode test scheme is: $testScheme"
    echo "Excluded paths from coverage are: $excludedPathsFromCoverage"
fi

## SCRIPT

# Start progress indicator in the background
if [ "$vflag" = "" -a "$nflag" = "" ]; then
    startProgress &
    # Save PID
    PROGRESS_PID=$!
fi

# Create sonar-reports/ for reports output
if [ "$vflag" = "on" ]; then
    echo 'Creating directory sonar-reports/'
fi
rm -rf sonar-reports
mkdir sonar-reports

# Extracting project information needed later
echo -n 'Extracting Xcode project information'
if [[ "$workspaceFile" != "" ]] ; then
    buildCmdPrefix="-workspace $workspaceFile"
else
    buildCmdPrefix="-project $projectFile"
fi
buildCmd=($XCODEBUILD_CMD clean build $buildCmdPrefix -scheme $appScheme)
if [[ ! -z "$destinationSimulator" ]]; then
    buildCmd+=(-destination "$destinationSimulator" -destination-timeout 360)
fi
runCommand  xcodebuild.log "${buildCmd[@]}"
#oclint-xcodebuild # Transform the xcodebuild.log file into a compile_command.json file
cat xcodebuild.log | $XCPRETTY_CMD -r json-compilation-database -o compile_commands.json

# Unit tests and coverage
if [ "$testScheme" = "" ] || [ "$unittests" = "" ]; then
    echo 'Skipping tests!'

    # Put default xml files with no tests and no coverage...
    echo "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><testsuites name='AllTestUnits'></testsuites>" > sonar-reports/TEST-report.xml
    echo "<?xml version='1.0' ?><!DOCTYPE coverage SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'><coverage><sources></sources><packages></packages></coverage>" > sonar-reports/coverage.xml
else

    echo -n 'Running tests'

    if [ "$coverageType" = "profdata" -o "$coverageType" = "" ]; then
        # profdata
        buildCmd=($XCODEBUILD_CMD test $buildCmdPrefix -scheme "$testScheme" -configuration Debug -enableCodeCoverage YES)
        xcode8BuildForTestingCmd=($XCODEBUILD_CMD build-for-testing $buildCmdPrefix -scheme "$testScheme" -configuration Debug -enableCodeCoverage YES)
        xcode8TestCmd=($XCODEBUILD_CMD test-without-building $buildCmdPrefix -scheme "$testScheme" -configuration Debug -enableCodeCoverage YES)
    else
        # Legacy coverage
        buildCmd=($XCODEBUILD_CMD test $buildCmdPrefix -scheme "$testScheme" -configuration Debug)
        xcode8BuildForTestingCmd=($XCODEBUILD_CMD build-for-testing $buildCmdPrefix -scheme "$testScheme" -configuration Debug)
        xcode8TestCmd=($XCODEBUILD_CMD test-without-building $buildCmdPrefix -scheme "$testScheme" -configuration Debug)
    fi

    if [[ ! -z "$destinationSimulator" ]]; then
        buildCmd+=(-destination "$destinationSimulator" -destination-timeout 360)
        xcode8BuildForTestingCmd+=(-destination "$destinationSimulator" -destination-timeout 360)
        xcode8TestCmd+=(-destination "$destinationSimulator" -destination-timeout 360)
    fi
    
    if testIsXcodeMinMajorVersionAvailable 8 ; then
        echo "Running build-for-testing"
        "${xcode8BuildForTestingCmd[@]}"  | $XCPRETTY_CMD
        echo "Running test-without-building"
        "${xcode8TestCmd[@]}"  | $XCPRETTY_CMD -t --report junit
    else
        echo "Testing"
        "${buildCmd[@]}"  | $XCPRETTY_CMD -t --report junit
    fi
    
    mv build/reports/junit.xml sonar-reports/TEST-report.xml

    echo -n 'Computing coverage report'

    if [ "$coverageType" = "profdata" -o "$coverageType" = "" ]; then

        # profdata = use slather

        echo 'Using profdata'

        # Build the --exclude flags
        excludedCommandLineFlags=""
        if [ ! -z "$excludedPathsFromCoverage" -a "$excludedPathsFromCoverage" != " " ]; then
            echo $excludedPathsFromCoverage | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh2
            while read word; do
                excludedCommandLineFlags+=" -i $word"
            done < tmpFileRunSonarSh2
            rm -rf tmpFileRunSonarSh2
        fi
        if [ "$vflag" = "on" ]; then
            echo "Command line exclusion flags for slather is:$excludedCommandLineFlags"
        fi


        projectArray=(${projectFile//,/ })
        firstProject=${projectArray[0]}

        slatherCmd=($SLATHER_CMD coverage --input-format profdata $excludedCommandLineFlags --cobertura-xml --output-directory sonar-reports)
        if [[ ! -z "$workspaceFile" ]]; then
            slatherCmd+=( --workspace $workspaceFile)
        fi
        slatherCmd+=( --scheme "$appScheme" $firstProject)

        runCommand /dev/stdout "${slatherCmd[@]}"
        mv sonar-reports/cobertura.xml sonar-reports/coverage.xml

    else

        # Legacy mode = use gcovr

        # Build the --exclude flags
        excludedCommandLineFlags=""
        if [ ! -z "$excludedPathsFromCoverage" -a "$excludedPathsFromCoverage" != " " ]; then
            echo $excludedPathsFromCoverage | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh2
            while read word; do
                excludedCommandLineFlags+=" --exclude $word"
            done < tmpFileRunSonarSh2
            rm -rf tmpFileRunSonarSh2
        fi
        if [ "$vflag" = "on" ]; then
            echo "Command line exclusion flags for gcovr is:$excludedCommandLineFlags"
        fi

        # Create symlink on the build directory to enable its access from the workspace
        coverageFilesPath=$(grep 'command' compile_commands.json | sed 's#^.*-o \\/#\/#;s#",##' | grep "${projectName%%.*}.build" | awk 'NR<2' | sed 's/\\\//\//g' | sed 's/\\\\//g' | xargs -0 dirname)
        splitIndex=$(awk -v a="$coverageFilesPath" -v b="/Intermediates" 'BEGIN{print index(a,b)}')
        coverageFilesPath=$(echo ${coverageFilesPath:0:$splitIndex}Intermediates)
        ln -s $coverageFilesPath sonar-reports/build

        # Run gcovr with the right options
        runCommand "sonar-reports/coverage.xml" gcovr -r . $excludedCommandLineFlags --xml

    fi


fi

if [ "$oclint" = "on" ]; then

    # OCLint
    echo -n 'Running OCLint...'

    # Options
    maxPriority=9999
    longLineThreshold=250

    # Build the --include flags
    currentDirectory=${PWD##*/}
    echo "$srcDirs" | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
    while read word; do

        includedCommandLineFlags=" --include .*/${currentDirectory}/${word}"
        if [ "$vflag" = "on" ]; then
            echo
            echo -n "Path included in oclint analysis is:$includedCommandLineFlags"
        fi
        # Run OCLint with the right set of compiler options
        # runCommand no oclint-json-compilation-database -- -report-type pmd -o oclint.xml -max-priority-1 100000  -max-priority-2 100000  -max-priority-3 100000 
        #oclint-json-compilation-database -v $includedCommandLineFlags -- -rc LONG_LINE=$longLineThreshold -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority -report-type pmd -o sonar-reports/$(echo $word | sed 's/\//_/g')-oclint.xml
        oclint-json-compilation-database -- -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority -rc LONG_LINE=150 -report-type pmd -o oclint.xml
    done < tmpFileRunSonarSh
    rm -rf tmpFileRunSonarSh


else
    echo 'Skipping OCLint (test purposes only!)'
fi

if [ "$fauxpas" = "on" ]; then
    hash fauxpas 2>/dev/null
    if [ $? -eq 0 ]; then

        #FauxPas
        echo -n 'Running FauxPas...'

        if [ "$projectCount" = "1" ]
        then

            fauxpas -o json check $projectFile --workspace $workspaceFile --scheme $appScheme > sonar-reports/fauxpas.json


        else

            echo $projectFile | sed -n 1'p' | tr ',' '\n' > tmpFileRunSonarSh
            while read projectName; do

                $XCODEBUILD_CMD -list -project $projectName | sed -n '/Schemes/,$p' | while read scheme
                do

                if [ "$scheme" = "" ]
                then
                exit
                fi

                if [ "$scheme" == "${scheme/Schemes/}" ]
                then
                    if [ "$scheme" != "$testScheme" ]
                    then
                        projectBaseDir=$(dirname $projectName)
                        workspaceRelativePath=$(python -c "import os.path; print os.path.relpath('$workspaceFile', '$projectBaseDir')")
                        fauxpas -o json check $projectName --workspace $workspaceRelativePath --scheme $scheme > sonar-reports/$(basename $projectName .xcodeproj)-$scheme-fauxpas.json
                    fi
                fi

                done

            done < tmpFileRunSonarSh
            rm -rf tmpFileRunSonarSh

        fi

    else
        echo 'Skipping FauxPas (not installed)'
    fi
else
    echo 'Skipping FauxPas'
fi

# Lizard Complexity
if [ "$lizard" = "on" ]; then
    if hash $LIZARD_CMD 2>/dev/null; then
        echo -n 'Running Lizard...'
        $LIZARD_CMD --xml "$srcDirs" > sonar-reports/lizard-report.xml
    else
        echo 'Skipping Lizard (not installed!)'
    fi
else
    echo 'Skipping Lizard (test purposes only!)'
fi

# SonarQube
if [ "$sonarscanner" = "on" ]; then
    echo -n 'Running SonarQube using SonarQube Scanner'
    if hash /dev/stdout sonar-scanner 2>/dev/null; then
        runCommand /dev/stdout sonar-scanner
    else
        echo 'Skipping sonar-scanner (not installed!)'
    fi
else
    echo -n 'Running SonarQube using SonarQube Runner'
    if hash /dev/stdout sonar-runner 2>/dev/null; then
        runCommand /dev/stdout sonar-runner 
    else
        runCommand /dev/stdout sonar-scanner
    fi
fi

# Kill progress indicator
stopProgress

exit 0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342