kaptKotlin not recognizing micronaut options - kotlin

I'm trying to setup a micronaut project.
The project is using Gradle and Kotlin.
I'm using the Micronaut CLI to generate the project, but after the initial setup with the CLI I'm getting this warning:
> Task :kaptKotlin
warning: The following options were not recognized by any processor: '[micronaut.processing.incremental, micronaut.processing.module, kapt.kotlin.generated, micronaut.processing.annotations]'
The following options were not recognized by any processor: '[micronaut.processing.incremental, micronaut.processing.module, kapt.kotlin.generated, micronaut.processing.annotations]'
I've used this command to setup the project:
mn create-app com.my.example --build=gradle --lang=kotlin
build.gradle file:
plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.32"
id("org.jetbrains.kotlin.kapt") version "1.4.32"
id("com.github.johnrengelman.shadow") version "7.0.0"
id("io.micronaut.application") version "1.5.3"
id("org.jetbrains.kotlin.plugin.allopen") version "1.4.32"
}
version = "0.1"
group = "com.my"
repositories {
mavenCentral()
}
micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.my.*")
}
}
dependencies {
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("javax.annotation:javax.annotation-api")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
runtimeOnly("ch.qos.logback:logback-classic")
implementation("io.micronaut:micronaut-validation")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
application {
mainClass.set("com.my.ApplicationKt")
}
java {
sourceCompatibility = JavaVersion.toVersion("14")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
}
Micronaut version: 2.5.8
Java version: 14
Gradle version: 7.0
Kotlin version: 1.4.32
Anyone knows what could be the problem?

Related

How to generate Schema?

this is my gradle ,I use kotlin plus graphql
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.5"
id("io.spring.dependency-management") version "1.0.15.RELEASE"
id("com.apollographql.apollo3").version("3.7.0")
kotlin("jvm") version "1.6.21"
kotlin("plugin.spring") version "1.6.21"
}
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("com.apollographql.apollo3:apollo-runtime:3.7.0")
implementation("org.reactivestreams:reactive-streams:1.0.3")
runtimeOnly("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.6.0")
}
apollo {
packageName.set("com.example")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I go generateServiceApolloSchema
and in schema.graphqls file I see this error
Could not find a schema, make sure to add dependencies to the apolloSchema configuration
I have json file under src/main/graphql

How to fix `Expression 'jar' cannot be invoked as a function` error in gradle file?

I have the following gradle file. I'm getting a Expression 'jar' cannot be invoked as a function error on the line where jar is used (jar {). How can I fix this?
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.21"
java
application
}
group = "me.talha"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
application {
mainClass.set("MainKt")
}
jar {
manifest {
attributes 'Main-Class': 'MainKt'
}
}
Turns out I did not need to add any of the jar stuff. I can simply use the gradle distZip task which generates the needed jars and executable scripts and all works fine.

Page not found message using micronaut, Gradle and Kotlin

I just started using Micronaut, but I can't get a simple endpoint to work. I get this response for http://localhost:8901/game_compositions/
{
"message": "Page Not Found",
"_links": {
"self": {
"href": "/game_compositions/",
"templated": false
}
}
}
I have seen a few other questions here about the same response but none of them helped me. I have annotation processor activated for my build and I do not return null.
I assume I have some simple stupid mistake, but I can't find it. According to the sample projects I seem to have all I need.
This is my project:
Controller:
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
#Controller("/game_compositions")
class CompositionsController {
#Produces(MediaType.APPLICATION_JSON)
#Get(uri = "/")
fun getCompositions(): Iterable<String> {
return listOf("something")
}
}
Build.gradle (mostly autogenerated from the https://micronaut.io/launch/ site, so there might be some unnecessary stuff there for a simple service. Feel free to point it out)
plugins {
id("io.micronaut.application") version "1.2.0"
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
kotlin("plugin.allopen") version "1.4.10"
}
version = "0.1"
group = "com.example.backend"
val kotlinVersion=project.properties.get("kotlinVersion")
repositories {
mavenCentral()
jcenter()
}
micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.example.backend.*")
}
}
dependencies {
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("io.micronaut.sql:micronaut-hibernate-jpa")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
application {
mainClass.set("com.example.backend.ApplicationKt")
}
java {
sourceCompatibility = JavaVersion.toVersion("14")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
}
Application.yml (the database connection is so far unused untested)
micronaut:
application:
name: Backend
server:
port: 8901
datasources:
default:
url: jdbc:mysql://localhost:3306/db
username: root
password: root
driver-class-name: org.mariadb.jdbc.Driver
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: none
show_sql: true
Thanks to Jeff Scott Brown in the comments I have been able to determine that the order of the plugins is the problem. I was not aware that the order mattered in gradle. Apparently Micronaut requires the kapt plugin to be added first. Anyone with more insight in to this is welcome to post a better answer, for me it worked to change the start of my gradle file to this:
plugins {
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
id("io.micronaut.application") version "1.2.0"
kotlin("plugin.allopen") version "1.4.10"
}
Edit: An important note if anyone attempts to use my gradle file. It turns out kapt does not support java 14 and that caused some very hard to track down trouble for me. This is my current working gradle file:
plugins {
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
kotlin("plugin.allopen") version "1.4.10"
kotlin("plugin.jpa") version "1.4.10"
id("io.micronaut.application") version "1.2.0"
}
version = "0.1"
group = "com.example.backend"
val kotlinVersion: String by project
repositories {
mavenCentral()
jcenter()
}
micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.example.backend.*")
}
}
dependencies {
annotationProcessor("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("io.micronaut.sql:micronaut-hibernate-jpa")
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
application {
mainClass.set("com.example.backend.ApplicationKt")
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
}

Kotlin-gRPC: error: incompatible types: NonExistentClass cannot be converted to Annotation #error.NonExistentClass()

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
Here is the full code github
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 {}
}
}
}

Kotlin: How to create a runnable jar?

I'm trying to create a runnable jar with Kotlin.
My gradle.build is this:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
(As it stands, when I do ./gradlew run, everything works as expected.)
I'm reading an article here on how to proceed, and it says to do: java -jar <MY_PROJECT_NAME>.jar.
I don't quite understand this -- where do we run this? I tried running it from my project root and I got an error:
Error: Unable to access jarfile <my_jarname>.jar
As of Gradle 5.4.1, a build.gradle.kts would need a section like this:
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
manifest {
attributes(
"Main-Class" to "mytest.AppKt",
"Implementation-Title" to "Gradle",
"Implementation-Version" to archiveVersion
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
Ok, I figured it out :)
So, the way to create a jar is to go: ./gradlew build. This creates a jar in build/libs.
The problem is, when running that jar, one would run into an exception about java.lang.intrinsics because the kotlin stdlib hasn't been packed into the jar.
While there is a way to manually accomplish that, I found the easiest solution is to simply use the shadowjar plugin.
My build.gradle ended up looking like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
apply plugin: 'java'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
jcenter()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
apply plugin: 'com.github.johnrengelman.shadow'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
When using a Kotlin main class you need to add a Kt at the end of the class name while referencing it on MANIFEST.
So, if your main class is called interpreter.Repl, use:
jar {
manifest {
attributes 'Main-Class': 'interpreter.ReplKt'
}
}
instead of
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}