Getting an error - Could not find method plugins() for arguments [] on object of type org.gradle.api.inte rnal.initialization.DefaultScriptHandler

Getting error -
Could not find method plugins() for arguments [build_553cwzwje4qhyq8irhxdu1zd8$_run_closure1$_closure3@7a842f46] on object of type org.gradle.api.inte
rnal.initialization.DefaultScriptHandler

My build.gradle file -

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    plugins {
        id 'java-library'
        id 'java'
        id 'org.sonarqube' version '3.3'
    }

sourceCompatibility = '1.8'

    dependencies {

        implementation 'org.seleniumhq.selenium:selenium-java:4.0.0-alpha-6'
        implementation 'org.seleniumhq.selenium:selenium-server:4.0.0-alpha-2'
        implementation 'org.seleniumhq.selenium:selenium-chrome-driver:4.0.0-alpha-5'
        implementation 'org.seleniumhq.selenium:selenium-firefox-driver:4.0.0-alpha-5'
        implementation 'org.apache.poi:poi:4.1.2'
        implementation 'jexcelapi:jxl:2.4.2'
        implementation 'com.aventstack:extentreports:5.0.4'
        implementation 'org.apache.poi:poi-ooxml:3.10-FINAL'
        implementation 'org.apache.logging.log4j:log4j-api:2.13.3'
        implementation 'org.apache.logging.log4j:log4j-core:2.13.3'
        implementation 'org.apache.commons:commons-email:1.5'
        testImplementation 'org.testng:testng:7.3.0'
        testImplementation 'io.github.bonigarcia:webdrivermanager:4.2.2'
        implementation group: 'ru.yandex.qatools.ashot', name: 'ashot', version: '1.5.4'
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.3"
    }
}

sonarqube {
    properties {
        property "sonar.host.URL", "[URL hidden]"
        property "sonar.ProjectKey", "testdemogradlesonar"
        property "sonar.Projectname", "testdemogradlesonar"
        property "sonar.sourceEncoding", "UTF-8"
    }
}
// run with the command 'gradle clean test' in CLI
test {
    //this tells Gradle to use TestNG
    useTestNG() {
        // this tells TestNG to execute its default reporting structure
        useDefaultListeners = true
        testLogging.showStandardStreams = true
        // location of testng.xml
        suites 'src/test/resources/testng.xml'
    }
}

// run with the command 'gradle clean smokeTests' in CLI
task smokeTests(type: Test) {
    useTestNG() {
        useDefaultListeners = true
        testLogging.showStandardStreams = true
        suites 'src/test/resources/testng.xml'
        includeGroups 'Smoke'
    }
}
apply plugin: "org.sonarqube"

Hi @Prateek_Agarwal ,

Welcome to SonarSource community! :sonarsource:

The problem you are encountering is probably not related to Sonar scanner plugin. Your build.gradle file needs to be adjusted. The error Could not find method plugins() for arguments... probably means that you should move the plugins{} block to the top of your file and outside of your buildscript {} block.

Also, please remove all references to Sonar scanner (SonarQube) plugin and test again to make sure your gradle file works without SonarQube, then try adding it back in. You should make sure you are using the latest Gradle version AND Sonar scanner for Gradle plugin (see versions here) also. Here’s an example of our gradle file with Sonar scanner/SonarQube plugin: gradle-basic