【问题标题】:Excluding test classes for all analysis from gradle从 gradle 中排除所有分析的测试类
【发布时间】:2015-11-12 17:24:37
【问题描述】:

我正在使用 gradle 中的 sonar-runner 插件调用声纳。我也在使用重用报告标志。 如何从所有分析(Checkstyle、Findbugs、Coverage)中排除所有测试类?

我目前正在使用以下插件配置:

sonarRunner {
sonarProperties {
    property "sonar.host.url", "<HOST>"

    property "sonar.scm.disabled", "true"
    property "sonar.login", "<USER>"
    property "sonar.password", "<password>"

    property "sonar.sources", "src"
    property "sonar.exclusions", "**/test/**/*.java"
    property "sonar.projectVersion", project.releaseDisplayName
    // these should not change anything as sonar uses the defaults set for gradle
    //property "sonar.tests", "test"
}

我的源码集如下:

sourceSets {
main {
    java {
        srcDir 'src'
        srcDir 'src-gen'
    }
}
test {
    java { srcDir 'test' }
}

谢谢

【问题讨论】:

  • 到目前为止您尝试过什么吗?您目前如何配置源集?显示您的插件配置。
  • 我的配置是这样的: sonarRunner { sonarProperties { property "sonar.host.url", "" property "sonar.scm.disabled", "true" property "sonar.login" , "" 属性 "sonar.password", "" 属性 "sonar.sources", "src" 属性 "sonar.exclusions", "*/test/**/ .java" property "sonar.projectVersion", project.releaseDisplayName // 这些不应该改变任何东西,因为 sonar 使用为 gradle 设置的默认值 //property "sonar.tests", "test" } }
  • 您的源集设置如何?
  • sourceSets { main { java { srcDir 'src' srcDir 'src-gen' } } 测试 { java { srcDir 'test' } } }
  • 你能不能试着注释掉这一行:属性“sonar.sources”,“src”。插件应该选择 main 作为默认源集并测试作为默认测试源集

标签: gradle sonarqube


【解决方案1】:

试试这个:

jacocoTestReport {
        afterEvaluate {
                sourceDirectories = files(sourceDirectories.files.collect {
                fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
                })
                classDirectories = files(classDirectories.files.collect {
                fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
                })
        }
}

sonarRunner {
   sonarProperties {
        property "sonar.exclusions", "com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**"
   }
}

  //Required with Gradle 2.0+ -- 2.0+ -- 2.3
  pmd {
       ruleSets = ["java-basic", "java-braces", "java-design" ]
       ignoreFailures = true
  }

   codenarc {
     ignoreFailures = true
     //The following file should exist or build will fail, you can find one online a sample version
     configFile = file("config/codenarc/codenarc.xml")
   }


   checkstyle {
        configFile = new File(rootDir, "config/checkstyle.xml")
        ignoreFailures = true
        //sourceSets = [sourceSets.main, sourceSets.test, sourceSets.integrationTest]

        //Just run checkstyle only on main source code
        sourceSets = [sourceSets.main]
   }


   findbugs {
        ignoreFailures = true
        //Just run findbugs only on main source code
        sourceSets = [sourceSets.main]

        //You can use if statement in groovy to set which toolVersion 2.0.3 or 3.0.1 depending upon JAVA version used in the project
        toolVersion = "3.0.1"
   }

同样,您可以直接在测试或测试任务的 jacoco 部分中使用 excludes 属性。

    def generatedSources = ['com/yahoo/**', 'com/amazon/**']

    test {
         jacoco {
              excludes = generatedSources
         }
    }

    jacocoTestReport {
         doFirst {
              classDirectories = fileTree(dir: "${buildDir}/classes/main/").exclude(generatedSources)
         }
         reports {
              xml.enabled true
         }
    }

发布到 SonarQube 时(sonar.exclusions=value 应该与您的 WORKSPACE 相关,即 src/java/com/.../...)

【讨论】:

    【解决方案2】:

    sonarRunner 插件已弃用。请切换到SonarQube官方插件:https://plugins.gradle.org/plugin/org.sonarqube

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 2015-08-03
      • 2012-06-30
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多