如何修复 gradle/kotlin/netty 项目中 io.ktor.server.engine.CommandLineKt 抛出的 'Neither port nor sslPort specified'?

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

我根据这里的教程创建了以下文件:https://ktor.io/quickstart/quickstart/gradle.html

这是我的科特林文件:

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)
        }
    }

}

这是我的 build.gradle 文件的内容

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'
}

和application.conf

ktor {
    deployment {
        port = 8080
    }

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

他们都在同一个文件夹中。

当我尝试通过此命令 运行 启动服务器时 ./gradlew run,我收到此错误消息:

> 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.invoke(CommandLine.kt:129)
        at io.ktor.server.engine.CommandLineKt$commandLineEnvironment$environment.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)

在这种情况下,如何使 application.conf 对 Netty 进程可见?

我需要将其添加到 build.gradle 文件中。

基本上我需要明确地告诉 gradle 我的源文件和资源文件在哪里。

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