milkcoke / java-gRPC-practice

This is practicce for gRPC Java + SpringBoot
0 stars 0 forks source link

SpringBoot 3.x #3

Open milkcoke opened 1 year ago

milkcoke commented 1 year ago

SpringBoot 3.x version makes use of Jakarta EE 9 APIs (jakarta.) instead of EE 8 (javax.) \ However, io.grpc still uses javax.* So there's error when building as shwon below.

Problem

error: cannot find symbol Task : compileJava FAILED

BankServiceGrpc.java

// ❌ Error occurs here.
@javax.annotation.Generated(
    value = "by gRPC proto compiler (version 1.52.1)",
    comments = "Source: bank-service.proto")
@io.grpc.stub.annotations.GrpcGenerated
public final class BankServiceGrpc {
// ..
}
milkcoke commented 1 year ago

First try

I applied OpenRewrite library But it doesn't resolve this problem. Still io-grpc generate javax.annotation.x .

plugins {
    id("org.openrewrite.rewrite") version("5.34.0")
}

rewrite {
    activeRecipe("org.openrewrite.java.migrate.jakarta.JavaxAnnotationMigrationToJakartaAnnotation")
}

repositories {
    mavenCentral()
}

dependencies {
    rewrite("org.openrewrite.recipe:rewrite-migrate-java:1.16.0")
}
milkcoke commented 1 year ago

OpenRewrite can't enforce for third party libary to use another annotation.

Second try

Configure annotation processors supported by IntelliJ

annotation_processing_image

build.gradle

buildscript {
    ext {
        protobufVersion = '3.21.12'
        protobufPluginVersion = '1.52.1'
        grpcVersion = '1.52.1'
    }
}

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.2'
    id 'io.spring.dependency-management' version '1.1.0'
    // The Protobuf plugin provides protobuf compilation to your project
    id 'com.google.protobuf' version '0.9.2'
    id 'org.openrewrite.rewrite' version '5.34.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '18'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1'
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'net.devh:grpc-spring-boot-starter:2.14.0.RELEASE'

    implementation "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    // stub is a client-side component in gRPC that provides a simple wy to make remote procedure calls to a server.
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    implementation "io.grpc:grpc-netty:${grpcVersion}"
    implementation "com.google.protobuf:protobuf-java:${protobufVersion}"
    implementation 'com.fasterxml.jackson.core:jackson-core:2.14.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2'

    rewrite "org.openrewrite.recipe:rewrite-migrate-java:1.16.0"

    compileOnly 'org.projectlombok:lombok'
    compileOnly 'jakarta.annotation:jakarta.annotation-api:2.1.1'
    annotationProcessor 'org.projectlombok:lombok'
        // jakarta annotation processor.
    annotationProcessor 'jakarta.annotation:jakarta.annotation-api:2.1.1'
    testImplementation 'org.projectlombok:lombok:1.18.22'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

// sourceSets is a configuration that sources of project (target, generated)
sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

rewrite {
    activeRecipe(
            "org.openrewrite.java.migrate.jakarta.JavaxAnnotationMigrationToJakartaAnnotation",
    )
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${protobufVersion}"
    }

// Deprecated!
//  generatedFilesBaseDir = "$projectDir/src/generated"
    clean {
        delete generatedFilesBaseDir
    }

    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${protobufPluginVersion}"
        }
    }

    generateProtoTasks {
        all()*.plugins {
            grpc {option 'jakarta.annotation=true'}
        }
    }
}

Still doesn't work.