当前位置: 首页 > 面试题库 >

声纳使用JENKINS分析Maven 3和多语言项目

逄皓轩
2023-03-14
问题内容

这是在使用 JENKINS 构建的 Maven 3 Java JS 项目中使用 Sonar 时发现的问题/问题。

为了分析我的项目,我选择了两种不同的方式,但是两种方式均不起作用。

第一种方法 :在JENKINS中将Sonar作为独立任务启动,作为构建后的动作

*从 *JENKINS 安装的 Sonar插件* v2.1 *

*从 *JENKINS 安装的 Sonar Runner* v2.3 *

在我的Maven项目的后期构建部分中,我将Launch Sonar 选中为独立任务,并设置以下属性:

# Required metadata
sonar.projectKey=***
sonar.projectName=***
sonar.projectVersion=1.0
sonar.java.source=1.7

# Path to the parent source code directory.
sonar.sources=src/main

#Path to the directories to exclude
sonar.exclusions=src/main/webapp/ui/ext/**,src/main/webapp/ui/build/**,src/main/webapp/ui/admin/sass/**,src/main/webapp/ui/user/sass/**,src/main/ressources/**

#Path to classes directory
sonar.binaries=target/classes

#Path to Junit test reports and test files
sonar.junit.reportsPath=target/surefire-reports
sonar.tests=src/test
sonar.dynamicAnalysis=reuseReports

#Jacoco coverage report
sonar.jacoco.reportPath=target/jacoco.exec
sonar.java.coveragePlugin=jacoco

# Encoding of the source code
sonar.sourceEncoding=UTF-8

通过这种方式,我可以根据需要分析 JAVAJS。 但是, SONAR 不会加载 Maven 依赖项,
因为它们应该被加载。这是 SONAR 分析时的日志:

09:23:55.605 INFO  - Java bytecode scan...
09:23:55.646 WARN  - Class 'org/springframework/context/ApplicationContext' is not    accessible through the ClassLoader.
09:23:55.656 WARN  - Class 'com/mongodb/MongoClient' is not accessible through the ClassLoader.
09:23:55.657 WARN  - Class 'com/mongodb/DB' is not accessible through the ClassLoader.
09:23:55.661 WARN  - Class 'org/springframework/context/ApplicationContext' is not    accessible through the ClassLoader.
09:23:55.662 WARN  - Class 'org/springframework/context/ApplicationContext' is not accessible through the ClassLoader.
...
09:23:55.749 WARN  - Class 'org/apache/log4j/Logger' is not accessible through the ClassLoader.
09:23:55.773 INFO  - Java bytecode scan done: 168 m

我可以在 Sonar 中看到代码覆盖率…并且分析是 成功的, 但是我无法在构建中保留这些警告。

所以我做了一个 Sonar 使用 maven的 分析。

第二种方式:使用Maven进行Soanar

*从 *JENKINS 安装的 Maven* 插件v3.0.4 *

*从 *JENKINS 安装的 Sonar* 插件v2.1 *

在构建后部分中,我选择Sonar并设置以下属性:

 -Dsonar.projectKey=***
 -Dsonar.projectName=***
 -Dsonar.projectVersion=1.0
 -Dsonar.java.source=1.7

 -Dsonar.sources=src/main

-Dsonar.exclusions=src/main/webapp/ui/ext/**,src/main/webapp/ui/build/**,src/main/webapp/ui/admin/sass/**,src/main/webapp/ui/user/sass/**,src/main/ressources/**

-Dsonar.binaries=target/classes

-Dsonar.junit.reportsPath=target/surefire-reports
-Dsonar.tests=src/test
-Dsonar.dynamicAnalysis=reuseReports

-Dsonar.jacoco.reportPath=target/jacoco.exec
-Dsonar.java.coveragePlugin=jacoco

-Dsonar.sourceEncoding=UTF-8

使用这种方式,我不再有类加载器问题,并且所有依赖项都已加载。

但是 Sonar 仅分析 JAVA 代码,即使我定义了sonar.sources=src/main他也仅分析 JAVA

这是日志:

[INFO] --- sonar-maven-plugin:2.2:sonar (default-cli) @ WebDark ---
[INFO] SonarQube version: 4.2
INFO: Default locale: "fr_FR", source code encoding: "UTF-8"
....
[INFO] [15:20:59.054] Base dir: /**/**/jenkins/jobs/**/workspace
[INFO] [15:20:59.054] Working dir: /**/**/jenkins/jobs/**/workspace/target/sonar
[INFO] [15:20:59.054] Source dirs: /**/**/jenkins/jobs/**/workspace/src/main/java
[INFO] [15:20:59.054] Test dirs: /**/**/jenkins/jobs/**/workspace/src/test/java
[INFO] [15:20:59.054] Binary dirs: /**/**/jenkins/jobs/WebDark/workspace/target/classes
[INFO] [15:20:59.055] Source encoding: UTF-8, default locale: fr_FR
[INFO] [15:20:59.055] Index files
[INFO] [15:20:59.083] Excluded sources:
...
...

