且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以使用不同的排除项在声纳中对同一个Maven项目进行两次分析吗?

更新时间:2023-10-28 08:03:40

您的编辑正确.使用sonar.branch属性会创建一个不同的ID,即从Sonar的角度来看,这是一个不同的项目.

您可以将sonar.branch属性包含在栏"配置文件中(当然,您需要在第二份工作中调用该配置文件),从而确保您的数据库不会损坏.

是的,您需要对项目进行两次分析,但是这样做更加简洁(以后您可以选择对条形图使用不同的规则).

但是,您不一定需要创建两个作业,您可以将Sonar分析用作构建动作,而不是后期构建动作.

使用商业Views插件,您可能无需两次构建就可以实现效果.

I have a multi-module Maven project that is analyzed (within Jenkins) by Sonar. This creates a Sonar project containing metrics for the entire code base of my Maven project. The Sonar project has the same ID as the ID of my Maven project. So if my Maven project is Foo (with modules M1, M2, and M3) then I observe that a Sonar project Foo is created, and I can see the metrics for components M1, M2, and M3.

I am wondering if I can create another Sonar project that performs analysis on a subset of the Maven project. I can do this with Maven + Cobertura, by specifying a Maven profile that defines the set of packages to be included in (or excluded from) the Cobertura analysis. This allows me to produce a set of Cobertura reports: one for the project as a whole, one for a packages X, Y and Z; etc.

So what I am looking at now is creating a Sonar project called Bar that is the result of calculating metrics for the Maven project Foo, and that Sonar project will be configured in such a way to include only packages X, Y, Z (which may live in modules M1, M2, etc). It will kick off as a Jenkins job, checking out Maven project Foo and calling the sonar post-build action, somehow specifying Sonar project name Bar instead of defaulting to Foo. Then (I hope) I will have two Sonar projects: Foo representing the entire Maven project, and Bar representing a subset of that Maven project.

I'm wondering if (a) this is possible, and (b) will it work? I'm not sure if the Sonar database will be corrupted (there will be modules M1, M2, and M3 in the Sonar projects Foo and Bar, these may overwrite the database entries for each other).

Update 1

Maybe I can use the sonar.branch property when invoking sonar as a post-build action in Jenkins.

e.g.

Jenkins Job One

  • SCM: checks out maven project Foo
  • Build Step: clean install -DskipTests
  • Post-Build Action: sonar

Jenkins Job Two

  • SCM: checks out maven project Foo
  • Build Step: clean install -DskipTests
  • Post-Build Action: sonar -Dsonar.branch=Bar

I can setup the inclusions/exclusions for the Bar branch in Sonar, as necessary. The downside of this approach is that the sonar analysis is run twice on two different branches. It would be much more efficient to run the sonar analysis once and then query the database to narrow the focus as required.

Update 2

I'm running into a problem: although I can run a sonar analysis on the branch, the sonar analysis of the original (non-branch) Maven project is failing. The sonar analysis is taking 3 times as long, before it fails with MySQL errors:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.

The last packet successfully received from the server was 28,962,745 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.

I don't get any MySQL errors when running a sonar analysis on other Maven projects, however.

Your edit is correct. Using the sonar.branch property creates a different Id, i.e. from the Sonar point of view a different project.

You could include the sonar.branch property in your "bar"-profile (of course you need to call the profile in your second job) and thus make sure that your database does not get corrupted.

True, you would need to analyze the project twice, but is much more cleaner that way (and you could later choose to use different rules for your bar profile).

However you would not necessarily need to create two jobs, you can use a Sonar analysis as a build action instead of a post build action.

You might be able to achieve your effect without building twice using the commerical Views plugin.