ZOFTINO.COM android and web dev tutorials

Developing Kotlin Applications using Intellij Idea and Gradle

This tutorial helps you in using Intellij Idea and Gradle to develop kotlin applications, package them as jar or war and deploy kotlin web applications to tomcat in Intellij Idea.

Creating Gradle Kotlin Project and Building Jar

Open intellij idea, in the welcome window, click create new project.

intellij create new project

Then select Gradle and Kotlin as shown in the following image.

intellij create new project gradle kotlin

Then enter group id, artifact id and version, click next and then click next and finish.

intellij create new project enter group id and artifact id

It creates the gradle kotlin project with build.gradle file which contains kotlin plugin and its dependency. After automatic build sync, it will create gradle wrapper.

intellij gradle project without source folders with gradle wrapper

Notice that source folders are missing in the project structure. To create source folders automatically, you need to enable a related setting. To do that, first go to settings by clicking File menu and then clicking Settings menu item. Then in the left pane of settings window, expand Build, Execution, Deployment and select Gradle. In the right pane of settings window, check the Create directories for empty content root automatically option and click apply and ok.

intellij create directories for empty content roots

After build sync, you will see java, kotlin and resources folders in src/main and src/test folders.

Then add java and kotlin source files by right clicking the corresponding folder and clicking new and java class or kotlin file menu items.

Here is the project structure after adding source folders and files.

intellij gradle project structure with gradle wrapper and source folders

Then add java-library gradle plug-in to build.gradle file. To package the project as jar which includes both java and kotlin classes, you need to open Gradle view. To do that, click View menu and then click Tools Window and Gradle menu items.

intellij gradle view, build kotlin and java jar and war

In the gradle tasks window, double click build to create jar. It will create the jar in the libs folder under build folder in the project.

intellij kotlin java gradle build jar

Kotlin Web Application

To create kotlin web application in intellij idea using gradle, you need to first create kotlin project following steps discussed in the previous sections. Then add gradle war plug-in to build.gradle file.

apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'kotlin'

Then create webapp directory in src/main folder and create WEB-INF directory under webapp folder by clicking File menu and then selecting New and Directory sub menus .

Then add source and resource files to your web application. To build war, open Gradle view and double click build. It will create war file in build/libs folder.

Deploying Kotlin War to Tomcat

For testing your web application locally, you can deploy it to tomcat in intellij idea by adding gradle tomcat plugin-in and running tomcatRun task as explained in developing kotlin applications using eclipse and gradle.

If you have Intellij Idea ultimate edition, you can use intellij idea tomcat plugin to deploy web apps to servers. For that, here are the steps.