WAR (Servlet Container)

预计阅读时间: 3 分钟

A WAR archive allows you to easily deploy your application inside your web container / servlet container, by just copying it to its webapps folder. Ktor supports two popular servlet containers: Jetty and Tomcat. It also serves when deploying to google app engine.

To generate a war file, you can use the gretty gradle plugin. You also need a WEB-INF/web.xml which looks like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    <!-- path to application.conf file, required -->
    <!-- note that this file is always loaded as an absolute path from the classpath -->

        <!-- required! -->

        <!-- 100mb max file upload, optional -->


buildscript {
    ext.gretty_version = '2.0.0'

    repositories {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.akhikhl.gretty:gretty:$gretty_version"

apply plugin: 'kotlin'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'

webAppDirName = 'webapp'

gretty {
    contextPath = '/'
    logbackConfigFile = 'resources/logback.xml'

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

repositories {

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "io.ktor:ktor-server-servlet:$ktor_version"
    compile "io.ktor:ktor-html-builder:$ktor_version"
    compile "ch.qos.logback:logback-classic:$logback_version"

kotlin.experimental.coroutines = 'enable'

task run

afterEvaluate {

This gradle buildscript defines several tasks that you can use to run your application.

In the case where you only need to generate a war file, there is a war task defined in the war plugin.
Just run ./gradlew war and it will generate a /build/libs/projectname.war file.

For a full example: https://github.com/ktorio/ktor-samples/tree/1.3.0/deployment/jetty-war