The Detekt (a Kotlin static analysis tool) integration for the Bazel build system.
- configuration and baseline files;
- HTML, text and XML reports;
- plugins;
- customizable Detekt version and JVM flags;
- persistent workers support;
- and more.
First of all you need to declare the rule in the WORKSPACE
file.
Please refer to GitHub releases for the version and the SHA-256 hashsum.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
rules_detekt_version = "see-github-releases-page"
rules_detekt_sha = "see-github-releases-page"
http_archive(
name = "rules_detekt",
sha256 = rules_detekt_sha,
strip_prefix = "bazel_rules_detekt-{v}".format(v = rules_detekt_version),
url = "https://github.com/buildfoundation/bazel_rules_detekt/archive/v{v}.tar.gz".format(v = rules_detekt_version),
)
load("@rules_detekt//detekt:dependencies.bzl", "rules_detekt_dependencies")
rules_detekt_dependencies()
load("@rules_detekt//detekt:toolchains.bzl", "rules_detekt_toolchains")
rules_detekt_toolchains()
Users on Bazel releases prior to 5.1.0 need to enable the JSON Persistent Worker protocol in their .bazelrc
like so:
build --experimental_worker_allow_json_protocol
This option became stable and enabled by default for newer Bazel releases.
Once declared in the WORSKPACE
file, the rule can be loaded in the BUILD
file.
load("@rules_detekt//detekt:defs.bzl", "detekt")
detekt(
name = "my_detekt",
srcs = glob(["src/main/kotlin/**/*.kt"]),
)
See available attributes.
$ bazel build //mypackage:my_detekt
Results will be cached on successful runs.
Change the WORKSPACE
file:
- rules_detekt_toolchains()
+ rules_detekt_toolchains(detekt_version = "x.y.z")
Define a toolchain in a BUILD
file:
load("@rules_detekt//detekt:toolchain.bzl", "detekt_toolchain")
detekt_toolchain(
name = "my_detekt_toolchain_impl",
jvm_flags = ["-Xms16m", "-Xmx128m"],
)
toolchain(
name = "my_detekt_toolchain",
toolchain = "my_detekt_toolchain_impl",
toolchain_type = "@rules_detekt//detekt:toolchain_type",
)
Change the WORKSPACE
file:
- rules_detekt_toolchains()
+ rules_detekt_toolchains(toolchain = "//mypackage:my_detekt_toolchain")