ktor execute jar file - kotlin

I want make a jar file from my ktor project.
here is my main function
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}.start(wait = true)
}
and here is my build.gradle
plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.6.20'
}
group "com.example"
version "0.0.1"
mainClassName = "com.example.ApplicationKt"
def isDevelopment = project.ext.has("development")
applicationDefaultJvmArgs = ["-Dio.ktor.development=$isDevelopment"]
application {
mainClass.set("io.ktor.server.netty.EngineMain")
}
repositories {
mavenCentral()
maven { url "https://maven.pkg.jetbrains.space/public/p/ktor/eap" }
}
dependencies {
implementation "io.ktor:ktor-server-core-jvm:$ktor_version"
implementation "io.ktor:ktor-server-netty-jvm:$ktor_version"
implementation "ch.qos.logback:logback-classic:$logback_version"
testImplementation "io.ktor:ktor-server-tests-jvm:$ktor_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
and here is my settings.gradle
rootProject.name = "ktor-sample"
when I try to run ./gradlew :ktor-sample:installDist I get this error
What went wrong:
Project 'ktor-sample' not found in root project 'ktor-sample'.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

I guess you don't have a subproject named ktor-sample so just run ./gradlew installDist.

Related

Can't implement "Ktor-server-auth " library in my ktor project

I am trying to implement a ktor auth library to authenticate user .The library is
implementation("io.ktor:ktor-server-auth:$ktor_version") but it gives me error as follows :
Execution failed for task ':compileKotlin'.
> Could not resolve all files for configuration ':compileClasspath'.
> Could not resolve io.ktor:ktor-server-auth:1.5.1.
Required by:
project :
> Could not resolve io.ktor:ktor-server-auth:1.5.1.
> Could not get resource 'https://kotlin.bintray.com/ktor/io/ktor/ktor-server-auth/1.5.1/ktor-server-auth-1.5.1.pom'.
> Could not HEAD 'https://kotlin.bintray.com/ktor/io/ktor/ktor-server-auth/1.5.1/ktor-server-auth-1.5.1.pom'.
> Certificate for <kotlin.bintray.com> doesn't match any of the subject alternative names: [*.jfrog.io, *.int.jfrog.io, jfrog.io, *.pe.jfrog.io]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
This is my build.gradle file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
group 'com.example'
version '0.0.1'
mainClassName = "io.ktor.server.netty.EngineMain"
sourceSets {
main.kotlin.srcDirs = main.java.srcDirs = ['src']
test.kotlin.srcDirs = test.java.srcDirs = ['test']
main.resources.srcDirs = ['resources']
test.resources.srcDirs = ['testresources']
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url = uri('https://kotlin.bintray.com/ktor') }
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
maven {
url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:$logback_version"
implementation "io.ktor:ktor-server-core:$ktor_version"
implementation "io.ktor:ktor-freemarker:$ktor_version"
implementation "io.ktor:ktor-server-host-common:$ktor_version"
testImplementation "io.ktor:ktor-server-tests:$ktor_version"
implementation("org.jetbrains.exposed:exposed:0.12.1")
// H2 in-memory database
implementation("com.h2database:h2:1.4.191")
// Exposed depends on Joda-time so include it
implementation("joda-time:joda-time:2.9.2")
implementation 'com.google.code.gson:gson:2.8.5'
//session
implementation("io.ktor:ktor-server-sessions:$ktor_version")
// Auth
implementation "io.ktor:ktor-server-auth:$ktor_version"
}
gradle.properties
ktor_version=1.5.1
kotlin.code.style=official
kotlin_version=1.4.21
logback_version=1.2.1
exposed_version = 0.41.1
h2_version = 2.1.214
what is the problem here? The official documentation link is : https://ktor.io/docs/authentication.html I followed it but failed.

Cannot access org.apache.hc.client5.http.utils.URLEncodedUtils in kotlin build script

Why is org.apache.hc unresolved considering the dependency has been added?
package com.blah
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
// Error seen when import line is uncommented:
// 'Unresolved reference: hc'
// import org.apache.hc.client5.http.utils.URLEncodedUtils <--------
import org.w3c.dom.Element
import org.xml.sax.InputSource
import java.io.FileWriter
import java.io.StringReader
import java.net.URI
import java.util.*
val projectVersion: String by project
val liquibaseVersion: String = "4.13.0"
val liquibaseGradlePluginVersion: String = "2.0.4"
plugins {
java
id("org.springframework.boot") apply false
id("org.liquibase.gradle") apply true
}
dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
liquibaseRuntime(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.postgresql:postgresql")
implementation("org.liquibase:liquibase-core:$liquibaseVersion")
implementation("org.liquibase:liquibase-gradle-plugin:$liquibaseGradlePluginVersion")
implementation("org.hibernate:hibernate-core")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("com.fasterxml.jackson.core:jackson-core")
implementation("com.fasterxml.jackson.core:jackson-databind")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
// No errors when building the Gradle model during import into IDE
// https://mvnrepository.com/artifact/org.apache.httpcomponents.core5/httpcore5
implementation("org.apache.httpcomponents.core5:httpcore5:5.2.1")
liquibaseRuntime("org.liquibase:liquibase-core:$liquibaseVersion")
liquibaseRuntime("org.postgresql:postgresql")
liquibaseRuntime("org.springframework.boot:spring-boot-starter-data-jpa")
liquibaseRuntime(sourceSets.getByName("main").output)
}
Dependencies declared in the dependencies {} block are added to the classpath of the project, not the classpath of the buildscript.
If you want to use dependencies in a Gradle build script, then there are two options
Use the buildscript {} block in build.gradle(.kts)
// build.gradle.kts
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.apache.httpcomponents.core5:httpcore5:5.2.1")
}
}
plugins {
java
id("org.springframework.boot") apply false
id("org.liquibase.gradle") apply true
}
dependencies {
// project dependencies...
}
Define dependencies in an included build, or buildSrc
// ./buildSrc/build.gradle.kts
plugins {
`kotlin-dsl` // a Kotlin or Java plugin is required, so that 'implementation' is available
}
dependencies {
// dependencies defined here will be available in all of the project's build scripts
implementation("org.apache.httpcomponents.core5:httpcore5:5.2.1")
}

