Explain rules_java module in Bazel with example
The rules_java
module in Bazel provides a set of rules for building Java projects. These rules allow you to compile Java code, run Java tests, create Java libraries, and package Java applications. The module encapsulates various tasks and workflows needed for Java development within the Bazel build system.
Key Components of rules_java
-
Java Library Rules:
java_library
: This rule is used to compile Java source files into a.jar
file. It can depend on otherjava_library
targets, and it can produce a library that other targets can depend on.java_library( name = "mylib", srcs = ["MyClass.java"], deps = [ "//path/to/other:library", ], )
-
Java Binary Rules:
java_binary
: This rule is used to create an executable Java application. It specifies the main class and includes all dependencies needed to run the application.java_binary( name = "myapp", srcs = ["MyApp.java"], main_class = "com.example.MyApp", deps = [ "//path/to/other:library", ], )
-
Java Test Rules:
java_test
: This rule is used to compile and run JUnit tests. It behaves similarly tojava_library
but is tailored for test code.java_test( name = "MyTest", srcs = ["MyTest.java"], deps = [ "//path/to/other:library", "@maven//:junit", ], )
-
Java Import Rules:
java_import
: This rule is used to bring existing.jar
files into the Bazel build. It's useful for including third-party libraries.java_import( name = "guava", jars = ["//path/to/guava-23.0.jar"], )
-
Java Plugin Rules:
java_plugin
: This rule defines a Java annotation processor. It can be used to generate code during the build process.java_plugin( name = "my_plugin", srcs = ["MyProcessor.java"], processor_class = "com.example.MyProcessor", )
-
Java Runtime Rules:
java_runtime
: This rule is used to define a Java runtime environment. It specifies the JDK to use for running Java binaries.java_runtime( name = "my_jdk", java_home = "/path/to/jdk", visibility = ["//visibility:public"], )
Additional Features and Integrations
- Dependency Management: The
rules_java
module integrates with dependency management systems like Maven, allowing you to include external libraries easily. - Customizable Builds: You can customize the behavior of the Java rules through various attributes and configuration options, tailoring the build process to your specific needs.
- Toolchains: Bazel's toolchain system can be used to configure and select different JDK versions or Java compilers, making it easy to switch between different Java environments.
- Bazel Extensions: The
rules_java
module can be extended with additional Bazel rules and macros, providing further customization and integration with other build tools and workflows.
Example Project Structure
Here’s an example of how a Bazel project using rules_java
might be structured:
/my_project
|-- WORKSPACE
|-- MODULE.bazel
|-- BUILD
|-- src
|-- main
|-- java
|-- com
|-- example
|-- MyApp.java
|-- test
|-- java
|-- com
|-- example
|-- MyTest.java
- WORKSPACE: The WORKSPACE file specifies the external dependencies and Bazel modules, including
rules_java
. - MODULE.bazel: The MODULE.bazel file is used to declare module dependencies and configure the module system.
- BUILD: The BUILD file defines the Bazel targets for the project, such as
java_library
,java_binary
, andjava_test
.
Example WORKSPACE File
workspace(name = "my_project")
bazel_dep(name = "rules_java", version = "7.6.3")
load("@rules_java//java:repositories.bzl", "rules_java_dependencies")
rules_java_dependencies()
Example BUILD File
load("@rules_java//java:defs.bzl", "java_library", "java_binary", "java_test")
java_library(
name = "mylib",
srcs = ["src/main/java/com/example/MyApp.java"],
deps = [
"//src/main/java/com/example:otherlib",
],
)
java_binary(
name = "myapp",
srcs = ["src/main/java/com/example/MyApp.java"],
main_class = "com.example.MyApp",
deps = [
":mylib",
],
)
java_test(
name = "mytest",
srcs = ["src/test/java/com/example/MyTest.java"],
deps = [
":mylib",
"@maven//:junit",
],
)
Published on: Jun 27, 2024, 02:59 AM