WAR

note

WAR - 图1

This help topic is in development and will be updated in the future.

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:

WAR - 图2

Xml

Groovy

WAR - 图3

  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>

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

note

WAR - 图4

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