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  不要放在不相關的目錄,這樣便於監測 是否決定根據自己業務規則執行新增文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM