gradle - 从gradle中排除所有分析的测试类

标签 gradle sonarqube

我正在使用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' }
}

谢谢

最佳答案

试试这个:

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应该相对于您的工作空间相对,即src / java / com / ... / ...)

关于gradle - 从gradle中排除所有分析的测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633355/

相关文章:

java - Dodgy - 已知为 null 的值的冗余 nullcheck

java - 处理缓慢的构建时间(Gradle)

android - ionic : compileSdkVersion is not specified - Could not find method leftShift()

android - 当我想导入项目时成绩同步失败

android - 无法将库添加到Android Studio中的项目

java - SONAR Violation IllegalType,为什么修复它很重要?

debugging - 当我在 Gradle 中运行单个测试时,如何获得详细的日志记录?

c# - 从 C# SONAR 分析中排除生成的代码

java - 使用JaCoCo和Gradle进行离线检测

java - Sonar 错误报告删除此始终计算为 "true"的表达式