Integrate with other tools

Gradle

Buf provides official support for the Gradle build tool with the Buf Gradle Plugin, which enables you to:

Setup

Gradle setup

To get started, you'll need to add the Buf Gradle plugin:

build.gradle.kts
plugins {
  id("build.buf") version "0.8.4"
}

Buf project setup

For a basic project or one that uses the protobuf-gradle-plugin, create a Buf configuration file. You can either do this in the project directory:

buf.yaml
version: v1
lint:
  ignore:
    - path/to/dir/to/ignore
  use:
    - DEFAULT

Or you can specify the location of buf.yaml by configuring the extension:

buf {
    configFileLocation = rootProject.file("buf.yaml")
}

Buf Gradle tasks

Once the plugin is set up, you can run the following tasks through Gradle (via ./gradlew or gradle):

  • bufFormatApply: Applies Buf's formatter to Protobuf code.
  • bufFormatCheck: Validates Protobuf code using Buf's formatter.
  • bufLint: Lints Protobuf code.
  • bufBreaking: Checks a Protobuf schema against a previous version for backwards-incompatible changes.
  • bufGenerate: Generates code stubs from your Protobuf schema.

Format checks

bufFormatApply is run manually and has no configuration.

bufFormatCheck is run automatically during the check task if enforceFormat is enabled. It has no other configuration.

buf {
    enforceFormat = true // true by default
}

Linting

bufLint is configured by thebuf.yaml files in your project or workspace. It's run automatically during the check task.

Breaking change detection

bufBreaking is more complicated since it requires a previous version of the Protobuf schema to validate the current version. Buf's built-in Git integration isn't quite enough since it requires a buildable Protobuf source set and the protobuf-gradle-plugin's extraction step typically targets the project build directory, which is ephemeral and not committed.

This plugin uses buf build to create an image from the current Protobuf schema and then publishes it as a Maven publication. In subsequent builds of the project, the plugin will resolve the previously published schema image and run buf breaking against the current schema with the image as its reference.

Checking against the latest published version

Enable checkSchemaAgainstLatestRelease and the plugin will resolve the previously published Maven artifact as its input for validation.

For example, first publish the project with publishSchema enabled:

buf {
    publishSchema = true
}

Then configure the plugin to check the schema:

buf {
    // continue to publish schema
    publishSchema = true

    checkSchemaAgainstLatestRelease = true
}

The plugin will run Buf to check the project's current schema:

> Task :bufBreaking FAILED
src/main/proto/buf/service/test/test.proto:9:1:Previously present field "1" with name "test_content" on message "TestMessage" was deleted.

Checking against a static version

If you don't want to dynamically check against the latest published version of your schema, you can specify a version with previousVersion:

buf {
    // continue to publish schema
    publishSchema = true

    // will always check against version 0.1.0
    previousVersion = "0.1.0"
}

Artifact details

By default, the published image artifact will infer its details from an existing Maven publication if one exists. If one doesn't exist, you have more than one, or you'd like to specify the details yourself, you can configure them:

buf {
    publishSchema = true

    imageArtifact {
        groupId = rootProject.group.toString()
        artifactId = "custom-artifact-id"
        version = rootProject.version.toString()
    }
}

Generating code

bufGenerate is configured as described in the Buf docs. Create a buf.gen.yaml in the project root and bufGenerate will generate code in the project's build directory at "$buildDir/bufbuild/generated/<out path from buf.gen.yaml>".

An example for Java code generation using the remote plugin:

version: v1
plugins:
  - plugin: buf.build/protocolbuffers/java
    out: java

To use generated code in your build, you must add the generated code as a source directory and configure a task dependency to ensure code is generated before compilation:

build.gradle.kts
import build.buf.gradle.BUF_GENERATED_DIR

plugins {
    `java`
    id("build.buf") version "<version>"
}

// Add a task dependency for compilation
tasks.named("compileJava").configure { dependsOn("bufGenerate") }

// Add the generated code to the main source set
sourceSets["main"].java { srcDir("$buildDir/$BUF_GENERATED_DIR/java") }

// Configure dependencies for protobuf-java:
repositories { mavenCentral() }

dependencies {
    implementation("com.google.protobuf:protobuf-java:<protobuf version>")
}

Generating dependencies

If you'd like to generate code for your dependencies, configure the bufGenerate task:

build.gradle.kts
buf {
    generate {
        includeImports = true
    }
}

Ensure you have an up-to-date buf.lock file generated by buf mod update or this generation will fail.

Further configuration

By default, bufGenerate will read the buf.gen.yaml template file from the project root directory. You can override the location of the template file:

build.gradle.kts
buf {
    generate {
        templateFileLocation = rootProject.file("subdir/buf.gen.yaml")
    }
}

Specifying the Buf version

You can configure the version of Buf used by setting the toolVersion property on the plugin:

buf {
    toolVersion = "<version>"
}