Home  Bazel   Explain rul ...

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

  1. Java Library Rules:

    • java_library: This rule is used to compile Java source files into a .jar file. It can depend on other java_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",
          ],
      )
      
  2. 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",
          ],
      )
      
  3. Java Test Rules:

    • java_test: This rule is used to compile and run JUnit tests. It behaves similarly to java_library but is tailored for test code.
      java_test(
          name = "MyTest",
          srcs = ["MyTest.java"],
          deps = [
              "//path/to/other:library",
              "@maven//:junit",
          ],
      )
      
  4. 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"],
      )
      
  5. 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",
      )
      
  6. 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

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

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  
 

Comments

Add your comment