如何正确配置属性“sonar.java.binaries”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39290983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly configure the property "sonar.java.binaries"?
提问by Mark
We are using SonarQube 5.1.2 using Ant runner 2.2 and Java pluging 3.12 for the analysis. I can succesfully analyse my project. I just keep getting this error:
我们使用 SonarQube 5.1.2,使用 Ant runner 2.2 和 Java pluging 3.12 进行分析。我可以成功地分析我的项目。我只是不断收到此错误:
Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@d678716, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@58e28efd, CycleBetweenPackages rule are disabled.
So I need to configure my sonar.java.binaries and sonar.java.test.binaries properties (following http://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode).
所以我需要配置我的 sonar.java.binaries 和 sonar.java.test.binaries 属性(遵循http://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode)。
Which I think I have done correctly:
我认为我做得对:
<property name="project.dir" value="${basedir}/xalg.prj/h3_service_fo" />
<property name="sonar.java.binaries" location="${project.build.dir}/classes/main" />
<property name="sonar.java.test.binaries" value="${project.build.dir}/classes/test" />
Which resolve to the following valid directories for the above properties:
其中解析为上述属性的以下有效目录:
basedir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj
project.dir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo
sonar.java.binaries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
sonar.java.test.binaries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/build/classes/test
But I keep getting:
但我不断得到:
Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@d678716, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@58e28efd, CycleBetweenPackages rule are disabled.
And for the life of me, I can not figure out what values I need to give the sonar.java.binaries and sonar.java.test.binaries properties. I even tried using sonar.binaries, which gave me the following output:
对于我的生活,我无法弄清楚我需要为 sonar.java.binaries 和 sonar.java.test.binaries 属性提供什么值。我什至尝试使用 sonar.binaries,它给了我以下输出:
Binary dirs: xalg.prj/h3_service_fo/build/classes
Which I did not get using either sonar.java.binaries or sonar.java.test.binaries. I also got:
我没有使用 sonar.java.binaries 或 sonar.java.test.binaries。我也得到了:
JavaClasspath initialization...
sonar.binaries and sonar.libraries are deprecated since version 2.5 of sonar-java-plugin, please use sonar.java.binaries and sonar.java.libraries instead
Which is to be expected for a deprecated property. But using the sonar.java.binaries property I did not get the "Binary dirs" line in my log.
对于已弃用的属性,这是可以预期的。但是使用 sonar.java.binaries 属性,我没有在日志中看到“Binary dirs”行。
Using sonar.java.binaries:
使用 sonar.java.binaries:
Language is forced to java
Load rules
Load rules (done) | time=761ms
Code colorizer, supported languages: cs,plsql
Initializers :
Base dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj
Working dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
Source paths: xalg.prj/h3_service_fo/src/main/java
Test paths: xalg.prj/h3_service_fo/src/test/java
Source encoding: windows-1252, default locale: en_US
Index files
Versus using sonar.binaries:
对比使用 sonar.binaries:
Language is forced to java
Load rules
Load rules (done) | time=736ms
Code colorizer, supported languages: cs,plsql
Initializers :
Base dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj
Working dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
Source paths: xalg.prj/h3_service_fo/src/main/java
Test paths: xalg.prj/h3_service_fo/src/test/java
Binary dirs: xalg.prj/h3_service_fo/build/classes
Source encoding: windows-1252, default locale: en_US
Index files
I also looked into the source code of SonarQube, SonarQube Java Plugin and the SonarQube Scanner for instances of either "Java bytecode has not been made available to the analyzer." or sonar.java.binaries. I found plenty on sonar.java.binaries, but nothing on "Java bytecode has not been made available to the analyzer." So I have no clue what conditions exactly trigger that error.
我还查看了 SonarQube、SonarQube Java Plugin 和 SonarQube Scanner 的源代码,以查找“Java 字节码尚未提供给分析器”的实例。或 sonar.java.binaries。我在 sonar.java.binaries 上找到了很多,但没有关于“Java 字节码尚未提供给分析器”的内容。所以我不知道什么条件会触发该错误。
I also tried the following permutations on sonar.java.binaries:
我还在 sonar.java.binaries 上尝试了以下排列:
<property name="sonar.java.binaries" location="${project.build.dir}/classes" />
<property name="sonar.java.binaries" location="${project.build.dir}/classes/main/nl" />
But that did nothing either.
但这也没有任何作用。
What is weird is that Squid seems to resolve the classpath just fine:
奇怪的是,Squid 似乎很好地解析了类路径:
----- Classpath analyzed by Squid:
D:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
So, what am I missing? What am I doing wrong? Thanks in advance.
那么,我错过了什么?我究竟做错了什么?提前致谢。
Update 2016-09-08:
Removed the entire log, the post become to long.
2016-09-08 更新:
删除了整个日志,帖子变得很长。
A subset with the (I think) relevant paths:
具有(我认为)相关路径的子集:
project.build.dir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/build
project.dir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo
project.src.dir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/src
sonar.dir=D\:/appl/sonarqube-5.1.2
sonar.working.directory=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
sonar.projectBaseDir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj
sonar.jacoco.reportPath=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/build/jacoco/test.exec
sonar.junit.reportsPath=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/build/test-results
sonar.sources=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/src/main/java
sonar.java.binaries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
sonar.java.libraries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_deploy/*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_repos/lib/*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_repos/provided/*.jar
sonar.tests=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/src/test/java
sonar.java.test.binaries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/xalg.prj/h3_service_fo/build/classes/test
sonar.java.test.libraries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_deploy/*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_repos/lib/*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj/_repos/provided/*.jar
The paths have exactly the same format as in my post. Could it be that the Sonar Ant runner can't figure out a path with both backslashes and slashes?
路径的格式与我的帖子中的格式完全相同。会不会是 Sonar Ant runner 无法找出一条同时包含反斜杠和斜杠的路径?
Update 2016-09-16:
Removed the entire log, the post become to long.
2016 年 9 月 16 日更新:
删除了整个日志,帖子变得很长。
A subset with the (I think) relevant paths:
具有(我认为)相关路径的子集:
project.build.dir=xalg.prj\\h3_service_fo\\build
project.dir=xalg.prj\\h3_service_fo
project.src.dir=xalg.prj\\h3_service_fo\\src
sonar.dir=D\:\\appl\\sonarqube-5.1.2
sonar.working.directory=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
sonar.projectBaseDir=D\:\appl\BuildAgent\work\H3\src.prj\java.prj
sonar.jacoco.reportPath=xalg.prj\\h3_service_fo\\build\\jacoco/test.exec
sonar.junit.reportsPath=xalg.prj\\h3_service_fo\\build\\test-results
sonar.sources=xalg.prj\\h3_service_fo\\src\\main\\java
sonar.java.binaries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
sonar.java.libraries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_deploy\\*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_repos\\lib\\*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_repos\\provided\\*.jar
sonar.tests=xalg.prj\\h3_service_fo\\src\\test\\java
sonar.java.test.binaries=xalg.prj\\h3_service_fo\\build\\classes\\test
sonar.java.test.libraries=D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_deploy\\*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_repos\\lib\\*.jar,D\:\appl\BuildAgent\work\H3\src.prj\java.prj\\_repos\\provided\\*.jar
Some paths have become relative, but I think that is because TeamCity changed the Ant file to the file in SVN. The sonar.java.binaries is absolute and it definitelypoints to the correct directory.
有些路径变成了相对路径,但我认为这是因为 TeamCity 将 Ant 文件更改为 SVN 中的文件。sonar.java.binaries 是绝对的,它绝对指向正确的目录。
But I still get this error:
但我仍然收到此错误:
09:17:52.299 INFO - Java Main Files AST scan done: 1579 ms
09:17:52.301 INFO - 2/2 source files have been analyzed
09:17:52.305 WARN - Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@757a48f9, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@1adf492b, CycleBetweenPackages rule are disabled.
The classpath is still interpreted just fine:
类路径仍然可以很好地解释:
[sonar:sonar] 09:17:51.971 DEBUG - ----- Classpath analyzed by Squid:
[sonar:sonar] 09:17:51.972 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
[sonar:sonar] 09:17:51.973 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\batch.daemon.jar
[sonar:sonar] 09:17:51.974 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\buildinfo.jar
[sonar:sonar] 09:17:51.975 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h2_shared.jar
[sonar:sonar] 09:17:51.975 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_generator.jar
[sonar:sonar] 09:17:51.976 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_loadtest.jar
[sonar:sonar] 09:17:51.977 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_common.jar
[sonar:sonar] 09:17:51.977 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg.jar
[sonar:sonar] 09:17:51.978 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_dao.jar
[sonar:sonar] 09:17:51.979 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_mappers.jar
[sonar:sonar] 09:17:51.979 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_procedures.jar
[sonar:sonar] 09:17:51.980 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare.jar
[sonar:sonar] 09:17:51.981 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_dao.jar
[sonar:sonar] 09:17:51.982 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_mappers.jar
[sonar:sonar] 09:17:51.982 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_procedures.jar
Could the Sonar Ant runner have a problem with the escaped back slashes?
Sonar Ant runner 是否对转义的反斜杠有问题?
回答by David Hunsicker
This is what I use, and it works.
这是我使用的,并且有效。
sonar-scanner -Dsonar.projectKey=projectName
-Dsonar.gitlab.commit_sha=$CI_BUILD_REF
-Dsonar.gitlab.ref_name=$CI_BUILD_REF_NAME -Dsonar.sources=directory\src\
-Dsonar.java.binaries=.build\libs\
You need to point sonar.java.binaries to a directory that contains jars. I would't try to get fancy with wildcards and file extensions, that's not the same thing. You need a directory as an argument, not a regular expression for files.
您需要将 sonar.java.binaries 指向包含 jar 的目录。我不会尝试使用通配符和文件扩展名,这不是一回事。您需要一个目录作为参数,而不是文件的正则表达式。
I don't know everything about sonar scanner, but my configuration works.
我对声纳扫描仪一无所知,但我的配置有效。
Take my example into context, I use it in a gitlab runner pipeline. Your double backslashes should be fine, but you can always substitute them for forwards slashes in your config files.
将我的示例放在上下文中,我在 gitlab runner 管道中使用它。您的双反斜杠应该没问题,但您始终可以将它们替换为配置文件中的正斜杠。
回答by Martin Frank
you have to provide the parameter sonar.java.libraries
.
你必须提供参数sonar.java.libraries
。
if you use mavenyou can add these inside the pom.xml
as a property
如果您使用maven,您可以将这些添加pom.xml
到属性中
<project>
...
<properties>
...
<sonar.java.libraries>target</sonar.java.libraries>
</properties>
</project>
or as run parameter -Dsonar.java.libraries=target
或作为运行参数 -Dsonar.java.libraries=target
回答by elem103
Based on the documentation at https://docs.sonarqube.org/latest/analysis/languages/java/
基于https://docs.sonarqube.org/latest/analysis/languages/java/上的文档
The purpose of sonar.java.binaries
path is:
sonar.java.binaries
路径的目的是:
Comma-separated paths to directories containing the compiled bytecode files corresponding to your source files.
包含与源文件对应的已编译字节码文件的目录的逗号分隔路径。
For Gradle, the default path is at build/classes
, so I would set:
对于 Gradle,默认路径是 at build/classes
,所以我会设置:
sonar.java.binaries=build/classes