找不到方法sonarProperties()

时间:2018-10-09 13:34:59

标签: gradle sonarqube

我正在尝试配置build.gradle来生成代码覆盖率报告并将其上传到声纳

import static org.apache.tools.ant.taskdefs.condition.Os.*

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1"
  }
}

 plugins {
   id "org.sonarqube" version "2.6.1"
   id "java" 
 }

task sonarRunner {
        sonarProperties {
                property "sonar.host.url", sonar.host.url
                property "sonar.organization", sonar.organization
                property "sonar.projectKey", sonar.projectKey
                property "sonar.login", sonar.login
                property "sonar.jacoco.reportPath", sonar.jacoco.reportPath
        }
}

task generateReport {
        exec {
          commandLine 'ant', 'jacoccoalltests', '-Dtestclasses.extensions=' + code.coverage.extensions
      }

}

task checkOs() << {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'setantenv.bat'
    }
    if(Os.isFamily(Os.FAMILY_UNIX)) {
        exec {
          executable "./setantenv.sh"
        }
    }
    if(Os.isFamily(Os.FAMILY_MAC)) {
        exec {
          executable ". ./setantenv.sh"
        }
    }
}

sonarRunner.dependsOn generateReport
generateReport.dependsOn checkOs

当我运行 ./ gradlew clean build 时,出现以下错误

  

原因:   org.gradle.internal.metaobject.AbstractDynamicObject $ CustomMessageMissingMethodException:   找不到方法sonarProperties()作为参数   [build_5g79eps6d8kumu5fvgq9l0s46 $ _run_closure1 $ _closure5 @ 1f58d70]   org.gradle.api.DefaultTask类型的任务':sonarRunner'

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

为了配置 SonarQube 插件,您需要使用名为sonarqube Gradle扩展,如here

所述
sonarqube {
    properties {
        property "sonar.exclusions", "**/*Generated.java"
        property "sonar.host.url", sonar.host.url
        property "sonar.organization", sonar.organization
        property "sonar.projectKey", sonar.projectKey
        property "sonar.login", sonar.login
        property "sonar.jacoco.reportPath", sonar.jacoco.reportPath     
    }
}

另请参见此插件存储库here中的自述文件