Sonarqube plugin插件 在使用Sonar-scanner时不能 扫描 file index 动态新生成的文件 解决方案...

在维护公司 Sonarqube平台时 从旧的4.5升级到支持LTS 5.6.x 时 导致原来的plugin不能 兼容,特别是扫描代码时我需要生成新的 service代码文件。

在5.6之前 Sonar-runner调用plugin扫描代码开始execute时都是通过扫描项目路径 所以不存在扫描不到新文件的问题,但是5.6之后  都是在execute scan之前 将所有的file 进行了index处理,这样就导致 动态新文件无法被index,所以无法被扫描,翻了很多官网资料都无解决方案 。

我开始的想法是在execute之后动态增加inputfile  ,模拟scanner服务端 add  defaultinputfile ,但是不work,由于 sonar scanner服务端采用了cache和其他复杂的设计,我新增的file 总是 不能被有效添加index中,缺少这样或者那样的 初始化值。 无奈放弃。

通过调试插件发现 plugin入口类define方法 是可以在sonar-scanner  file indexed之前触发的,于是从这入手 ,先把需要生成的文件生成好,这样后面就能正常file index了。

 但是这样我们我们无法获取部分项目属性和参数比如项目路径,但是我们可以用File directory = new File("");获取到。

 

@Override
    public void define(Context context) {
        context.addExtensions(FlowLanguage.class, FlowRules.class, Profile.class, PagesProfile.class,
                DocumentationMetrics.class, WebMethodsSensor.class, PagesLanguage.class, PagesRules.class);

        context.addExtension(PropertyDefinition.builder(WebMethodsPlugin.PROPERTY_WM_HOME)
                .name("Directory of webMethods installation")
                .description(
                        "Directory of webMethods installation; used for building the inital set of service specifications.")
                .defaultValue(System.getenv("WM_HOME")).onQualifiers(Qualifiers.PROJECT).category(FLOW_CATEGORY)
                .build());
        File directory = new File("");
        File packagePath = directory.getAbsoluteFile();
        //File f2 = new File(this.getClass().getResource("").getPath());
        LOG.debug("Plugin start ....");
        LOG.info("project path: " + packagePath.getAbsolutePath());
        if (packagePath.getAbsolutePath().toString().contains("Rb")) {
            LOG.debug("Scan start preprocess: " + packagePath.getAbsolutePath());
            PrepareLoad.preprocess(packagePath);
        }
        //  LOG.info("System path: "+System.getProperty("java.class.path"));
         // LOG.info("Current project path: "+f2.getAbsolutePath().toString());
    }

 

但是这样有个问题,sensor类 是scan执行类,plugin 方法define  会被触发三次。看api注释 :

 /**
   * This method is executed at runtime when:
   * <ul>
   *   <li>Web Server starts</li>
   *   <li>Compute Engine starts</li>
   *   <li>Scanner starts</li>
   * </ul>
   */
  void define(Context context);

 

我们真正需要的是第三次 scanner starts 才是执行扫描 项目包的 真正开始时,所以需要想法屏蔽掉之前的两次执行。我此处是根据自己业务 检测 包含rb字样才执行生成新文件方法

