1、解决sonar-checkstyle-plugin运行过程中出现的异常问题

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一、版本说明

        sonar版本:5.6

        checkstyle: 6.12.1

        sonar-checkstyle: 2.5-SNAPSHOT

        checkstyle-all : 2.5-SNAPSHOT

        sonar-checkstyle-plugin: 2.5-SNAPSHOT 

二、问题现象和问题描述

        代码质量配置中添加checkstyle规则后,在执行代码检查时,报如下异常,导致检查中断:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.io.Closeables.closeQuietly(Ljava/io/InputStream;)
at com.puppycrawl.tools.checkstyle.PackageNamesLoader.getPackageNames(PackageNamesLoader.java:156)
at com.puppycrawl.tools.checkstyle.Checker.finishLocalSetup(Checker.java:137)
at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:134)
at org.sonar.plugins.checkstyle.CheckstyleExecutor.execute(CheckstyleExecutor.java:84)
at org.sonar.plugins.checkstyle.CheckstyleSensor.analyse(CheckstyleSensor.java:57)
at org.sonar.batch.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:58)
at org.sonar.batch.phases.SensorsExecutor.execute(SensorsExecutor.java:50)
at org.sonar.batch.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:83)
at org.sonar.batch.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:189)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.scan.ProjectScanContainer.scan(ProjectScanContainer.java:265)
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:260)
at org.sonar.batch.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:258)
at org.sonar.batch.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:250)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.batch.scan.ProjectScanContainer.startComponents(ProjectScanContainer.java:128)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.task.ScanTask.execute(ScanTask.java:55)
at org.sonar.batch.task.TaskContainer.doAfterStart(TaskContainer.java:86)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:142)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:127)
at org.sonar.batch.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:124)
at org.sonar.batch.bootstrapper.Batch.executeTask(Batch.java:119)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy0.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:240)
at org.sonarsource.scanner.api.EmbeddedScanner.runAnalysis(EmbeddedScanner.java:151)
at org.sonarsource.scanner.cli.Main.runAnalysis(Main.java:110)
at org.sonarsource.scanner.cli.Main.execute(Main.java:72)
at org.sonarsource.scanner.cli.Main.main(Main.java:60)

三、原因

        未完全解决guava版本不兼容的问题。具体原因如下:

        sonar-checkstyle插件中,引用了checkstyle和sonar-plugin-api两个jar包,这两个jar包都引用了guava,但是版本不一样,checkstyle使用guava 18.0,sonar-plugin-api使用guava 10.0.1。对于类 com.google.common.io.Closeables ,guava 18.0不兼容10.0.1【假如在pom文件中不指定guava版本的话,默认是用高版本】。sonar-checkstyle是通过maven的shade插件来解决guava版本不兼容的问题,具体做法:在sonar-checkstyle项目中建立checkstyle-all项目中通过【maven-shade-plugin】插件,将checkstyle中的guava18.0包合并到checkstyle包【com.puppycrawl.tools.checkstyle.guava】路径下,并重新生成【checkstyle-all-2.5.2-SNAPSHOT.jar】包来替代checkstyle-6.12.1,在真正的sonar-checkstyle插件项目sonar-checkstyle-plugin中引用的 checkstyle-all,是来解决guava冲突的问题。

1、解决sonar-checkstyle-plugin运行过程中出现的异常问题_第1张图片

        但是,由于 checkstyle-all 引用了checkstyle,导致 sonar-checkstyle-plugin间接引用了checkstyle,打包的时候,仍然将checkstyle打包进入 sonar-checkstyle-plugin的jar包。 类加载的时候依然加载的是 checkstyle,而不是替换checkstyle和guava18.0的 checkstyle-all,所以并没有真正解决guava版本不兼容的问题。

四、解决办法

        sonar-checkstyle-plugin项目在引用checkstyle-all的时候,排除引用checkstyle,又通过引用scope级别为provided的checksytle包,来避免运行时无法通过编译的问题。

1、解决sonar-checkstyle-plugin运行过程中出现的异常问题_第2张图片

    配置如下:

    
      ${project.groupId}
      checkstyle-all
      ${project.version}
      
      
        
          com.puppycrawl.tools
          checkstyle
        
      
    

    
    
      com.puppycrawl.tools
      checkstyle
      ${checkstyle.version}
      
      provided
    

 

 

转载于:https://my.oschina.net/andChow/blog/716440

你可能感兴趣的:(1、解决sonar-checkstyle-plugin运行过程中出现的异常问题)