macOS sonar-scanner環(huán)境搭建-docker版

docker安裝及啟動(dòng)此處不做贅述豪嚎,本文使用 sonarqube:8.9.1-community 版本docker鏡像

鏡像拉取及啟動(dòng)

  • 鏡像拉取
docker pull sonarqube:8.9.1-community

如遇數(shù)次拉取失敗,可修改docker鏡像源為"https://docker.mirrors.ustc.edu.cn"或"https://hub-mirror.c.163.com",具體設(shè)置如下圖所示

image.png
  • 鏡像啟動(dòng)
docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:8.9.1-community

啟動(dòng)完成

實(shí)例啟動(dòng)并運(yùn)行后,使用系統(tǒng)管理員憑據(jù)登錄到http://localhost:9000

  • login: admin
  • password: admin

掃描失敗的原因及解決方案

1. Failed to connect to localhost/127.0.0.1:9000
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
org.sonarsource.scanner.api.internal.ScannerException: Unable to execute SonarScanner analysis
    at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:85)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.createLauncher(IsolatedLauncherFactory.java:74)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.createLauncher(IsolatedLauncherFactory.java:70)
    at org.sonarsource.scanner.api.EmbeddedScanner.doStart(EmbeddedScanner.java:185)
    at org.sonarsource.scanner.api.EmbeddedScanner.start(EmbeddedScanner.java:123)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:73)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalStateException: Fail to get bootstrap index from server
    at org.sonarsource.scanner.api.internal.BootstrapIndexDownloader.getIndex(BootstrapIndexDownloader.java:42)
    at org.sonarsource.scanner.api.internal.JarDownloader.getScannerEngineFiles(JarDownloader.java:58)
    at org.sonarsource.scanner.api.internal.JarDownloader.download(JarDownloader.java:53)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:76)
    ... 7 more
Caused by: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:9000
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connectSocket(RealConnection.java:265)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connect(RealConnection.java:183)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.Transmitter.newExchange(Transmitter.java:169)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.RealCall.execute(RealCall.java:81)
    at org.sonarsource.scanner.api.internal.ServerConnection.callUrl(ServerConnection.java:114)
    at org.sonarsource.scanner.api.internal.ServerConnection.downloadString(ServerConnection.java:99)
    at org.sonarsource.scanner.api.internal.BootstrapIndexDownloader.getIndex(BootstrapIndexDownloader.java:39)
    ... 10 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
    at java.base/java.net.Socket.connect(Unknown Source)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.platform.Platform.connectSocket(Platform.java:130)
    at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connectSocket(RealConnection.java:263)
    ... 31 more

根源:scanner的container網(wǎng)絡(luò)跟sonarqube server不在一個(gè)網(wǎng)絡(luò)內(nèi)
解決:--network=host 參數(shù)

https://stackoverflow.com/questions/32097414/error-sonar-server-http-localhost9000-can-not-be-reached

2. please provide compiled classes with sonar.java.binaries property,
ERROR: Error during SonarScanner execution
org.sonar.java.AnalysisException: Your project contains .java files, please provide compiled classes with sonar.java.binaries property, or exclude them from the analysis with sonar.exclusions property.
    at org.sonar.java.JavaClasspath.init(JavaClasspath.java:64)
    at org.sonar.java.AbstractJavaClasspath.getElements(AbstractJavaClasspath.java:280)
    at org.sonar.java.SonarComponents.getJavaClasspath(SonarComponents.java:149)
    at org.sonar.java.JavaSquid.<init>(JavaSquid.java:75)
    at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:102)
    at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:393)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:389)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:358)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
    at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:144)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
    at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
    at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
    at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
    at com.sun.proxy.$Proxy0.execute(Unknown Source)
    at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
    at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)

根源: java中缺少sonar.java.binaries 必選參數(shù)
解決:使用mvn compile編譯源碼,并在sonar-project.properties中添加參數(shù)

sonar.java.binaries=target/classes

其他語言請(qǐng)參考https://docs.sonarqube.org/latest/analysis/languages/overview/ 獲取必選參數(shù)信息。

3. Scanner 出現(xiàn)java.lang.OutOfMemoryError: Java heap space
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution
java.lang.OutOfMemoryError: Java heap space
    at org.sonarsource.analyzer.commons.xml.XmlFilePosition.shift(XmlFilePosition.java:83)
    at org.sonarsource.analyzer.commons.xml.XmlFilePosition.shift(XmlFilePosition.java:62)
    at org.sonarsource.analyzer.commons.xml.XmlFilePosition.moveAfterClosingBracket(XmlFilePosition.java:109)
    at org.sonarsource.analyzer.commons.xml.XmlParser.visitEndElement(XmlParser.java:198)
    at org.sonarsource.analyzer.commons.xml.XmlParser.parseXml(XmlParser.java:133)
    at org.sonarsource.analyzer.commons.xml.XmlParser.<init>(XmlParser.java:68)
    at org.sonarsource.analyzer.commons.xml.XmlFile.getNamespaceUnawareDocument(XmlFile.java:129)
    at org.sonar.java.checks.xml.struts.FormNameDuplicationCheck.scanFile(FormNameDuplicationCheck.java:41)
    at org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck.scanFile(SonarXmlCheck.java:44)
    at org.sonar.plugins.java.XmlFileSensor.scanFile(XmlFileSensor.java:121)
    at org.sonar.plugins.java.XmlFileSensor.lambda$scanFile$2(XmlFileSensor.java:114)
    at org.sonar.plugins.java.XmlFileSensor$$Lambda$1477/0x0000000100aba840.accept(Unknown Source)
    at java.base/java.util.HashMap$Values.forEach(Unknown Source)
    at org.sonar.plugins.java.XmlFileSensor.scanFile(XmlFileSensor.java:112)
    at org.sonar.plugins.java.XmlFileSensor.execute(XmlFileSensor.java:86)
    at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor$$Lambda$344/0x0000000100657440.run(Unknown Source)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:77)
    at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:59)
    at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
    at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:393)
    at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:389)
    at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:358)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
    at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:144)
    at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
    at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)

