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" ?>

3. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6. version="3.0">
7. <!-- path to application.conf file, required -->
8. <!-- note that this file is always loaded as an absolute path from the classpath -->
9. <context-param>
10. <param-name>io.ktor.ktor.config</param-name>
11. <param-value>application.conf</param-value>
12. </context-param>

14. <servlet>
15. <display-name>KtorServlet</display-name>
16. <servlet-name>KtorServlet</servlet-name>
17. <servlet-class>io.ktor.server.servlet.ServletApplicationEngine</servlet-class>

19. <!-- required! -->
20. <async-supported>true</async-supported>

22. <!-- 100mb max file upload, optional -->
23. <multipart-config>
24. <max-file-size>304857600</max-file-size>
25. <max-request-size>304857600</max-request-size>
26. <file-size-threshold>0</file-size-threshold>
27. </multipart-config>
28. </servlet>

30. <servlet-mapping>
31. <servlet-name>KtorServlet</servlet-name>
32. <url-pattern>/</url-pattern>
33. </servlet-mapping>

35. </web-app>

build.gradle

1. buildscript {
2. ext.gretty_version = '2.0.0'

4. repositories {
5. jcenter()
6. }
7. dependencies {
8. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
9. classpath "org.akhikhl.gretty:gretty:$gretty_version"
10. }
11. }

13. apply plugin: 'kotlin'
14. apply plugin: 'war'
15. apply plugin: 'org.akhikhl.gretty'

17. webAppDirName = 'webapp'

19. gretty {
20. contextPath = '/'
21. logbackConfigFile = 'resources/logback.xml'
22. }

24. sourceSets {
25. main.kotlin.srcDirs = [ 'src' ]
26. main.resources.srcDirs = [ 'resources' ]
27. }

29. repositories {
30. jcenter()
31. }

33. dependencies {
34. compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
35. compile "io.ktor:ktor-server-servlet:$ktor_version"
36. compile "io.ktor:ktor-html-builder:$ktor_version"
37. compile "ch.qos.logback:logback-classic:$logback_version"
38. }

40. kotlin.experimental.coroutines = 'enable'

42. task run

44. afterEvaluate {
45. run.dependsOn(tasks.findByName("appRun"))
46. }

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>