技術関連の覚書

案件でやったり自宅で試したことの覚書

Springboot+kotlinでgRPCを動かすための準備

https://start.spring.io/

f:id:boctok-ctpoba:20201110183932p:plain
Spring Initializrの設定内容

qiitaやブログを観るとSpring web starterをdependenciesに追加するように書いてあるけど、なかったのでSpring Webを追加。

追加した内容

build.gradle.kts

import を2個追加

import com.google.protobuf.gradle.*
import org.gradle.kotlin.dsl.provider.gradleKotlinDslOf

pluginsに追加

   id("com.google.protobuf") version "0.8.8"

sourceSetsを追加 ここに自動生成したコードを出力する

sourceSets{
    create("kotlingrpc"){
        proto{
            srcDir("src/main/kotlin/protobuf")
        }
    }
}

dependenciesに追加 protobuf-gradle-pluginにある examples/exampleKotlinDslProject/build.gradle.kts を参照して追加。 元はcompileを使用しているが、非推奨なのでimplementationに変更

   implementation("com.google.protobuf:protobuf-java:3.6.1")
    implementation("io.grpc:grpc-stub:1.15.1")
    implementation("io.grpc:grpc-protobuf:1.15.1")

protobufを追加

protobuf{
    protoc{
        artifact = "com.google.protobuf:protoc:3.6.1"
    }
    plugins{
        id("grpc"){
            artifact = "io.grpc:protoc-gen-grpc-java:1.15.1"
        }
    }
    generateProtoTasks{
        ofSourceSet("main").forEach{
            it.plugins{
                id("grpc")
            }
        }
    }
}

これだけ設定すれば後は./gradlew generateProto を実行すればコードを生成してくれるはず。

gradlewの実行 IntelliJのターミナルから実行したけど、OSのターミナルからでも同じ。 Windowsの場合はgradlew.bat を実行する 順番は、generateProto、compileKotlinの順

protoファイルを別途管理

protoファイル管理プロジェクトを作成する タグ付けする protodep.tomlファイルを作る protodepを実行

user@localhost:~/IdeaProjects/kotlingrpc$ ./gradlew generateProto
Starting a Gradle Daemon, 1 busy and 1 incompatible Daemons could not be reused, use --status for details

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s
3 actionable tasks: 1 executed, 2 up-to-date
user@localhost:~/IdeaProjects/kotlingrpc$ ./gradle compileKotlin
bash: ./gradle: ディレクトリです
masataka@akagi:~/IdeaProjects/kotlingrpc$ ./gradlew compileKotlin
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s
4 actionable tasks: 4 up-to-date
user@localhost:~/IdeaProjects/kotlingrpc$