Skip to content

Instantly share code, notes, and snippets.

@lcamilo15
Forked from mjdetullio/android-sonar.gradle
Created March 29, 2018 17:57
Show Gist options
  • Select an option

  • Save lcamilo15/c281f981bfce1241c61521230aa048e3 to your computer and use it in GitHub Desktop.

Select an option

Save lcamilo15/c281f981bfce1241c61521230aa048e3 to your computer and use it in GitHub Desktop.

Revisions

  1. @mjdetullio mjdetullio renamed this gist Dec 29, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @mjdetullio mjdetullio created this gist Dec 29, 2015.
    81 changes: 81 additions & 0 deletions sonar.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    import com.android.build.gradle.AppPlugin

    task consolidateJunitXml {
    description 'Copies JUnit XML reports into a single directory so SonarQube can import them all'
    doLast {
    def dest = file("${buildDir}/allJunit")
    delete dest
    copy {
    from "${buildDir}/test-results/debug"
    into dest
    }
    copy {
    from "${buildDir}/outputs/androidTest-results/connected"
    into dest
    }
    }
    }

    // or LibraryPlugin
    def getAppPlugin() {
    plugins.getPlugin(AppPlugin)
    }

    def getVariantData(String fullName) {
    getAppPlugin().variantManager.variantDataList.find { it.variantConfiguration.fullName == fullName }
    }

    apply plugin: 'org.sonarqube'

    sonarqube {
    properties {
    property 'sonar.projectName', 'My Super Cool Project'
    property 'sonar.projectVersion', System.getenv('BUILD_NUMBER') ?: 'DEV'
    property 'sonar.branch', System.getenv('SONAR_BRANCH') ?: 'DEV'

    property 'sonar.java.source', JavaVersion.VERSION_1_7
    property 'sonar.java.target', JavaVersion.VERSION_1_7
    property 'sonar.sourceEncoding', 'UTF-8'

    Set<File> sources = []
    Set<File> binaries = []
    Set<File> libraries = []
    Set<File> testSources = []

    def debugVariantData = getVariantData('debug')
    debugVariantData.variantConfiguration.sortedSourceProviders.each { sourceProvider ->
    sources += sourceProvider.assetsDirectories
    sources += sourceProvider.javaDirectories
    sources += sourceProvider.resDirectories
    sources += sourceProvider.resourcesDirectories
    }
    // Replace javaCompileTask with javacTask if upgraded to Android Gradle Plugin 1.3.x
    binaries += debugVariantData.javaCompileTask.outputs.files.files
    libraries += debugVariantData.javaCompileTask.classpath.files
    libraries += getAppPlugin().androidBuilder.bootClasspath

    getVariantData('debugUnitTest').variantConfiguration.sortedSourceProviders.each { sourceProvider ->
    // Only care about the java sources for test variant
    testSources += sourceProvider.javaDirectories
    }
    getVariantData('debugAndroidTest').variantConfiguration.sortedSourceProviders.each { sourceProvider ->
    // Only care about the java sources for androidTest variant
    testSources += sourceProvider.javaDirectories
    }

    property 'sonar.sources', sources.findAll { it.exists() }
    property 'sonar.exclusions', '**/*.js,**/*.css,**/*.html'
    property 'sonar.tests', testSources.findAll { it.exists() }
    property 'sonar.java.binaries', binaries.findAll { it.exists() }
    property 'sonar.java.libraries', libraries.findAll { it.exists() }

    property 'sonar.junit.reportsPath', "${buildDir}/allJunit"
    property 'sonar.jacoco.reportMissing.force.zero', true
    property 'sonar.jacoco.reportPath', "${buildDir}/jacoco/testDebug.exec"
    property 'sonar.jacoco.itReportPath', "${buildDir}/outputs/code-coverage/connected/coverage.ec"
    }
    }

    afterEvaluate {
    tasks.sonarqube.dependsOn tasks.consolidateJunitXml
    }