### Setup rules_jvm Dependencies
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/README.md
Load and initialize the necessary rules_jvm dependencies in your WORKSPACE file.
```starlark
load("@contrib_rules_jvm//:repositories.bzl", "contrib_rules_jvm_deps", "contrib_rules_jvm_gazelle_deps")
contrib_rules_jvm_deps()
contrib_rules_jvm_gazelle_deps()
load("@contrib_rules_jvm//:setup.bzl", "contrib_rules_jvm_setup")
contrib_rules_jvm_setup()
load("@contrib_rules_jvm//:gazelle_setup.bzl", "contrib_rules_jvm_gazelle_setup")
contrib_rules_jvm_gazelle_setup()
```
--------------------------------
### Build Gazelle Binary
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/README.md
Build the gazelle binary to verify your setup.
```bash
bazel build //:gazelle_bin
```
--------------------------------
### Configure Java Maven Install File
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/README.md
Specify the path to the Maven install file using a Gazelle comment in your root BUILD.bazel file.
```starlark
# gazelle:java_maven_install_file project/main_maven_install.json
```
--------------------------------
### Load contrib_rules_jvm Dependencies and Setup
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/preamble.md
Use this snippet in your WORKSPACE file to load necessary dependencies and set up contrib_rules_jvm. Ensure you have already used an http_archive for the ruleset.
```starlark
load("@contrib_rules_jvm//:repositories.bzl", "contrib_rules_jvm_deps")
contrib_rules_jvm_deps()
load("@contrib_rules_jvm//:setup.bzl", "contrib_rules_jvm_setup")
contrib_rules_jvm_setup()
```
--------------------------------
### Define Maven Install for Frozen Dependencies
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
Configure a maven_install rule to manage your project's dependencies. Ensure 'fail_if_repin_required' is set to True and 'maven_install_json' points to the correct JSON file. This rule is referenced when creating the frozen dependency zip.
```starlark
maven_install(
name = "frozen_deps",
artifacts = [...],
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@workspace//:frozen_deps_install.json",
)
load(@frozen_deps//:defs.bzl, "pinned_maven_install")
pinned_maven_install()
```
--------------------------------
### Configure Linting with apple_rules_lint
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/preamble.md
Integrate linting checks into your Bazel build by configuring apple_rules_lint. This example shows how to set up mappings for common Java linters like Checkstyle, PMD, and SpotBugs.
```starlark
# In your WORKSPACE, after loading `apple_rules_lint`
load("@apple_rules_lint//lint:setup.bzl", "lint_setup")
lint_setup({
# Note: this is an example config!
"java-checkstyle": "@contrib_rules_jvm//java:checkstyle-default-config",
"java-pmd": "@contrib_rules_jvm//java:pmd-config",
"java-spotbugs": "@contrib_rules_jvm//java:spotbugs-default-config",
})
```
--------------------------------
### Update Gradle Wrapper
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/comparative-tests/README.md
Update the Gradle wrapper script to the latest version. This ensures consistency and avoids requiring users to install Gradle separately.
```bash
./gradlew wrapper --gradle-version latest
```
--------------------------------
### Define Java Library and Export Targets
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/testdata/java_export_targets/README.md
Defines a Java library and two java_export targets that export the same library. This setup is used to test how Gazelle handles duplicate exports.
```starlark
java_library(name = "A")
java_export(name = "X", exports = [":B"])
java_export(name = "Y", exports = [":B"])
java_library(name = "B")
```
--------------------------------
### Load Compat Repositories
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
After enabling compat repositories generation, load them using the provided bzl file. This makes the compat repositories available for use in your build.
```starlark
load(@workspace_deps//:compat.bzl, "compat_repositories")
compat_repositories()
```
--------------------------------
### Generate Compat Repositories
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
If your rules depend on older compat repositories, enable `generate_compat_repositories = True` in the `maven_install` rule and load the `compat_repositories` macro from the frozen dependency repository.
```starlark
load(@workspace_deps//:compat.bzl, "compat_repositories")
compat_repositories()
```
--------------------------------
### Configure SpotBugs
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Sets up configuration for SpotBugs static analysis. Includes options for effort level, exclude filters, failure on warning, plugin list, and the SpotBugs binary.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "spotbugs_config")
spotbugs_config(name, effort, exclude_filter, fail_on_warning, plugin_list, spotbugs_binary)
```
--------------------------------
### Run Tests with Gradle
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/comparative-tests/README.md
Execute tests using Gradle. Ensure you are in the `comparative-tests` directory. This command utilizes the Gradle wrapper script.
```bash
./gradlew test
```
--------------------------------
### Run Tests with Bazel
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/comparative-tests/README.md
Execute tests using Bazel. This command targets all tests within the `comparative-tests` directory.
```bash
bazel test //comparative-tests/...
```
--------------------------------
### Create Java Binary Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Adds linting tests to a standard Bazel `java_binary` target. Configure with name and other keyword arguments.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "java_binary")
java_binary(name, kwargs)
```
--------------------------------
### Generate BUILD Files
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/README.md
Run the Gazelle binary to generate BUILD files for your Java project.
```bash
# Run Gazelle with the java extension
bazel run //:gazelle
```
--------------------------------
### Enable Compat Repositories Generation
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
To generate compat_repositories for dependencies, add 'generate_compat_repositories = True' to the original maven_install rule. This is necessary when your rule depends on another rule still using older compat repositories.
```starlark
maven_install(
name = "frozen_deps",
artifacts = [...],
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@workspace//:frozen_deps_install.json",
generate_compat_repositories = True, # Add this line
)
```
--------------------------------
### Configure Zip Repository for Frozen Dependencies
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
Use the 'zip_repository' rule to load your frozen dependencies from a zip file. The 'path' attribute should point to the location of the dependency zip file. This makes the dependencies available under their own repository namespace.
```starlark
maybe(
zip_repository,
name = "workspace_deps",
path = "@workspace//path/to:dependency.zip",
)
```
--------------------------------
### Create a Java Test Suite
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Generates a `java_test` for each `*Test.java` file matching `test_suffixes`. It also creates a `test_suite` for running all tests together. Specify `runner` as `junit4` or `junit5`.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "java_test_suite")
java_test_suite(name, srcs, runner, test_suffixes, package, deps, runtime_deps, size, kwargs)
```
--------------------------------
### Define spotbugs_binary Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use this macro to create a `java_binary` target for use with `spotbugs_config`. At least one of `runtime_deps`, `deps`, or `srcs` must be specified.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "spotbugs_binary")
potbugs_binary(
name = "spotbugs_cli",
runtime_deps = [
artifact("com.github.spotbugs:spotbugs"),
artifact("org.slf4j:slf4j-jdk14"),
],
)
```
--------------------------------
### Define Frozen Dependencies with maven_install
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use `maven_install` to declare your Java dependencies. Set `fail_if_repin_required` to `True` to ensure pinned versions. The `maven_install_json` attribute specifies the output file for dependency information.
```starlark
maven_install(
name = "frozen_deps",
artifacts = [...],
fail_if_repin_required = True,
fetch_sources = True,
maven_install_json = "@workspace//:frozen_deps_install.json",
)
load(@frozen_deps//:defs.bzl, "pinned_maven_install")
pinned_maven_install()
```
--------------------------------
### Load Pinned Dependencies from Frozen Repository
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
After configuring the frozen dependencies with `zip_repository`, load the `pinned_maven_install` macro from the new repository to make them available for use.
```starlark
load(@workspace_deps//:defs.bzl, "pinned_maven_install")
pinned_maven_install()
```
--------------------------------
### Pin and Load Frozen Dependencies
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
After configuring the zip repository, pin and load the frozen dependencies. This makes them available for use in your Bazel build targets.
```starlark
load(@workspace_deps//:defs.bzl, "pinned_maven_install")
pinned_maven_install()
```
--------------------------------
### Run Tests with Maven
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/comparative-tests/README.md
Execute tests using Maven. Ensure you are in the `comparative-tests` directory. The `--fail-at-end` flag ensures all tests run before reporting failures.
```bash
mvn --fail-at-end test
```
--------------------------------
### Configure Gazelle with Java Extension
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/java/gazelle/README.md
Configure Gazelle in your top-level BUILD.bazel file to include the Java extension. Ensure the gazelle:prefix comment is set.
```starlark
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
# gazelle:prefix github.com/your/project
gazelle(
name = "gazelle",
gazelle = ":gazelle_bin",
)
gazelle_binary(
name = "gazelle_bin",
languages = DEFAULT_LANGUAGES + [
"@contrib_rules_jvm//java/gazelle",
],
)
```
--------------------------------
### Run PMD Test
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Executes PMD to lint specified source files. Requires a name, source files, and a ruleset label. An optional target can also be specified.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "pmd_test")
pmd_test(name, srcs, ruleset, target)
```
--------------------------------
### Configure Frozen Dependencies with zip_repository
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Integrate the frozen dependency zip file into your Bazel workspace using `zip_repository`. The `path` attribute should point to the committed zip file. This makes the dependencies available under the specified repository name.
```starlark
maybe(
zip_repository,
name = "workspace_deps",
path = "@workspace//path/to:dependency.zip",
)
```
--------------------------------
### Configure PMD Ruleset
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Defines a PMD ruleset for static analysis. Specify name, format, PMD binary, rulesets to use, and whether to perform shallow analysis.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "pmd_ruleset")
pmd_ruleset(name, format, pmd_binary, rulesets, shallow)
```
--------------------------------
### Create Checkstyle Binary Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Generates a `java_binary` target for Checkstyle. Useful for creating a custom Checkstyle executable. Ensure at least one of `runtime_deps`, `deps`, or `srcs` is provided.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_binary")
checkstyle_binary(name, main_class, deps, runtime_deps, srcs, visibility, kwargs)
```
```starlark
checkstyle_binary(
name = "checkstyle_cli",
runtime_deps = [
artifact("com.puppycrawl.tools:checkstyle"),
]
)
```
--------------------------------
### Use Frozen Dependencies in java_library
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Reference the frozen dependencies in your `java_library` rules using their repository-qualified names. This ensures your Java code uses the pinned versions from the frozen zip file.
```starlark
java_library(
name = "my_library",
srcs = glob(["*.java"]),
deps = [
"@workspace_deps//:my_frozen_dep",
],
)
```
--------------------------------
### Generate PMD Binary Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
A macro for creating a `java_binary` target compatible with `pmd_ruleset`. It defaults to PMD's main class but can be overridden. At least one of `runtime_deps`, `deps`, or `srcs` must be provided.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "pmd_binary")
pmd_binary(name, main_class, deps, runtime_deps, srcs, visibility, kwargs)
```
```starlark
pmd_binary(
name = "pmd",
runtime_deps = [
artifact("net.sourceforge.pmd:pmd-dist"),
],
)
```
--------------------------------
### pmd_test
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Creates a Bazel test target that uses PMD to lint specified source files.
```APIDOC
## pmd_test
### Description
Use PMD to lint the `srcs`.
### Attributes
- **name** (Name) - Required - A unique name for this target.
- **srcs** (List of labels) - Optional - Source files to lint. Default: `[]`
- **ruleset** (Label) - Required - The PMD ruleset to apply.
- **target** (Label) - Optional - The target to lint. Default: `None`
```
--------------------------------
### Define Java Library Rule
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use `java_library` to add linting tests to Bazel's standard `java_library` rule. Specify the name and any additional keyword arguments.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "java_library")
java_library(name, kwargs)
```
--------------------------------
### Configure JUnit5 Test Runner
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use `java_junit5_test` to run JUnit5 tests with Bazel. It serves as a replacement for `java_test` and supports JUnit5 directly. Configure test class, runtime dependencies, package prefixes, JVM flags, and tag/engine inclusions/exclusions.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "java_junit5_test")
java_junit5_test(name, test_class, runtime_deps, package_prefixes, jvm_flags, include_tags,
exclude_tags, include_engines, exclude_engines, kwargs)
```
--------------------------------
### Create SpotBugs Test Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Defines a Bazel target to run SpotBugs for code analysis. Configure with name, dependencies, and optional config or output jar settings.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "spotbugs_test")
spotbugs_test(name, deps, config, only_output_jars)
```
--------------------------------
### Create Checkstyle Test Target
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Defines a Bazel target to run Checkstyle tests. Customizable with name, size, and timeout settings.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_test")
checkstyle_test(name, size, timeout, kwargs)
```
--------------------------------
### Format Starlark files with buildifier
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/CONTRIBUTING.md
Use buildifier to format Starlark files. This command applies fixes and lints the code recursively.
```sh
buildifier --mode fix -lint fix -r .
```
--------------------------------
### java_library
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Adds linting tests to Bazel's `java_library` rule.
```APIDOC
## java_library
### Description
Adds linting tests to Bazel's own `java_library`.
### Parameters
- **name** (string) - Required - The name of the library rule.
- **kwargs** (dict) - Optional - Additional keyword arguments.
```
--------------------------------
### java_junit5_test
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Runs JUnit5 tests using Bazel. Designed as a replacement for `java_test` with JUnit5 support.
```APIDOC
## java_junit5_test
### Description
Run junit5 tests using Bazel. This rule is a drop-in replacement for `java_test`, providing support for JUnit5 directly.
### Parameters
- **name** (string) - Required - The name of the test.
- **test_class** (string) - Optional - The Java class to be loaded by the test runner. Defaults to `None`.
- **runtime_deps** (list) - Optional - Runtime dependencies for the test. Defaults to `[]`.
- **package_prefixes** (list) - Optional - Prefixes for packages to include in the test run. Defaults to `[]`.
- **jvm_flags** (list) - Optional - JVM flags to apply to the test execution. Defaults to `[]`.
- **include_tags** (list) - Optional - JUnit5 tag expressions to include specific tests. Defaults to `[]`.
- **exclude_tags** (list) - Optional - JUnit tag expressions to exclude specific tests. Defaults to `[]`.
- **include_engines** (list) - Optional - A list of JUnit Platform test engine IDs to include. Defaults to `[]`.
- **exclude_engines** (list) - Optional - A list of JUnit Platform test engine IDs to exclude. Defaults to `[]`.
- **kwargs** (dict) - Optional - Additional keyword arguments.
```
--------------------------------
### Use Frozen Dependencies in Java Library
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/docs/postfix.md
Reference the frozen dependencies from your 'java_library' rules using their repository-qualified names. This ensures your library uses the pinned versions of its dependencies.
```starlark
java_library(
name = "my_library",
srcs = glob(["*.java"]),
deps = [
"@workspace_deps//:my_frozen_dep",
],
)
```
--------------------------------
### Define a Java Test Rule
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use this rule to add linting tests to Bazel's own `java_test` targets. It requires a name and accepts additional keyword arguments.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "java_test")
java_test(name, kwargs)
```
--------------------------------
### Configure Checkstyle Rule
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use the `checkstyle_config` rule to configure Checkstyle, typically for use with linting rules. Specify the config file and optionally the Checkstyle binary and output format.
```starlark
load("@contrib_rules_jvm//java:defs.bzl", "checkstyle_config")
checkstyle_config(name, data, checkstyle_binary, config_file, output_format)
```
--------------------------------
### Override repository for local development
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/CONTRIBUTING.md
Configure Bazel to use the local rules_jvm directory instead of a fetched version. Add these lines to your ~/.bazelrc.user file to ensure all usages of @contrib_rules_jvm point to your local development copy.
```sh
OVERRIDE="--override_repository=contrib_rules_jvm=$(pwd)/rules_jvm"
echo "build $OVERRIDE" >> ~/.bazelrc.user
echo "fetch $OVERRIDE" >> ~/.bazelrc.user
echo "query $OVERRIDE" >> ~/.bazelrc.user
```
--------------------------------
### spotbugs_config
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Configures SpotBugs, a static analysis tool for finding bugs in Java code. This configuration is typically used by linting rules.
```APIDOC
## spotbugs_config
### Description
Configuration used for spotbugs, typically by the `//lint` rules.
### Attributes
- **name** (Name) - Required - A unique name for this target.
- **effort** (String) - Optional - Effort can be min, less, default, more or max. Defaults to default. Default: `"default"`
- **exclude_filter** (Label) - Optional - Report all bug instances except those matching the filter specified by this filter file. Default: `None`
- **fail_on_warning** (Boolean) - Optional - Whether to fail on warning, or just create a report. Defaults to True. Default: `True`
- **plugin_list** (List of labels) - Optional - Specify a list of plugin Jar files to load. Default: `[]`
- **spotbugs_binary** (Label) - Optional - The spotbugs binary to run. Default: `"@contrib_rules_jvm//java:spotbugs_cli"`
```
--------------------------------
### pmd_ruleset
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Defines a ruleset for PMD, a static analysis tool for Java. It allows selection of rule sets and configuration of the PMD binary.
```APIDOC
## pmd_ruleset
### Description
Select a rule set for PMD tests.
### Attributes
- **name** (Name) - Required - A unique name for this target.
- **format** (String) - Optional - Generate report in the given format. One of html, text, or xml (default is xml). Default: `"xml"`
- **pmd_binary** (Label) - Optional - PMD binary to use. Default: `"@contrib_rules_jvm//java:pmd"`
- **rulesets** (List of labels) - Optional - Use these rulesets. Default: `[]`
- **shallow** (Boolean) - Optional - Use the targetted output to increase PMD's depth of processing. Default: `True`
```
--------------------------------
### Define Java Export Rule
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Use `java_export` to add linting tests to `rules_jvm_external`'s Java exports. Specify the name, Maven coordinates, POM template, deployment environment, visibility, and other keyword arguments.
```bazel
load("@contrib_rules_jvm//java:defs.bzl", "java_export")
java_export(name, maven_coordinates, pom_template, deploy_env, visibility, kwargs)
```
--------------------------------
### checkstyle_config
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Rule allowing checkstyle to be configured. This is typically used with the linting rules from @apple_rules_lint to configure how checkstyle should run.
```APIDOC
## checkstyle_config
### Description
Rule allowing checkstyle to be configured. This is typically used with the linting rules from `@apple_rules_lint` to configure how checkstyle should run.
### Method
`load(@contrib_rules_jvm//java:defs.bzl, "checkstyle_config")`
`checkstyle_config(name, data, checkstyle_binary, config_file, output_format)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Parameters
- **name** (Name) - Required - A unique name for this target.
- **data** (List of labels) - Optional - Additional files to make available to Checkstyle such as any included XML files. Defaults to `[]`.
- **checkstyle_binary** (Label) - Optional - Checkstyle binary to use. Defaults to `"@contrib_rules_jvm//java:checkstyle_cli"`.
- **config_file** (Label) - Required - The config file to use for all checkstyle tests.
- **output_format** (String) - Optional - Output format to use. Defaults to `"plain"`.
### Request Example
None
### Response
#### Success Response (200)
None
#### Response Example
None
```
--------------------------------
### java_export
Source: https://github.com/bazel-contrib/rules_jvm/blob/main/README.md
Adds linting tests to the java_export rule. This rule is used for exporting Java artifacts.
```APIDOC
## java_export
### Description
Adds linting tests to `rules_jvm_external`'s `java_export`.
### Parameters
- **name** (string) - Required - The name of the export rule.
- **maven_coordinates** (string) - Required - Maven coordinates for the exported artifact.
- **pom_template** (string) - Optional - Template for the POM file. Defaults to `None`.
- **deploy_env** (string) - Optional - Deployment environment configuration. Defaults to `None`.
- **visibility** (list) - Optional - Visibility settings for the rule. Defaults to `None`.
- **kwargs** (dict) - Optional - Additional keyword arguments.
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.