DEV Community

Cover image for Creating a Ktor Server with Gradle and SDKMAN!: A Step-by-Step Guide
Cherif Bouchelaghem
Cherif Bouchelaghem

Posted on

Creating a Ktor Server with Gradle and SDKMAN!: A Step-by-Step Guide

Ktor, a powerful web framework built with Kotlin, offers a lightweight and flexible solution for building web applications. In this article, we will guide you through the process of creating a Ktor project manually using Gradle and SDKMAN!.
By following the steps below, you'll have a basic Ktor project up and running in no time.

Prerequisites:

Before getting started, make sure you have the following installed on your machine:

  • Java Development Kit (JDK)
  • SDKMAN! (Software Development Kit Manager)

Step 1: Install SDKMAN!

SDKMAN! is a convenient tool for managing multiple software development kits, including Gradle. Follow the installation instructions provided at https://sdkman.io/install to install SDKMAN! on your machine.

Step 2: Install Gradle using SDKMAN!

Once SDKMAN! is installed, open a new terminal or command prompt window and run the following command to install Gradle:

sdk install gradle
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize a New Project Directory

Create a new directory for your Ktor project. Choose a suitable name for your project and navigate to the project directory in your terminal or command prompt.

Step 4: Initialize a Gradle Project

Inside the project directory, run the following command to initialize a new Gradle project:

gradle init --type kotlin-application
Enter fullscreen mode Exit fullscreen mode

This command initializes a new Gradle project with Kotlin support, creating the necessary project structure which looks similar to the following:

.
└── the_app_folder/
    ├── .gradle
    ├── app/
    │   ├── src/
    │   │   ├── main
    │   │   └── test
    │   └── build.gradle.kts
    ├── gradle
    ├── .gitattribute
    ├── .gitignore
    ├── gradlew
    ├── gradlew.bat
    └── settings.gradle.kts
        └── settings.gradle.kts
Enter fullscreen mode Exit fullscreen mode

Step 5: Configure the Gradle Build

Open the build.gradle.kts (or build.gradle) file in a text editor. Modify the file to include the necessary dependencies and plugins for Ktor. Here's an example of a basic configuration:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.5.1/userguide/building_java_projects.html
 */

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.8.21"

    // Apply the application plugin to add support for building a CLI application in Java.
    application
    // add ktor gradle plugin
    id("io.ktor.plugin") version "2.3.2"
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // This dependency is used by the application.
    implementation("com.google.guava:guava:31.0.1-jre")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")

    // add ktor dependencies
    implementation("io.ktor:ktor-server-core")
    implementation("io.ktor:ktor-server-netty")
    implementation("ch.qos.logback:logback-classic:1.2.11")
}

application {
    // Define the main class for the application.
    mainClass.set("<your-package-name>.AppKt")
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Create the Application Entry Point

Create a new Kotlin file in the src/main/kotlin directory with the name Application.kt. Inside the file, add the following code:

/*
 * This Kotlin source file was generated by the Gradle 'init' task.
 */
package sdkman.gradle.ktor.tut

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main() {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                call.respondText("Hello, Ktor!")
            }
        }
    }.start(wait = true)
}
Enter fullscreen mode Exit fullscreen mode

This code sets up a simple Ktor application with a single route that responds with "Hello, Ktor!" when accessing the root URL.

Step 7: Run the Application

In the terminal or command prompt, navigate to the project directory and run the following command to start the Ktor application:

gradle run
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully created a Ktor project using Gradle and SDKMAN!. Your Ktor application is now running and accessible at http://localhost:8080. Feel free to explore and customize the project based on your specific requirements.

Conclusion:

In this article, we walked through the step-by-step process of creating a Ktor project manually using Gradle and SDKMAN!. By leveraging SDKMAN!, you can easily manage your Gradle installation, ensuring you have the latest stable version. Combine Gradle's powerful build capabilities with Ktor's flexible features to build robust and scalable web applications with ease. Happy coding with Ktor, Gradle, and SDKMAN!!

Top comments (0)