To build a gRPC service with Gradle, Kotlin, and Avro

You're absolutely right! The setSource method is not available in Kotlin DSL the same way it was referenced earlier. Let’s correct the Avro plugin configuration to make it work properly in a Kotlin build.gradle.kts. Below is the corrected version.


Corrected build.gradle.kts

plugins {
    java
    id("com.google.protobuf") version "0.9.1"
    id("com.github.davidmc24.gradle.plugin.avro") version "1.3.0"
}

group = "com.example"
version = "1.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.grpc:grpc-netty-shaded:1.56.0")
    implementation("io.grpc:grpc-protobuf:1.56.0")
    implementation("io.grpc:grpc-stub:1.56.0")
    implementation("org.apache.avro:avro:1.11.0")
    compileOnly("org.apache.avro:avro-compiler:1.11.0")
    testImplementation("junit:junit:4.13.2")
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.22.0"
    }
    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.56.0"
        }
    }
    generateProtoTasks {
        all().forEach { task ->
            task.plugins {
                create("grpc")
            }
        }
    }
}

avro {
    source = file("src/main/avro")  // Proper syntax for Kotlin DSL
    outputCharacterEncoding = "UTF-8" // Assign using `=`
}

tasks.test {
    useJUnitPlatform()
}

Changes and Fixes

  1. Avro Configuration:

    • In the Avro section, use source = file(...) instead of setSource(...). This aligns with the Kotlin DSL way of assigning values directly.
    • Use outputCharacterEncoding = "UTF-8" directly instead of set(...).
  2. Java Compatibility:

    • No changes required here. java.sourceCompatibility = JavaVersion.VERSION_11 remains valid.

Verify the Setup

To ensure everything works:

  1. Clean the build:

    ./gradlew clean
    
  2. Generate the Protobuf and Avro files:

    ./gradlew generateProto generateAvro
    
  3. Run the application:

    ./gradlew run
    

This version should now resolve the issue you encountered with setSource. Let me know if you encounter any further issues!

댓글

이 블로그의 인기 게시물

To switch to a specific tag in a Git repository

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

Using the MinIO API via curl

To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기

리눅스의 부팅과정 (프로세스, 서비스 관리)

Chromium 개발 환경 세팅, 크로미움 개발 준비하기

Joining an additional control plane node to an existing Kubernetes cluster

urllib3 with proxy settings

CDPEvents in puppeteer

Avro + Grpc in python