gradlekotlinktor

How to fix 'Neither port nor sslPort specified' throw by io.ktor.server.engine.CommandLineKt in a gradle/kotlin/netty project?


I created the following files based on the tutorial here: https://ktor.io/quickstart/quickstart/gradle.html

This is my kotlin file:

package blog

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*


fun Application.main() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
            call.respondText("My Example Blog2", ContentType.Text.Html)
        }
    }

}

and it is the content of my build.gradle file

group 'Example'
buildscript {
    ext.kotlin_version = '1.3.61'
    ext.ktor_version = '1.3.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'io.ktor.server.netty.EngineMain' 

sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlin {
    experimental {
        coroutines "enable"
    }
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "io.ktor:ktor-server-netty:$ktor_version"
    compile "ch.qos.logback:logback-classic:1.2.3"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

and application.conf

ktor {
    deployment {
        port = 8080
    }

    application {
        modules = [ blog.BlogAppKt.main ]
    }
}

All of them live in the same folder.

When I tried to run up the server by this command ./gradlew run, I got this error message:

> Task :run FAILED
Exception in thread "main" java.lang.IllegalArgumentException: Neither port nor sslPort specified. Use command line options -port/-sslPort or configure connectors in application.conf
        at io.ktor.server.engine.CommandLineKt$commandLineEnvironment$environment$1.invoke(CommandLine.kt:129)
        at io.ktor.server.engine.CommandLineKt$commandLineEnvironment$environment$1.invoke(CommandLine.kt)
        at io.ktor.server.engine.ApplicationEngineEnvironmentBuilder.build(ApplicationEngineEnvironment.kt:105)
        at io.ktor.server.engine.ApplicationEngineEnvironmentKt.applicationEngineEnvironment(ApplicationEngineEnvironment.kt:44)
        at io.ktor.server.engine.CommandLineKt.commandLineEnvironment(CommandLine.kt:57)
        at io.ktor.server.netty.EngineMain.main(EngineMain.kt:21)

How can I make application.conf visible to the Netty process in this case?


Solution

  • I need to add this to the build.gradle file.

    Basically I need to explicitly tell gradle where are my source and resource files.

    sourceSets {
        main.kotlin.srcDirs = [ 'src/main/kotlin' ]
        main.resources.srcDirs = [ 'src/main/resource' ]
    }