根源: jvm默認(rèn)配置太小待笑,內(nèi)存不足
解決:設(shè)置環(huán)境變量 SONAR_SCANNER_OPTS="-Xmx512m" 或者更大。

docker的解決方案為

docker run --network=host    --rm     -e SONAR_HOST_URL="http://localhost:9000" -e SONAR_SCANNER_OPTS="-Xmx1024m"     -v "/Documents/github/security/Benchmark:/usr/src"     sonarsource/sonar-scanner-cli
4. sonarqube server出現(xiàn)ES 137故障抓谴,自動(dòng)退出暮蹂。
onarqube_1  | 2020.12.09 07:38:00 INFO  ce[][o.s.c.t.CeWorkerImpl] worker AXZGb3aOoJQO0GPdHzsd found no pending task (including indexation task). Disabling indexation task lookup for this worker until next SonarQube restart.
sonarqube_1  | 2020.12.09 07:49:33 INFO  es[][o.e.m.j.JvmGcMonitorService] [gc][762] overhead, spent [281ms] collecting in the last [1s]
sonarqube_1  | 2020.12.09 09:31:15 WARN  app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 137
sonarqube_1  | 2020.12.09 09:31:15 INFO  app[][o.s.a.SchedulerImpl] Process[es] is stopped
sonarqube_1  | 2020.12.09 09:31:16 INFO  ce[][o.s.p.ProcessEntryPoint] Hard stopping process
sonarqube_1  | 2020.12.09 09:31:16 INFO  ce[][o.s.ce.app.CeServer] Compute Engine is stopping...
sonarqube_1  | 2020.12.09 09:31:16 INFO  ce[][o.s.c.t.CeProcessingSchedulerImpl] Hard stopping workers...
sonarqube_1  | 2020.12.09 09:31:16 INFO  ce[][o.s.ce.app.CeServer] Compute Engine is stopped
sonarqube_1  | 2020.12.09 09:31:16 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is stopped
sonarqube_1  | 2020.12.09 09:31:16 INFO  web[][o.s.p.ProcessEntryPoint] Hard stopping process
sonarqube_1  | 2020.12.09 09:31:16 INFO  web[][o.s.s.n.NotificationDaemon] Notification service stopped
sonarqube_1  | 2020.12.09 09:31:17 INFO  app[][o.s.a.SchedulerImpl] Process[web] is stopped
sonarqube_1  | 2020.12.09 09:31:17 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped

根源:server 端ES 內(nèi)存不足, 因?yàn)镋S默認(rèn)的heap size為2GB,而Docker的limit也是2GB癌压。
解決:加大內(nèi)存仰泻,docker Desktop端(Mac 與windows端)需要配置docker的最大內(nèi)存超過4GB√步欤或者限制ES的Heap size到750MB集侯。
https://github.com/10up/wp-local-docker/issues/6#issue-211554733

5. # Error during SonarQube Scanner execution ERROR: You must define the following mandatory properties for ‘Unknown’: sonar.projectKey

根源:scanner無法在工程的根目錄上找到sonar-project.properties文件,盡管該文件存在帜消,但是docker -v掛在目錄時(shí)不支持相對(duì)路徑棠枉。
解決:將docker run -v...的運(yùn)行腳本也放到工程根目錄下,將當(dāng)前目錄掛載到/usr/src:

docker run ...
    -v "$(pwd):/usr/src" \
    sonarsource/sonar-scanner-cli

掃描失敗的原因及解決方案轉(zhuǎn)載于:http://www.reibang.com/p/a388252af433

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末泡挺,一起剝皮案震驚了整個(gè)濱河市辈讶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌娄猫,老刑警劉巖荞估,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異稚新,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)跪腹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門褂删,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人冲茸,你說我怎么就攤上這事屯阀∶辶保” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵难衰,是天一觀的道長(zhǎng)钦无。 經(jīng)常有香客問我,道長(zhǎng)盖袭,這世上最難降的妖魔是什么失暂? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮鳄虱,結(jié)果婚禮上弟塞,老公的妹妹穿的比我還像新娘。我一直安慰自己拙已,他們只是感情好决记,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著倍踪,像睡著了一般系宫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上建车,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天扩借,我揣著相機(jī)與錄音,去河邊找鬼癞志。 笑死往枷,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的凄杯。 我是一名探鬼主播错洁,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼戒突!你這毒婦竟也來了屯碴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤膊存,失蹤者是張志新(化名)和其女友劉穎导而,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體隔崎,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡今艺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了爵卒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虚缎。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖钓株,靈堂內(nèi)的尸體忽然破棺而出实牡,到底是詐尸還是另有隱情陌僵,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布创坞,位于F島的核電站碗短,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏题涨。R本人自食惡果不足惜偎谁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望携栋。 院中可真熱鬧搭盾,春花似錦、人聲如沸婉支。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽向挖。三九已至蝌以,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間何之,已是汗流浹背跟畅。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留溶推,地道東北人徊件。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蒜危,于是被迫代替她去往敵國(guó)和親虱痕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容