forked from ktorio/ktor-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaven.xml
198 lines (184 loc) · 8.27 KB
/
Maven.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic SYSTEM "https://resources.jetbrains.com/stardust/html-entities.dtd">
<topic xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/stardust/topic.v2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
title="Adding Ktor to an existing Maven project"
id="Maven">
<microformat>
<p>
<control>Final project</control>: <a href="https://github.com/ktorio/ktor-maven-sample/tree/main">ktor-maven-sample</a>
</p>
</microformat>
<p>
In this tutorial, we'll show you how to integrate Ktor into the existing Maven project.
</p>
<chapter title="Prerequisites" id="prerequisites">
<p>
Before starting this tutorial, do the following:
</p>
<list>
<li>
<p>
<a href="https://www.jetbrains.com/help/idea/installation-guide.html">Install IntelliJ IDEA</a>.
</p>
</li>
<li>
<p>
Make sure the <control>Kotlin</control> plugin is <a href="https://www.jetbrains.com/help/idea/managing-plugins.html#open-plugin-settings">installed and enabled</a>.
</p>
</li>
<li>
<p>
Make sure the <control>Maven</control> and <control>Maven Extension</control> plugins are <a href="https://www.jetbrains.com/help/idea/managing-plugins.html#open-plugin-settings">installed and enabled</a>.
</p>
</li>
</list>
</chapter>
<chapter title="Create a new Maven project" id="create-new-maven-project">
<p>
To create a new Maven project,
<a href="https://www.jetbrains.com/help/idea/run-for-the-first-time.html">open IntelliJ IDEA</a>, and follow
the steps below:
</p>
<procedure hide-from-structure="true">
<step>
<include src="lib.xml" include-id="new_project_idea"/>
</step>
<step>
<p>
In the <control>New Project</control> wizard, choose <control>Kotlin</control> from the list on the left and specify the following settings:
</p>
<img src="ktor_idea_new_maven_project_settings.png" alt="Gradle Project Settings" width="861"/>
<list>
<li>
<p>
Specify a project's <control>Name</control>.
</p>
</li>
<li>
<p>
Set <control>Project Template</control> to <control>Application</control>.
</p>
</li>
<li>
<p>
Select <control>Maven</control> as a build system.
</p>
</li>
<li>
<p>
Choose the <control>Project JDK</control> from the list.
</p>
</li>
</list>
<p>
Click <control>Next</control>.
</p>
</step>
<step>
<p>
On the next wizard page, leave the default settings and click <control>Finish</control>.
</p>
<img src="ktor_idea_new_maven_project_name.png" alt="Gradle Project Name" width="861"/>
<p>
Wait until IntelliJ IDEA creates and builds a project.
</p>
</step>
</procedure>
</chapter>
<chapter title="Add Ktor dependencies" id="add-ktor-dependencies">
<p>
After <a anchor="create-new-maven-project">creating an empty project</a>, we are ready to examine a Maven configuration file and add Ktor dependencies to it:
</p>
<procedure hide-from-structure="true">
<step>
<p>
Open the <path>pom.xml</path> file. It should look something like this:
</p>
<code style="block" lang="XML"
src="snippets/_misc/maven-initial.pom.xml"
interpolate-variables="true"/>
<p>
This configuration file includes the <a href="https://kotlinlang.org/docs/maven.html">kotlin-maven-plugin</a> for compiling Kotlin sources.
Moreover, it declares the Maven Central repository in the <control>repositories</control> block.
Since Ktor dependencies are stored in Maven Central, we don't need to change anything here.
</p>
</step>
<step>
<p>
To create a simple Ktor application, we need to add at least the following dependencies:
</p>
<list>
<li>
<p>
<code>ktor-server-core</code>: contains core Ktor functionality.
</p>
</li>
<li>
<p>
A dependency for an <a href="Engines.md">engine</a> (for example, <code>ktor-server-netty</code>).
</p>
</li>
<li>
<p>
Logback artifacts for <a href="logging.md">logging</a>.
</p>
</li>
</list>
<p>
The <code>properties</code> and <code>dependencies</code> blocks might look as follows:
</p>
<code style="block" lang="XML"
src="snippets/_misc/maven-ktor.pom.xml"
interpolate-variables="true"/>
<p>
Note that other engines and <a href="Plugins.md">plugins</a> that extend Ktor functionality might require additional dependencies.
You can learn more from corresponding topics.
</p>
</step>
<step>
<p>
The resulting <path>pom.xml</path> file should look as shown below.
</p>
<code style="block" lang="XML"
src="snippets/_misc/maven-final.pom.xml"
interpolate-variables="true"/>
<tip>
<p>
To import the added dependencies and update a project, you can click <a href="https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html#maven_import_dependency">Load Maven Changes</a> at the right part of the editor.
</p>
</tip>
</step>
</procedure>
</chapter>
<include src="server-dependencies.xml" include-id="create-server"/>
<chapter title="Run an application" id="run-app">
<p>
Now we can run our Ktor application:
</p>
<procedure>
<step>
<p>
To run the application, click the gutter icon next to the <code>main</code> function
and choose <control>Run 'ApplicationKt'</control>.
</p>
<img src="ktor_idea_new_project_run_gutter.png" alt="Run a Ktor application" width="706"/>
</step>
<step>
<p>
Wait until Intellij IDEA runs the application.
The Run tool window should show the following message:
</p>
<code style="block">
[main] INFO Application - Responding at http://0.0.0.0:8080
</code>
<!-- <img src="ktor_idea_new_project_run_tool_window.png" alt="Run tool window" width="1000"/>-->
<p>
This means that the server is ready to accept requests at the <path>http://0.0.0.0:8080</path> address.
You can click this link to open the application in a default browser:
</p>
<img src="ktor_idea_new_project_browser.png" alt="Ktor app in a browser" width="430"/>
</step>
</procedure>
</chapter>
</topic>