Skip to content

sgammon/rules_gradle

Repository files navigation

Gradle Rules for Bazel

Contributor Covenant Bazel 7 Bzlmod


Latest release: (None yet)

Important Currently in active development. Feedback welcome but will probably break your build.

Use Gradle from Bazel, with support for:

  • Obtaining outputs built by Gradle from a Bazel build
  • Building with Gradle within Bazel's sandbox
  • Sandboxed Gradle home and build directories
  • Provide sealed Maven dependency repositories
  • Bazel-provided distribution of Gradle

Installation

Via WORKSPACE.bazel:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
http_archive(
    name = "rules_gradle",
    sha256 = "...",
    strip_prefix = "rules_gradle-<revision>",
    urls = [
        "https://github.com/sgammon/rules_gradle/archive/<revision>/rules_gradle-<revision>.zip",
    ],
)
http_file(
    name = "gradle",
    sha256 = "591855b517fc635b9e04de1d05d5e76ada3f89f5fc76f87978d1b245b4f69225",
    url = "https://dl.less.build/toolchains/gradle/gradle-8.3-bin.zip",
)

You can use any Gradle toolchain download URL, including those provided by Gradle, in the form: https://services.gradle.org/distributions/gradle-<version>-<bin|all>.zip

Usage

In a BUILD.bazel file:

load("@rules_gradle//gradle:defs.bzl", "gradle_task")

filegroup(
    name = "files",
    srcs = [
        "build.gradle.kts",
        "settings.gradle.kts",
        "src/main/java/HelloWorld.java",
    ],
)

gradle_task(
    name = "project",
    outputs = [
        "libs/sample-project.jar",
    ],
    project = ":files",
)

Then, you can do:

bazel build //path/to/your:project

And obtain the outputs within the Bazel task graph:

BUILD SUCCESSFUL in 639ms
2 actionable tasks: 2 executed
Target //e2e/project:project up-to-date:
  bazel-bin/e2e/project/build
  bazel-bin/e2e/project/build/libs/sample-project.jar

INFO: Elapsed time: 58.984s, Critical Path: 36.79s
INFO: 395 processes: 44 internal, 331 darwin-sandbox, 20 worker.
INFO: Build completed successfully, 395 total actions

Roadmap

Stuff which is coming soon, time and APIs permitting, in rough order of priority:

  • Support for Windows
  • Support for Bzlmod
  • Extension to rules_jvm_external for easily installing Gradle plugins
  • Integration between Bazel's Java Toolchain and Gradle Toolchains
  • Stubbed Coursier-resolved repos
  • Dispatch of Gradle within the Bazel wrapper entrypoint
  • Analysis-time task graph integration (not sure this is even possible)
  • Gradle invocation with no daemon
  • Gradle plugin for tighter integration with Bazel
  • Support for Gradle invocations from Bazel persistent workers
  • Hand Gradle specific paths for toolchains (for instance, Java toolchain integration)
  • Native binary for wrapper (maybe)

About

Bazel → Gradle integration

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published