Kotlin Could not find or load main class when deploy on heroku

I using intellij and wanted to deploy my kotlin project on cloud but got some error when deployed.
2022-03-14T14:20:36.467566+00:00 app[web.1]: Error: Could not find or load main class src.main.kotlin.mainKt
can someone help me with this error, this is my gradle config.
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.5.10'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven{ url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
apply plugin: "kotlin"
task stage {
dependsOn build
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation("dev.kord:kord-core:0.7.4")
implementation('org.slf4j:slf4j-simple:1.7.36')
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "dev.inmo:krontab:0.7.1"
}
jar {
manifest {
attributes 'Main-Class' : 'src.main.kotlin.mainKt'
}
from { configurations.runtimeClasspath.findAll { it.isDirectory() ? it : zipTree(it) } }
}
and this is my directory structure
src ----> main ----> kotlin ----> main.kt
build.gradle

Could not initialize class org.jetbrains.kotlin.gradle.internal.KotlinSourceSetProviderImplKt by running gradle run via cmd

I have a question about gradle:
I Have the following code in Kotlin (just started to learn it):
#file:JvmName("KtlTest")
fun main(args: Array<String>)
{
println("Hello Kotlin !")
}
and the following build.gradle:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.51'
}
group 'KGroupId'
version '1.0-SNAPSHOT'
ext.kotlin_version = '{{ site.data.releases.latest.version }}'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'io.rest-assured:rest-assured:4.3.1'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
task runExample(type: JavaExec) {
main = 'KtlTest'
classpath = sourceSets.main.runtimeClasspath
}
If I run this via IntelliJIDEA by clicking green triangle near task - it runs ok.
But, if I run it via other -> runExample in Gradle tab in IntelliJIDEA I got this error:
Also, I got this error if I try to run it via CMD:
Could someone help me figure out what the problem is?

