How to add SonarQube Plugin in Build.Gradle and take the reports of current project

Achiever picture Achiever · Aug 13, 2018 · Viewed 7.3k times · Source

I tried downloading SonarQube and followed each steps based on this link, SonarQube Setup and add SonarQube plugins in build.gradle

Was able to execute SonarQube from Command but while I am adding the plugins of SonarCube in Build.Gradle it is showing an error like this when I tried to sync a project,

Error#25: all buildscript {} blocks must appear before any plugins {} blocks in the script

But I have added a plugin and properties of SonarQube in build.gradle,

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

and adding properties of 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 Version is, 3.1.3 and Gradle Version is, 4.4 and, downloaded SonarQube Version is, 7.2.1

My Question here is, Is SonarQube 7.2.1 not compatible for Gradle Version 4.4?

Please see the Image below (SonarQube is Up) in command Prompt, SonarQube up in command prompt

Any idea regarding how to generate the reports of my currrent project using sonarqube and executing a command in cmd prompt?

Error # 33: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

Please find the below build.gradle file,

 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'
    }

What mistake I am doing here in above build.gradle file and how to run sonarQube and generate the reports of my current project?

And, please find the below Image - When I add http://localhost:9000/ in my browser, it does not show any of my Android Projects. Please refer this Image for reference how SonarQube islooking in my browser,

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,

Tried to create Gradle.properties file to add this lines,

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

Opened Android Studio Terminal and executed this, .\gradlew sonarqube

Please see below Image, I am getting Sonar Qube Exception when I tried to execute the command on Android Studio Terminal (Bottom).

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

Answer

M.Ricciuti picture M.Ricciuti · Aug 13, 2018

The error #33 you get comes from the strict syntax that must be followed when using plugins{} block, which is described in Gradle documentation here : https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html (see "Strict syntax" section) In your build.gradle script, you have added the "plugins{}" block after some other directives, which is not allowed. I guess the other error #25 comes from the same reason.

You should try to rewrite your script and move the 'buildscript' block first, then the 'plugins' block, and then other parts of your scripts

hope this helps.