I have gradle 8.5/java 21 and my scripts written on kotlin dsl.
I create this gradle.kts

import com.github.spotbugs.snom.SpotBugsTask

plugins {
    id("com.github.spotbugs")
}

spotbugs {
    group = "Verification"
    description = "Run Spotbugs on this project."
    ignoreFailures = false
    showStackTraces = true
    showProgress = true
    maxHeapSize = "1g"

}
tasks.withType<SpotBugsTask>().configureEach {
    dependsOn("assemble")
    classDirs = files("${project.layout.buildDirectory}.absolutePath/build/classes/java/main")
    sourceDirs = files("${project.layout.buildDirectory}.absolutePath/src/main/java")
}

Also I have libs.versions.toml:

[versions]
spotbugs = "6.0.6"

[libraries]
spotbugs-gradle = { module = "com.github.spotbugs.snom:spotbugs-gradle-plugin", version.ref = "spotbugs" }