Kotlin. Micronaut. TestNG test fails in IDEA but pass via gradle

I have created sample micronaut project with simple testNG test based on this instruction.
The test works perfectly when I run it via gradle from command line, but fails when I try to run it from IDEA.
For successful gradle run in logs I see routing for my controller:
13:40:44.314 [Test worker] DEBUG i.m.web.router.DefaultRouteBuilder - Created Route: GET /hello -> HelloController#String index() (application/json )
which is missing for IDEA run.
QUESTION: What do I need to configure to make the test also pass in IDEA?
sources
src/main/kotlin/example/micronaut/Application.kt:
package example.micronaut
import io.micronaut.runtime.Micronaut
object Application {
#JvmStatic
fun main(args: Array<String>) {
Micronaut.build()
.packages("example.micronaut")
.mainClass(Application.javaClass)
.start()
}
}
src/main/kotlin/example/micronaut/HelloController.kt:
package example.micronaut
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
#Controller("/hello")
class HelloController {
#Get("/")
fun index(): String {
return "Hello World"
}
}
src/test/kotlin/example/micronaut/HelloTest.kt:
package example.micronaut
import io.micronaut.context.ApplicationContext
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
class HelloTest {
lateinit var embeddedServer: EmbeddedServer
lateinit var client: HttpClient
#BeforeMethod
fun init() {
embeddedServer = ApplicationContext.run(EmbeddedServer::class.java)
client = HttpClient.create(embeddedServer.url)
}
#Test
fun check() {
val response: String = client.toBlocking().retrieve("/hello")
assert(response == "Hello World")
}
#AfterMethod
fun cleanup() {
client.close()
embeddedServer.close()
}
}
build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
classpath "io.spring.gradle:dependency-management-plugin:1.0.5.RELEASE"
classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
}
}
version "0.1"
group "example"
apply plugin:"io.spring.dependency-management"
apply plugin:"com.github.johnrengelman.shadow"
apply plugin:"application"
apply plugin:"java"
apply plugin:"net.ltgt.apt-eclipse"
apply plugin:"net.ltgt.apt-idea"
apply plugin:"kotlin"
apply plugin:"kotlin-kapt"
repositories {
mavenLocal()
mavenCentral()
maven { url "https://jcenter.bintray.com" }
}
dependencyManagement {
imports {
mavenBom 'io.micronaut:bom:1.0.0.M1'
}
}
dependencies {
annotationProcessor "io.micronaut:inject-java"
compile "io.micronaut:http-client"
compile "io.micronaut:http-server-netty"
compile "io.micronaut:inject"
compile "io.micronaut:runtime"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.31"
compile "org.jetbrains.kotlin:kotlin-reflect:1.2.31"
compileOnly "io.micronaut:inject-java"
kapt "io.micronaut:inject-java"
runtime "ch.qos.logback:logback-classic:1.2.3"
testCompile "io.micronaut:inject-java"
testImplementation("org.testng:testng:6.13.1")
}
shadowJar {
mergeServiceFiles()
}
mainClassName = "example.micronaut.Application"
compileJava.options.compilerArgs += '-parameters'
compileTestJava.options.compilerArgs += '-parameters'
test {
useTestNG()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Solved
fixed by setting gradle as test runner as described in http://mrhaki.blogspot.com/2016/03/gradle-goodness-configure-intellij-idea.html
Currently a build tool is required to build Kotlin + Micronaut applications until IntelliJ brings native support for Kapt. This is explained here https://docs.micronaut.io/latest/guide/index.html#kotlin in the section "Kotlin, Kapt and IntelliJ"