如何在Build.Gradle中添加SonarQube插件并获取当前项目的报告

时间:2018-08-13 09:58:24

标签: android gradle android-gradle-2.0

我尝试下载SonarQube,并根据此链接进行了每个步骤, SonarQube Setup and add SonarQube plugins in build.gradle

能够从Command执行SonarQube,但是当我在Build.Gradle中添加SonarCube插件时,当我尝试同步项目时却显示出这样的错误,

错误#25:所有buildscript {}块必须出现在脚本中的任何插件{}块之前

但是我在build.gradle中添加了SonarQube的插件和属性,

plugins {
id "org.sonarqube" version "7.2.1"
} 

并添加SonarQube的属性,

sonarqube {
properties {
    property "sonar.projectName", "MyApplication2"
    property "sonar.projectKey", "SQKey"
    property "sonar.sources","src/main/java"
    property "sonar.language","java"
    property "sonar.sourceEncoding", "UTF-8"
}}

Android Studio版本为3.1.3和 Gradle版本为4.4 并且,下载的SonarQube版本是7.2.1

我的问题是,SonarQube 7.2.1与Gradle版本4.4不兼容吗?

请在命令提示符下查看下面的图像(SonarQube已启动), SonarQube up in command prompt

关于如何使用sonarqube生成我的当前项目报告并在cmd提示符下执行命令的任何想法吗?

错误#33:在插件{}块之前,仅允许buildscript {}和其他插件{}脚本块,不允许其他语句

请找到以下build.gradle文件,

 buildscript {
repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.3'
    classpath 'com.google.gms:google-services:2.1.0'
    classpath 'io.fabric.tools:gradle:1.+'
    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: "org.sonarqube"



repositories {
maven { url 'https://maven.fabric.io/public' }
}


apply plugin: 'com.google.gms.google-services'

plugins {
id "org.sonarqube" version "7.2.1"
}
sonarqube{
properties{
    property "sonar.projectName", "Example 16-8"
    property "sonar.projectKey", "example 16-6-key"
    property "sonar.sources","src/com.example.project"
    property "sonar.language","java"
    property "sonar.sourceEncoding", "UTF-8"
    property "sonar.jacoco.reportPath", 
    "build/jacoco/testDebugUnitTest.exec"
    //        property "sonar.exclusions", "src/main/java/com/foo/Foo.java"
    }}
    allprojects {
    repositories {
    jcenter()
    google()
    }
    }
    android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    }

在build.gradle文件中我在这里犯了什么错误?如何运行sonarQube并生成当前项目的报告?

并且,请找到下面的图像-当我在浏览器中添加http://localhost:9000/时,它不会显示任何Android项目。请参考此图像以获取SonarQube在我的浏览器中的外观,

When I add http://localhost:9000/ in my browser, it does not show any of my Android Projects. Please refer this Image how does sonarqube looks in my browser,

试图创建Gradle.properties文件以添加此行,

systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=admin
systemProp.sonar.password=admin

打开Android Studio终端并执行此操作,     。\ gradlew sonarqube

请参见下图,尝试在Android Studio终端(底部)上执行命令时,我收到Sonar Qube异常。

I am getting Sonar Qube Exception when I tried to execute the command on Terminal.

2 个答案:

答案 0 :(得分:1)

sonarqube {
    properties {
    property "sonar.host.url", "https://sonar-url"
    property "sonar.login", "login - token "

    property "sonar.projectName", "hello-world-service"
    property "sonar.projectKey", "hello-world-service"

    property "sonar.coverage.jacoco.xmlReportPaths", "$buildDir/reports/jacoco/test/jacocoTestReport.xml"
    property "sonar.junit.reportPaths", "$buildDir/test-results/test/"
    property "sonar.exclusions", "**/xmldefs/**,**/spec/api.json"
    property "sonar.cpd.exclusions", ", **/persistence/entity/**"
    property "sonar.dynamicAnalysis", "reuseReports"
    property "sonar.clover.reportPaths", "**/build/clover/clover.xml"
    property "sonar.java.coveragePlugin", "clover"
    property "sonar.sources", "src/main/kotlin"
    property "sonar.tests", "."
    property "sonar.test.inclusions", "**/*Test*/**"
    }
  }

// Build should fail if coverage less than minimum.
jacocoTestCoverageVerification.dependsOn jacocoTestReport
build.finalizedBy jacocoTestCoverageVerification

//在您的build.gradle中添加

答案 1 :(得分:0)

您收到的错误#33来自使用plugins {}块时必须遵循的严格语法,在此处Gradle文档中对此进行了描述:https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html(请参阅“严格语法”部分) 在build.gradle脚本中,您在其他一些指令之后添加了“ plugins {}”块,这是不允许的。 我猜另一个错误#25来自相同的原因。

您应该尝试重写脚本,然后先移动“ buildscript”块,然后移动“ plugins”块,然后移动脚本的其他部分

希望这会有所帮助。