简体   繁体   English

在Jenkins的多模块项目中从Sonar Qube扫描中排除目录而不修改项目

[英]Excluding directories from Sonar Qube scan in a multi module project in Jenkins without modifications to project

I have created a Jenkins job which performs a SONAR QUBE analysis. 我创建了一个执行SONAR QUBE分析的Jenkins作业。 I have added the SONAR scan as a generic build step. 我已将SONAR扫描添加为通用构建步骤。 I have not made any modifications to the project to enable the SONAR integration since the project should not have to care about SONAR. 我没有对项目进行任何修改以启用SONAR集成,因为项目不应该关心SONAR。 (ie no changes to POM.xml and no properties file(s) added) (即没有更改POM.xml并且没有添加属性文件)

My SONAR integration is working, without any mods necessary to the project, which is great. 我的SONAR集成工作正常,没有项目所需的任何模块,这很棒。

However, it is a multi module project and now I want to exclude one of the modules from the scan, since this module contains auto generated code. 但是,它是一个多模块项目,现在我想从扫描中排除其中一个模块,因为该模块包含自动生成的代码。 Here are the Maven commands that I'm using to trigger the SONAR scan in Jenkins: 以下是我用于在Jenkins中触发SONAR扫描的Maven命令:

sonar:sonar
-Dsonar:host.url=http://url.com:9000/sonar
-Dsonar.login=myusername
-Dsonar.password=mypassword

Here is my project structure: 这是我的项目结构:

    root
      Project A
      Project B
      Project C

I want to exclude ProjectB from the scan. 我想从扫描中排除ProjectB。

I have tried -Dsonar.exclusions=/ProjectB and it did not work. 我试过-Dsonar.exclusions=/ProjectB但它没有用。

Here is an excerpt from the log: 以下是日志的摘录:

[INFO] ------------- Scan Project B
[INFO] Base dir: /workspace/jenkins/…/workspace/CI-JOB-NAME/projectB
[INFO] Working dir: /workspace/jenkins/…/workspace/CI-JOB-NAME/projectB/target/sonar
[INFO] Source paths: pom.xml, src/main/java
[INFO] Binary dirs: target/classes
[INFO] Source encoding: UTF-8, default locale: en_US
[INFO] Index files
[INFO] Excluded sources: 
[INFO]   /projectB
[INFO] 44 files indexed
[INFO] 0 files ignored because of inclusion/exclusion patterns

As shown in the log, the excluded sources property is set but not working since it says 0 files ignored... 如日志中所示,排除的sources属性已设置但无法正常工作,因为它表示忽略了0个文件...

Question: How do I exclude everything inside the project B directory from the scan? 问题:如何从扫描中排除项目B目录中的所有内容? (Preferably using a command in the Jenkins job and not adding any properties files or other modifications to the project itself) (最好使用Jenkins作业中的命令,不要向项目本身添加任何属性文件或其他修改)

Is the “Excluded sources path” relative to the “Base dir” path or the Root?? 相对于“Base dir”路径或Root的“Excluded sources path”是什么? Should I define the exclusion path as an absolute path? 我应该将排除路径定义为绝对路径吗?

The documentation has a dedicated section for this. 文档有一个专门的部分。 The options that seem suitable for your case: 适合您案例的选项:

  • use build profiles to exclude some module (like for integration tests) 使用构建配置文件来排除某些模块(如集成测试)
  • use Advanced Reactor Options (such as -pl ). 使用Advanced Reactor Options (例如-pl )。 For example mvn sonar:sonar -pl !module2 例如mvn sonar:sonar -pl !module2

For example using this last one, you could do: 例如,使用最后一个,你可以这样做:

sonar:sonar -pl "!projectB"
-Dsonar:host.url=http://url.com:9000/sonar
-Dsonar.login=myusername
-Dsonar.password=mypassword

Is the “Excluded sources path” relative to the “Base dir” path or the Root?? 相对于“Base dir”路径或Root的“Excluded sources path”是什么? Should I define the exclusion path as an absolute path? 我应该将排除路径定义为绝对路径吗?

According to the documentation on exclusions , patterns are relative to the base directory, so not absolute paths. 根据排除文档 ,模式是相对于基本目录的,因此不是绝对路径。 In any case, I don't think this option is useful in your case, because in a multi-module maven project, the files of a sub-module are indexed with the path relative to the module's base directory. 在任何情况下,我认为此选项在您的情况下并不有用,因为在多模块maven项目中,子模块的文件使用相对于模块基本目录的路径编制索引。 So a pattern like projectB/** will not match anything, as projectB is not part of the paths used. 所以像projectB/**这样的模式将不匹配任何东西,因为projectB不是所用路径的一部分。 If you have some unique package name in the module, let's say someuniquepackage , located at src/main/java/someuniquepackage , then the pattern src/main/java/someuniquepackage/** would work. 如果你在模块中有一些独特的包名,让我们说someuniquepackage位于src/main/java/someuniquepackage ,那么模式src/main/java/someuniquepackage/**就可以了。

In anycase, I recommend the -pl option above, or using Maven's profile feature. 无论如何,我推荐上面的-pl选项,或者使用Maven的配置文件功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM