Skip to content

Commit

Permalink
Allow the spotbugs binary to be selected by the user
Browse files Browse the repository at this point in the history
But continue to default to the current latest version of spotbugs
because that's just good manners.

This change mirrors the same one made for `checkstyle_test`, and
so we also include a macro for creating the target for the
spotbugs CLI tool.
  • Loading branch information
shs96c committed Jan 20, 2022
1 parent 327fd4e commit da93854
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 64 deletions.
52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Use PMD to lint the `srcs`.
## spotbugs_config

<pre>
spotbugs_config(<a href="#spotbugs_config-name">name</a>, <a href="#spotbugs_config-effort">effort</a>, <a href="#spotbugs_config-exclude_filter">exclude_filter</a>, <a href="#spotbugs_config-fail_on_warning">fail_on_warning</a>)
spotbugs_config(<a href="#spotbugs_config-name">name</a>, <a href="#spotbugs_config-effort">effort</a>, <a href="#spotbugs_config-exclude_filter">exclude_filter</a>, <a href="#spotbugs_config-fail_on_warning">fail_on_warning</a>, <a href="#spotbugs_config-spotbugs_binary">spotbugs_binary</a>)
</pre>

Configuration used for spotbugs, typically by the `//lint` rules.
Expand All @@ -166,14 +166,15 @@ Configuration used for spotbugs, typically by the `//lint` rules.
| <a id="spotbugs_config-effort"></a>effort | Effort can be min, less, default, more or max. Defaults to default | String | optional | "default" |
| <a id="spotbugs_config-exclude_filter"></a>exclude_filter | Report all bug instances except those matching the filter specified by this filter file | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="spotbugs_config-fail_on_warning"></a>fail_on_warning | Whether to fail on warning, or just create a report. Defaults to True | Boolean | optional | True |
| <a id="spotbugs_config-spotbugs_binary"></a>spotbugs_binary | The spotbugs binary to run. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | @contrib_rules_jvm//java:spotbugs_cli |


<a id="#spotbugs_test"></a>

## spotbugs_test

<pre>
spotbugs_test(<a href="#spotbugs_test-name">name</a>, <a href="#spotbugs_test-config">config</a>, <a href="#spotbugs_test-deps">deps</a>, <a href="#spotbugs_test-effort">effort</a>, <a href="#spotbugs_test-fail_on_warning">fail_on_warning</a>, <a href="#spotbugs_test-only_output_jars">only_output_jars</a>)
spotbugs_test(<a href="#spotbugs_test-name">name</a>, <a href="#spotbugs_test-config">config</a>, <a href="#spotbugs_test-deps">deps</a>, <a href="#spotbugs_test-only_output_jars">only_output_jars</a>)
</pre>

Use spotbugs to lint the `srcs`.
Expand All @@ -184,10 +185,8 @@ Use spotbugs to lint the `srcs`.
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="spotbugs_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="spotbugs_test-config"></a>config | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="spotbugs_test-config"></a>config | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //java:spotbugs-default-config |
| <a id="spotbugs_test-deps"></a>deps | - | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
| <a id="spotbugs_test-effort"></a>effort | Effort can be min, less, default, more or max. Defaults to default | String | optional | "default" |
| <a id="spotbugs_test-fail_on_warning"></a>fail_on_warning | If set to true the test will fail on a warning, otherwise it will succeed but create a report. Defaults to True | Boolean | optional | True |
| <a id="spotbugs_test-only_output_jars"></a>only_output_jars | If set to true, only the output jar of the target will be analyzed. Otherwise all transitive runtime dependencies will be analyzed | Boolean | optional | True |


Expand Down Expand Up @@ -395,6 +394,49 @@ attribute to allow all the tests to be run in one go.
| <a id="java_test_suite-kwargs"></a>kwargs | <p align="center"> - </p> | none |


<a id="#spotbugs_binary"></a>

## spotbugs_binary

<pre>
spotbugs_binary(<a href="#spotbugs_binary-name">name</a>, <a href="#spotbugs_binary-main_class">main_class</a>, <a href="#spotbugs_binary-deps">deps</a>, <a href="#spotbugs_binary-runtime_deps">runtime_deps</a>, <a href="#spotbugs_binary-srcs">srcs</a>, <a href="#spotbugs_binary-visibility">visibility</a>, <a href="#spotbugs_binary-kwargs">kwargs</a>)
</pre>

Macro for quickly generating a `java_binary` target for use with `spotbugs_config`.

By default, this will set the `main_class` to point to the default one used by spotbugs
but it's ultimately a drop-replacement for a regular `java_binary` target.

At least one of `runtime_deps`, `deps`, and `srcs` must be specified so that the
`java_binary` target will be valid.

An example would be:

```starlark
spotbugs_binary(
name = "spotbugs_cli",
runtime_deps = [
artifact("com.github.spotbugs:spotbugs"),
artifact("org.slf4j:slf4j-jdk14"),
],
)
```


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="spotbugs_binary-name"></a>name | The name of the target | none |
| <a id="spotbugs_binary-main_class"></a>main_class | The main class to use for spotbugs. | <code>"edu.umd.cs.findbugs.LaunchAppropriateUI"</code> |
| <a id="spotbugs_binary-deps"></a>deps | The deps required for compiling this binary. May be omitted. | <code>None</code> |
| <a id="spotbugs_binary-runtime_deps"></a>runtime_deps | The deps required by spotbugs at runtime. May be omitted. | <code>None</code> |
| <a id="spotbugs_binary-srcs"></a>srcs | If you're compiling your own <code>spotbugs</code> binary, the sources to use. | <code>None</code> |
| <a id="spotbugs_binary-visibility"></a>visibility | <p align="center"> - </p> | <code>["//visibility:public"]</code> |
| <a id="spotbugs_binary-kwargs"></a>kwargs | <p align="center"> - </p> | none |



## Freezing Dependencies

Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ maven_install(
{
"group": "com.github.spotbugs",
"artifact": "spotbugs",
"version": "4.5.0",
"version": "4.5.3",
"exclusions": [
{
"group": "org.slf4j",
Expand Down
2 changes: 2 additions & 0 deletions docs/stardoc-input.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ load(
_java_test_suite = "java_test_suite",
_pmd_ruleset = "pmd_ruleset",
_pmd_test = "pmd_test",
_spotbugs_binary = "spotbugs_binary",
_spotbugs_config = "spotbugs_config",
_spotbugs_test = "spotbugs_test",
)
Expand All @@ -26,5 +27,6 @@ java_test = _java_test
java_test_suite = _java_test_suite
pmd_ruleset = _pmd_ruleset
pmd_test = _pmd_test
spotbugs_binary = _spotbugs_binary
spotbugs_config = _spotbugs_config
spotbugs_test = _spotbugs_test
50 changes: 25 additions & 25 deletions frozen_deps_install.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dependency_tree": {
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": -407317411,
"__RESOLVED_ARTIFACTS_HASH": -1818747059,
"__INPUT_ARTIFACTS_HASH": -407314528,
"__RESOLVED_ARTIFACTS_HASH": 752093506,
"conflict_resolution": {},
"dependencies": [
{
Expand All @@ -28,7 +28,7 @@
"url": "https://repo1.maven.org/maven2/com/beust/jcommander/1.48/jcommander-1.48.jar"
},
{
"coord": "com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.0",
"coord": "com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.3",
"dependencies": [
"com.google.code.findbugs:jsr305:jar:sources:3.0.2"
],
Expand All @@ -38,15 +38,15 @@
"exclusions": [
"org.slf4j:slf4j-api"
],
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0-sources.jar",
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0-sources.jar"
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3-sources.jar"
],
"sha256": "b338136e3e82d585348cde58a8fe3a678e16f51a35c31c1463e05fefef557aad",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0-sources.jar"
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3-sources.jar"
},
{
"coord": "com.github.spotbugs:spotbugs-annotations:4.5.0",
"coord": "com.github.spotbugs:spotbugs-annotations:4.5.3",
"dependencies": [
"com.google.code.findbugs:jsr305:3.0.2"
],
Expand All @@ -56,17 +56,17 @@
"exclusions": [
"org.slf4j:slf4j-api"
],
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0.jar",
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0.jar"
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3.jar"
],
"sha256": "65199ea3fe5dc4d106e30ebf67a92c7c816da816563d75c8ea6da1f0ff662857",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.0/spotbugs-annotations-4.5.0.jar"
"sha256": "cb8ef0e128fefc3885205b09a758bcc5aeca2c4faa205195a10d22301530e4f8",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs-annotations/4.5.3/spotbugs-annotations-4.5.3.jar"
},
{
"coord": "com.github.spotbugs:spotbugs:jar:sources:4.5.0",
"coord": "com.github.spotbugs:spotbugs:jar:sources:4.5.3",
"dependencies": [
"com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.0",
"com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.3",
"com.google.code.findbugs:jsr305:jar:sources:3.0.2",
"com.google.code.gson:gson:jar:sources:2.8.9",
"jaxen:jaxen:jar:sources:1.2.0",
Expand All @@ -83,7 +83,7 @@
"org.ow2.asm:asm:jar:sources:9.2"
],
"directDependencies": [
"com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.0",
"com.github.spotbugs:spotbugs-annotations:jar:sources:4.5.3",
"com.google.code.gson:gson:jar:sources:2.8.9",
"jaxen:jaxen:jar:sources:1.2.0",
"net.jcip:jcip-annotations:jar:sources:1.0",
Expand All @@ -101,17 +101,17 @@
"exclusions": [
"org.slf4j:slf4j-api"
],
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0-sources.jar",
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0-sources.jar"
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3-sources.jar"
],
"sha256": "4bf5daff85cf2e7fbc1b0950be87d7933f1bb80756cf9f55c534a63f07551eb9",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0-sources.jar"
"sha256": "196df4ebf7fb681cb819362461137d4e8b475b6a8229bb65363c7042924b702b",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3-sources.jar"
},
{
"coord": "com.github.spotbugs:spotbugs:4.5.0",
"coord": "com.github.spotbugs:spotbugs:4.5.3",
"dependencies": [
"com.github.spotbugs:spotbugs-annotations:4.5.0",
"com.github.spotbugs:spotbugs-annotations:4.5.3",
"com.google.code.findbugs:jsr305:3.0.2",
"com.google.code.gson:gson:2.8.9",
"jaxen:jaxen:1.2.0",
Expand All @@ -128,7 +128,7 @@
"org.ow2.asm:asm:9.2"
],
"directDependencies": [
"com.github.spotbugs:spotbugs-annotations:4.5.0",
"com.github.spotbugs:spotbugs-annotations:4.5.3",
"com.google.code.gson:gson:2.8.9",
"jaxen:jaxen:1.2.0",
"net.jcip:jcip-annotations:1.0",
Expand All @@ -146,12 +146,12 @@
"exclusions": [
"org.slf4j:slf4j-api"
],
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0.jar",
"file": "v1/https/repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0.jar"
"https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3.jar"
],
"sha256": "7063b740850a27bafbfd2d4528bec2faf2ebef9845a96efea47e15ccbc8a9317",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.0/spotbugs-4.5.0.jar"
"sha256": "7416dda5bf6f1e2740a906bc86b0db4c4413a039cc81d5a80a171d0996708745",
"url": "https://repo1.maven.org/maven2/com/github/spotbugs/spotbugs/4.5.3/spotbugs-4.5.3.jar"
},
{
"coord": "com.google.code.findbugs:jsr305:jar:sources:3.0.2",
Expand Down
7 changes: 3 additions & 4 deletions java/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//java/private:artifact.bzl", "artifact")
load("//java/private:checkstyle_config.bzl", "checkstyle_binary", "checkstyle_config")
load("//java/private:spotbugs_config.bzl", "spotbugs_config")
load("//java/private:spotbugs_config.bzl", "spotbugs_binary", "spotbugs_config")
load("//java/private:pmd_ruleset.bzl", "pmd_ruleset")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -45,12 +45,11 @@ spotbugs_config(
name = "spotbugs-default-config",
effort = "default",
fail_on_warning = True,
spotbugs_binary = ":spotbugs_cli",
)

java_binary(
spotbugs_binary(
name = "spotbugs_cli",
testonly = True,
main_class = "edu.umd.cs.findbugs.LaunchAppropriateUI",
runtime_deps = [
artifact("com.github.spotbugs:spotbugs"),
artifact("org.slf4j:slf4j-jdk14"),
Expand Down
6 changes: 5 additions & 1 deletion java/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ load(
load("//java/private:pmd.bzl", _pmd_test = "pmd_test")
load("//java/private:pmd_ruleset.bzl", _pmd_ruleset = "pmd_ruleset")
load("//java/private:spotbugs.bzl", _spotbugs_test = "spotbugs_test")
load("//java/private:spotbugs_config.bzl", _spotbugs_config = "spotbugs_config")
load(
"//java/private:spotbugs_config.bzl",
_spotbugs_binary = "spotbugs_binary",
_spotbugs_config = "spotbugs_config")

checkstyle_binary = _checkstyle_binary
checkstyle_config = _checkstyle_config
Expand All @@ -35,5 +38,6 @@ JUNIT5_DEPS = _JUNIT5_DEPS
JUNIT5_VINTAGE_DEPS = _JUNIT5_VINTAGE_DEPS
pmd_ruleset = _pmd_ruleset
pmd_test = _pmd_test
spotbugs_binary = _spotbugs_binary
spotbugs_config = _spotbugs_config
spotbugs_test = _spotbugs_test
Binary file modified java/private/contrib_rules_jvm_deps.zip
Binary file not shown.
37 changes: 9 additions & 28 deletions java/private/spotbugs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ Spotbugs integration logic
"""

def _spotbugs_impl(ctx):
# Preferred options: 1/ config on rule, 2/ config via attributes
if ctx.attr.config != None:
info = ctx.attr.config[SpotBugsInfo]
effort = info.effort
fail_on_warning = info.fail_on_warning
exclude_filter = info.exclude_filter
else:
effort = ctx.attr.effort
fail_on_warning = ctx.attr.fail_on_warning
exclude_filter = None
info = ctx.attr.config[SpotBugsInfo]
effort = info.effort
fail_on_warning = info.fail_on_warning
exclude_filter = info.exclude_filter

if ctx.attr.only_output_jars:
deps = []
Expand All @@ -28,7 +22,7 @@ def _spotbugs_impl(ctx):
else:
jars = depset(transitive = [target[JavaInfo].transitive_runtime_deps for target in ctx.attr.deps if JavaInfo in target]).to_list()

runfiles = [ctx.executable._spotbugs_cli]
runfiles = [info.binary]
flags = ["-textui", "-effort:%s" % effort]
if exclude_filter:
flags.extend(["-exclude", exclude_filter.short_path])
Expand All @@ -38,7 +32,7 @@ def _spotbugs_impl(ctx):
"#!/usr/bin/env bash",
"ERRORLOG=$(mktemp)",
"RES=`{lib} {flags} {jars} 2>$ERRORLOG`".format(
lib = ctx.executable._spotbugs_cli.short_path,
lib = info.binary.short_path,
flags = " ".join(flags),
jars = " ".join([jar.short_path for jar in jars]),
),
Expand Down Expand Up @@ -67,12 +61,12 @@ def _spotbugs_impl(ctx):

runfiles = ctx.runfiles(
files = jars + runfiles,
)
).merge(ctx.attr.config[DefaultInfo].default_runfiles)

return [
DefaultInfo(
executable = out,
runfiles = runfiles.merge(ctx.attr._spotbugs_cli[DefaultInfo].default_runfiles),
runfiles = runfiles,
),
]

Expand All @@ -84,28 +78,15 @@ spotbugs_test = rule(
allow_files = False,
),
"config": attr.label(
default = "//java:spotbugs-default-config",
providers = [
SpotBugsInfo,
],
),
"effort": attr.string(
doc = "Effort can be min, less, default, more or max. Defaults to default",
values = ["min", "less", "default", "more", "max"],
default = "default",
),
"only_output_jars": attr.bool(
doc = "If set to true, only the output jar of the target will be analyzed. Otherwise all transitive runtime dependencies will be analyzed",
default = True,
),
"fail_on_warning": attr.bool(
doc = "If set to true the test will fail on a warning, otherwise it will succeed but create a report. Defaults to True",
default = True,
),
"_spotbugs_cli": attr.label(
cfg = "host",
default = "//java:spotbugs_cli",
executable = True,
),
},
executable = True,
test = True,
Expand Down
Loading

0 comments on commit da93854

Please sign in to comment.