Sonar 使用 JENKINS 分析 Maven 3 和多语言项目 [英] Sonar analysing Maven 3 and multi language project using JENKINS

查看:20
本文介绍了Sonar 使用 JENKINS 分析 Maven 3 和多语言项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is a problem/issue found while using Sonar in a Maven 3 Java JS project using JENKINS build.

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

To analyse my project , I've choosen two different way, but both doesn't work as it should.

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

First way : Launching Sonar as standlone task as a post-build action in JENKINS

Sonar 插件 v2.1 从 JENKINS

Sonar Runner v2.3 从 JENKINS

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

In post-build section of my maven project, I check Launch Sonar as standlone task and I set these properties :

# 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 分析时的日志:

With this way I can analyse both JAVA and JS as I want.But, SONAR doesn't load maven dependencies as they should be loaded. Here is the log when SONAR analyse :

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 中看到代码覆盖率......并且分析结果成功但我无法在我的构建中保留这些警告.

I can see in Sonar the code coverage...and the analyse is SUCCESSFUL But I can't keep these WARNINGS in my build.

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

So I did a Sonar analyse using maven.

第二种方式:使用maven的Soanar

Second way : Soanar using maven

Maven 插件 v3.0.4 从 JENKINS

Maven plugin v3.0.4 Installed from JENKINS

Sonar 插件 v2.1 从 JENKINS

Sonar plugin v2.1 Installed from JENKINS

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

In after build section I choose Sonar and I set these properties :

 -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

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

Using this way, I have no more classe loader problem and all the dependencies are loaded.

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

But Sonar only analyse the JAVA code, even if I define sonar.sources=src/main he only analyse 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 之前设置.

Again, the analyse is SUCCESSFUL but, SONAR doesn't analyse JS. And as you can see in the log, the Source dirsis set to /**/**/jenkins/jobs/**/workspace/src/main/java even if I seted before -Dsonar.sources=src/main.

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

So , is it possible to analyse both JAVA and JS in a Maven project with many dependencies using SONAR and JENKINS with no WARNING/ERROR and in a proper way ? Does SONAR have limits ?

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

Thank you for concidering my post and help me resolving my issue.

推荐答案

希望能帮到你,指出本文档的第一段:

I hope I can help you by pointing out to you the first paragraph of this documentation:

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

It sais "...Therefore, for a multi-language project, the property usually has to be overridden to: sonar.sources=src. Note that this property can only be set in the pom file. It's not possible to set it via the command line."

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

祝你好运!

这篇关于Sonar 使用 JENKINS 分析 Maven 3 和多语言项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