Building a Kotlin Dropwizard Maven REST API from scratch

Part 1: Configuring Maven

Kotlin is a great JVM language, that started being popular with Android development but has started increasing its popularity with other types of development, including backend services.

However most of the tutorials on the internet use Kotlin with Gradle, and while that is a great way to build your Kotlin application, sometimes you will want or need to use Maven instead. The good news is that it also is actually incredibly easy to do as well.

Dropwizard is a light-weight, high performance RESTful API framework, similar to Spring Boot in that it makes it easy to start building something useful quickly, it doesn’t come with a lot of the bloat that Spring applications often do.

This series will run you through every step of the way. You can do all of this manually (after installing Maven and the Java 8 JDK), but I would highly recommend using IntelliJ Community Edition. It’s free and made by the same creators of the Kotlin language. If you are using IntelliJ, click Create New Project, Select Maven, JDK 1.8, next, enter details, next, next, finish.

If doing it manually, after creating a new folder for your project, the first task you will need to do is create a “Project Object Model” file for Maven, named pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mannanlive</groupId>
    <artifactId>dropwizard-kotlin-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <dropwizard.version>1.3.8</dropwizard.version>
        <kotlin.version>1.3.20</kotlin.version>
        <kotlin.compiler.languageVersion>1.3</kotlin.compiler.languageVersion>
        <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
    </properties>
</project>

All we have done here is defined some properties about the artefact and some version numbers. Next we need to use Maven for its primary purpose, avoiding dependency hell, and add some libraries to our project. You should not but surprised by this as it’s the topic of this guide, we will be adding Kotlin & Dropwizard, to do this add the below between </properties> and </project>:

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>${dropwizard.version}</version>
    </dependency>
</dependencies>

Unfortunately, Maven is still not ready to use yet. To finish it off, we need to add the Kotlin plugin so our code can be compiled to bytecode so it can run on the JVM. Just before the </project> closing tag, add the below XML.

<build>
    <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Wow! A lot of stuff happening. Let’s break it down into three parts. The two source directory items are just configuring the build tool, telling them where to look for our code that will be added in the next section.

The <plugin> section is configuring the Kotlin plugin to do the compiling our source code for us. Lucky for us the plugin does all the hard work so we don’t have to. Remember the languageVersion and jvmTarget we defined at the beginning? This tells the compiler what version of the JDK we are targeting (Java 8) and what version of the Kotlin language we want to use, 1.3.

Congratulations, you have now completed your POM file, now to start coding.

About the Author

Mannan

Mannan is a software engineering enthusiast and has been madly coding since 2002. When he isn't coding he loves to travel, so you will find both of these topics on this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *