WAR (Servlet Container)

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:

webapp/WEB-INF/web.xml

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <!-- path to application.conf file, required -->
  7. <!-- note that this file is always loaded as an absolute path from the classpath -->
  8. <context-param>
  9. <param-name>io.ktor.ktor.config</param-name>
  10. <param-value>application.conf</param-value>
  11. </context-param>
  12. <servlet>
  13. <display-name>KtorServlet</display-name>
  14. <servlet-name>KtorServlet</servlet-name>
  15. <servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>
  16. <!-- required! -->
  17. <async-supported>true</async-supported>
  18. <!-- 100mb max file upload, optional -->
  19. <multipart-config>
  20. <max-file-size>304857600</max-file-size>
  21. <max-request-size>304857600</max-request-size>
  22. <file-size-threshold>0</file-size-threshold>
  23. </multipart-config>
  24. </servlet>
  25. <servlet-mapping>
  26. <servlet-name>KtorServlet</servlet-name>
  27. <url-pattern>/</url-pattern>
  28. </servlet-mapping>
  29. </web-app>

build.gradle

  1. buildscript {
  2. ext.gretty_version = '2.0.0'
  3. repositories {
  4. jcenter()
  5. }
  6. dependencies {
  7. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  8. classpath "org.akhikhl.gretty:gretty:$gretty_version"
  9. }
  10. }
  11. apply plugin: 'kotlin'
  12. apply plugin: 'war'
  13. apply plugin: 'org.akhikhl.gretty'
  14. webAppDirName = 'webapp'
  15. gretty {
  16. contextPath = '/'
  17. logbackConfigFile = 'resources/logback.xml'
  18. }
  19. sourceSets {
  20. main.kotlin.srcDirs = [ 'src' ]
  21. main.resources.srcDirs = [ 'resources' ]
  22. }
  23. repositories {
  24. jcenter()
  25. }
  26. dependencies {
  27. compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  28. compile "io.ktor:ktor-server-servlet:$ktor_version"
  29. compile "io.ktor:ktor-html-builder:$ktor_version"
  30. compile "ch.qos.logback:logback-classic:$logback_version"
  31. }
  32. kotlin.experimental.coroutines = 'enable'
  33. task run
  34. afterEvaluate {
  35. run.dependsOn(tasks.findByName("appRun"))
  36. }

This gradle buildscript defines several tasks thatyou 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/master/deployment/jetty-war