sonarqube scaner docker 扫描失败的原因及解决方案

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网络跟sonarqube server不在一个网络内
解决:--network=host 参数

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 必选参数
解决:使用mvn compile编译源码,并在sonar-project.properties中添加参数

sonar.java.binaries=target/classes

其他语言请参考https://docs.sonarqube.org/latest/analysis/languages/overview/ 获取必选参数信息。

3. Scanner 出现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默认配置太小,内存不足
解决:设置环境变量 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出现ES 137故障,自动退出。

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 内存不足, 因为ES默认的heap size为2GB,而Docker的limit也是2GB。
解决:加大内存,docker Desktop端(Mac 与windows端)需要配置docker的最大内存超过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挂在目录时不支持相对路径。
解决:将docker run -v...的运行脚本也放到工程根目录下,将当前目录挂载到/usr/src:

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