packagePath.getAbsolutePath().toString().contains("Rb")
这样解决了 问题,通过执行 测试 
 
 
INFO: Scanner configuration file: D:\sonar-scanner-3.0.3.778-windows\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: C:\Users\xxxxx\Desktop\projectname\sonar-project.properties
INFO: SonarQube Scanner 3.0.3.778
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Windows Server 2008 R2 6.1 amd64
INFO: User cache: C:\Users\xxxxxx\.sonar\cache
INFO: Load global repositories
INFO: Load global repositories (done) | time=218ms
INFO: User cache: C:\Users\LUM9SZH\.sonar\cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=16ms
INFO: Download sonar-plugin-xxx-webmethods-1.0.0.jar
INFO: SonarQube server 5.6.7
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: project path: C:\Users\xxxxxx\Desktop\projectname
INFO: Found manifest: C:\Users\xxxx\Desktop\projectname\manifest.v3
INFO: Process project properties
INFO: project path: C:\Users\xxxx\Desktop\projectname
INFO: Found manifest: C:\Users\xxx\Desktop\projectname\manifest.v3
INFO: Load project repositories
INFO: Load project repositories (done) | time=187ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=62ms
INFO: Load active rules
INFO: Load active rules (done) | time=639ms
INFO: Publish mode
INFO: -------------  Scan xxxxxxx
INFO: project path: C:\Users\xxxxxx\Desktop\projectname
INFO: Found manifest: C:\Users\LUM9SZH\Desktop\projectname\manifest.v3
INFO: Load server rules
INFO: Load server rules (done) | time=156ms
INFO: Base dir: C:\Users\LUM9SZH\Desktop\projectname
INFO: Working dir: C:\Users\xxxx\Desktop\projectname\.scannerwork
INFO: Source paths: config, ns, code, pub, web
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 7 files indexed
INFO: Quality profile for flow: XXXX  Flow Language Profile
INFO: print start...
INFO: Setting: com.xxx.webmethods.sonar.pkgclassifile=/opt/sonar-runner-2.3/conf/packages.xlsx
INFO: Setting: sonar.projectName=RbKrDesadvToDesadv
INFO: Setting: sonar.host.url=http://localhost:9567/sonarqube/
INFO: Setting: 。。。。。

 请注意 ------------- Scan xxxxxxx 

下面才是 扫描代码的开始。  可以看到 7 files indexed(其中6个是在plugin方法中 动态生成的 )

Quality profile for flow: XXXX  Flow Language Profile...之后才是检测代码执行,之前index是在scanner服务端处理。

Jenkins测试也是正常,有一点需要注意的是,jenkin在配置参数时 路径一定要包含在项目文件xia  不要放在不相关的目录,这样便于监测 是否决定根据自己业务规则执行新增文件。

 

转载于:https://www.cnblogs.com/OneDirection/articles/9046230.html

要在您的 Android Studio 项目中集成 SonarQube,您可以按照以下步骤操作: 1. **安装 SonarQubeScanner**: - 下载并安装最版本的[SonarQube](https://www.sonarsource.org/)服务器。 - 启动 SonarQube 服务,默认访问地址为 http://localhost:9000。 - 安装适用于您构建系统的 SonarScanner(如 Gradle 插件)。 2. **配置 Project Properties**: 在项目的根目录下创建或编辑名为 `sonar-project.properties` 的文件,并添加如下内容: ```properties sonar.projectKey=your_project_key sonar.sources=src/main/java sonar.host.url=http://localhost:9000 sonar.login=<Your Token> ``` 其中 `<Your Token>` 可从 SonarQube Web GUI 用户个人资料页面生成。 3. **修改 Build.gradle 文件**: 如果使用的是 Gradle 构建系统,在模块级 (通常是 app) `build.gradle` 添加依赖项及任务: ```groovy plugins { id 'org.sonarqube' version '3.x.x' } repositories { ... } //已有代码无需改动 apply plugin: 'com.android.application' apply plugin: 'org.sonarqube' android {...} //原有android块保持变 dependencies{...} task sonarqube(type: JavaExec){ classpath = configurations.runtimeClasspath main = "org.sonarscanner.cli.Main" args '-X', "-Dproject.settings=${rootProject.file('sonar-project.properties')}" } ``` 4. **执行分析**: 打开终端窗口然后切换至包含此脚本工程路径之下,键入下面命令开始检查: ```shell ./gradlew clean :app:sonarqube ``` 成功完成后将能够在浏览器端浏览质量报告详情了! 5. **持续集成(可选)**: 为了进一步提升效率,建议将其纳入 CI/CD 流程当中去自动定期触发审核机制;比如 Jenkins、GitLab Runner 等工具都能很好适配上述流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值