【问题标题】:Sonar analysing Maven 3 and multi language project using JENKINSSonar 使用 JENKINS 分析 Maven 3 和多语言项目
【发布时间】:2014-07-17 21:24:45
【问题描述】:

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

为了分析我的项目,我选择了两种不同的方法,但两者都不能正常工作。

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

Sonar 插件 v2.1 从 JENKINS

安装

Sonar Runner v2.3 从 JENKINS

安装

在我的 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 不会加载 ma​​ven 依赖项,因为它们应该被加载。这是 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 中看到代码覆盖率...并且分析结果成功但我无法在构建中保留这些警告。

所以我使用 ma​​ven 进行了 Sonar 分析。

第二种方式:使用maven的Soanar

Maven 插件 v3.0.4 从 JENKINS

安装

Sonar 插件 v2.1 从 JENKINS

安装

在构建后部分,我选择声纳并设置这些属性:

 -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 之前设置。

那么,是否可以在具有许多 依赖项Maven 项目中同时分析 JAVAJS SONARJENKINS 没有 WARNING/ERROR 并且以 正确 方式? SONAR 有限制吗?

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

【问题讨论】:

  • 什么是“JS”? JavaScript?

标签: maven jenkins continuous-integration code-analysis sonarqube


【解决方案1】:

如果你想进行依赖分析,那么它确实是使用 Maven 插件进行分析的唯一方法。

如果您希望能够使用命令行中的属性 sonar.sources 覆盖默认的 Maven 源目录,那么您应该观看/投票支持这张票: https://jira.codehaus.org/browse/MSONAR-70

请注意,从 SonarQube Maven 插件 2.3 开始,我们在内部使用 sonar-runner API,因此命令行中的覆盖属性可能适用于您的情况(因为您似乎有一个模块项目)。但在正确修复 MSONAR-70 之前不提供任何保证。

【讨论】:

    【解决方案2】:

    我希望通过向您指出本文档的第一段来帮助您:

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

    http://docs.codehaus.org/display/SONAR/Analyzing+with+Maven#AnalyzingwithMaven-AnalyzingaMulti-languageProject

    祝你好运!

    【讨论】:

    • 就是这样,在pom.xml 中添加<properties><sonar.sources>src/main</sonar.sources></properties> 解决了我的问题。谢谢你的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多