如何解决gradle / kotlin / netty项目中io.ktor.server.engine.CommandLineKt抛出的“既未指定端口也未指定sslPort”的问题?

安东尼·孔

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

这是我的kotlin文件:

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

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

安东尼·孔

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档