再次,分析是 成功的, 但是 SONAR 不分析 JS 。正如您在日志中看到的那样,即使我之前Source dirs设置了,它也被设置为。/**/**/jenkins/jobs/**/workspace/src/main/java``-Dsonar.sources=src/main

那么,有没有可能来分析 JAVAJSMaven的 许多项目 的依赖 使用 SONAR
JENKINS 没有 警告/错误 并以 适当的 方式?确实 SONAR 有限制?

感谢您考虑我的帖子并帮助我解决问题。


问题答案:

我希望可以通过向您指出本文档的第一段为您提供帮助:

它说:“ …因此,对于多语言项目,通常必须将该属性重写为:sonar.sources = src。
请注意,此属性只能在pom文件中设置。无法通过命令行 。”

http://docs.codehaus.org/display/SONAR/Analyzing+with+Maven#AnalyzingwithMaven-
Analyzinga多语言项目

祝好运!



 类似资料:
  • 在使用SonarQube 4.5.4分析c#项目时,我遇到了以下错误。我已经安装了sonar-csharp-plugin-4.0。

  • 我有一个有java和Js文件的maven项目(pom.xml)。我想从Jenkins调用sonar analysis。请告诉我如何分析java和Javascript文件。我已经使用sonar runner为java做了这件事,但问题是Javascripts文件位于项目目录中的不同位置。因此,sonar.src=src/main/java在这里不起作用。我认为我需要使用maven pom.xml来进

  • 问题内容: 我想和詹金斯建立声纳。但是我不确定Sonar网站是否描述了两种不同的方法来执行此操作,或者是否有两个必要步骤: 据我了解,这是两种不同的方式。如果是这样的话,(Sonar本身和Sonar跑步者之间)有什么区别以及优点和缺点是什么? 问题答案: 如果要使用SonarQube和Jenkins分析项目,则需要满足以下条件: SonarQube服务器启动并运行 安装并运行SonarQube S

  • 我可以在构建的控制台输出中看到Sonar被正确调用,但它没有找到任何要索引的文件。据我所知,声纳分析成功完成,但报告是空的。 在本地SonarQube安装时,从命令行本地运行相同的命令可以正常工作。 詹金斯:1.605 声纳:5.1(与Jenkins在同一台机器上运行,在MySQL DB上运行) Jenkins Maven插件:2.8 Jenkins SonarQube插件:2.2 我在詹金斯上添

  • 我为Visual Studio代码分析定义了一组规则,并在Sonar(http://www.sonarqube.org/)中配置了这些规则。我得到不同的结果。

  • 问题内容: 我想在我们的jenkins服务器上安装sonarQube。 我可以找到的文档告诉我在以下位置配置声纳运行器 但是那里没有“声纳赛跑者”选项。我配置了声纳服务器时有一个“声纳”选项,并且声纳插件已安装且是最新的。 如何显示声纳运行器配置? (我们希望独立的声纳作业不是Maven,因为这是一个没有Maven的项目。) 问题答案: 好。所以,如果我了解得很好: 在插件管理器中进入Manage