Skip to content

Repository files navigation

bazel_rules_pmd

The PMD (a static analysis tool) integration for the Bazel build system.

Usage

MODULE.bazel Configuration

bazel_dep(name = "rules_pmd", version = "...")

Please refer to GitHub releases for the available versions.

BUILD Configuration

Once declared in the MODULE.bazel file, the rule can be loaded in the BUILD file.

load("@rules_pmd//pmd:defs.bzl", "pmd_test")

pmd_test(
    name = "pmd_analysis_test",
    srcs = glob(["src/main/java/**/*.java"]),
    rulesets = ["pmd_ruleset.xml"],
)

PMD Version

Change the MODULE.bazel file:

pmd = use_extension("@rules_pmd//pmd:extensions.bzl", "pmd")
pmd.pmd_version(
    version = "x.x.x",
    sha256 = "x.x.x.sha256",
)
use_repo(pmd, "net_sourceforge_pmd")

To download PMD from a custom location (for example, an internal mirror), provide a url_templates list:

pmd = use_extension("@rules_pmd//pmd:extensions.bzl", "pmd")
pmd.pmd_version(
    version = "x.x.x",
    sha256 = "x.x.x.sha256",
    url_templates = [
        "https://my-mirror.example.com/pmd/pmd-dist-{version}-bin.zip",
        "https://backup.example.com/pmd/pmd-dist-{version}-bin.zip",
    ],
)
use_repo(pmd, "net_sourceforge_pmd")

Each template may contain {version}, which is replaced with the selected PMD version. Supplying a non-empty list replaces the default GitHub URL; omitting url_templates, or passing an empty list, uses that default. Custom URLs must provide the same pinned PMD distribution archive layout (pmd-bin-{version}/lib) and the sha256 of that archive.

This release uses PMD 7.26.0. Custom PMD distributions must be PMD 7.14.0 or newer because the rule uses --exclude-file-list; PMD 6 distributions are no longer supported. Existing rulesets may need updates; see the PMD 7 migration guide and use PMD 7 rule references, for example:

<rule ref="category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod" />

See available attributes.

JVM Flags

The default PMD toolchain supplies no JVM flags, preserving the Java runtime defaults. To configure flags, define and register a custom toolchain:

# BUILD
load("@rules_pmd//pmd:toolchain.bzl", "pmd_toolchain")

pmd_toolchain(
    name = "pmd_toolchain_impl",
    jvm_flags = ["-Xmx1g", "-Dexample.property=value"],
)

toolchain(
    name = "pmd_toolchain",
    toolchain = ":pmd_toolchain_impl",
    toolchain_type = "@rules_pmd//pmd:toolchain_type",
)

Register it from MODULE.bazel with register_toolchains("//:pmd_toolchain").

Execution

$ bazel test //YOUR_PACKAGE:pmd_analysis_test

Human-readable PMD reports are replayed in the test log, including when the PMD build action is cached. All reports remain available as build outputs.

About

Bazel rules for PMD (static analysis tool)

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages