0

I downloaded from micronaut.lanch a simple grpc project and I can build it until I add Kotlin gRPC plugin.

Beyond the changes in build.gradle.kts, there is nothing else than the original scafolded project.

I edited in build.gradle.kts.

Firstly, in order to match IntelliJ Kotlin version

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.21"
    id("org.jetbrains.kotlin.kapt") version "1.4.21"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.21"

Seconddly, added folder for kotlin grpc proto autogenerated files

sourceSets {
    main {
        java {
...
            srcDirs ("build/generated/source/proto/main/grpckt")

And finally

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.14.0"
    }
    plugins {
        id("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:1.33.1"
        }
        id("grpckt")
            { artifact = "io.grpc:protoc-gen-grpc-kotlin:0.1.2" }
    }
    generateProtoTasks {
        ofSourceSet("main").forEach {
            it.plugins {
                // Apply the "grpc" plugin whose spec is defined above, without options.
                id("grpc")
                id("grpckt")
            }
        }
    }
}

The result while building either from INtelliJ or straigh from command line is

C:\_d\toLearn\kafka-proto\build\tmp\kapt3\stubs\main\com\test\KafkaProtoServiceGrpcKt.java:35: error: incompatible types: NonExistentClass cannot be converted to Annotation
    @error.NonExistentClass()

*** edited

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
        dependsOn ="generateProto" //how add this ?????
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

*** EDIT 2

I tried another project. I started from scratch and I am stuck on same issue again.

Here is the build.gradle

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
    id("com.google.protobuf") version "0.8.13"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

//https://stackoverflow.com/a/55646891/4148175
//kapt {
//    correctErrorTypes true
//}

dependencies {

    //implementation("io.grpc:protoc-gen-grpc-kotlin:1.0.0")

    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.grpc:micronaut-grpc-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut:micronaut-tracing")

    runtimeOnly("io.jaegertracing:jaeger-thrift")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
        dependsOn ':generateProto'
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

sourceSets {
    main {
        java {
            srcDirs("build/generated/source/proto/main/grpc")
            srcDirs 'build/generated/source/proto/main/grpckt'
            srcDirs("build/generated/source/proto/main/java")
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.14.0" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.33.1" }
        grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0:jdk7@jar"}
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            grpckt {}
        }
    }
}

The issue is raised on autogenerated stub

IntelliJ

Here is the full code github

Jim C
  • 3,957
  • 25
  • 85
  • 162
  • 1
    see that https://stackoverflow.com/questions/40499452/how-to-build-google-protocol-buffers-and-kotlin-using-gradle maybe it helps – IEE1394 Dec 08 '20 at 23:31
  • @IEE1394 do you know how I add compileKotlin.dependsOn ':generateProto' in my build.gradle.kts? I tried aboove and ccertainly the sintaxe is wrong – Jim C Dec 09 '20 at 08:43
  • 1
    have a look at that https://stackoverflow.com/a/55957803/7776688 – IEE1394 Dec 09 '20 at 21:08
  • @IEE1394, please, I am contely stuck on it. I tried other grpc project and falled in same issue. Please, how to add dependsOn ="generateProto" in my gradle? Is my issue somehow related to some kapt missed configration as declared in https://kotlinlang.org/docs/reference/kapt.html? – Jim C Dec 21 '20 at 01:10

1 Answers1

1

I fixed the issue by adding api("io.grpc:grpc-kotlin-stub:1.0.0") Thanks to https://github.com/grpc/grpc-kotlin/issues/220 help.

So my build.gradle is now:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
    id("com.google.protobuf") version "0.8.13"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

//https://stackoverflow.com/a/55646891/4148175
//https://kotlinlang.org/docs/reference/kapt.html
//kapt {
//    correctErrorTypes true
//    useBuildCache = false
//}

dependencies {

    implementation("io.grpc:protoc-gen-grpc-kotlin:1.0.0")
    api("io.grpc:grpc-kotlin-stub:1.0.0")

    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.grpc:micronaut-grpc-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut:micronaut-tracing")

    runtimeOnly("io.jaegertracing:jaeger-thrift")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
        dependsOn ':generateProto'
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

sourceSets {
    main {
        java {
            srcDirs("build/generated/source/proto/main/grpc")
            srcDirs 'build/generated/source/proto/main/grpckt'
            srcDirs("build/generated/source/proto/main/java")
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.14.0" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.33.1" }
        grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0:jdk7@jar"}
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            grpckt {}
        }
    }
}
Jim C
  • 3,957
  • 25
  • 85
  • 162