ZOFTINO.COM android and web dev tutorials

Developing Kotlin Applications Using Eclipse and Gradle

This tutorial helps you in learning how to use eclipse and gradle in building kotlin jar and web applications and deploying kotlin web application to tomcat within the eclipse using gradle tomcat plug-in.

There is a tutorial for java application development using eclipse and gradle, it gives details about installing and using gradle eclipse plug-in and gradle tomcat plug-ins.

Install eclipse kotlin plug-in which will be used for programming kotlin in eclipse.

Kotlin Jar Using Eclipse and Gradle

First create java project using gradle eclipse plug-in. To do that, click File menu, then click New and Other menu items, then expand Gradle, select Gradle Project, click next, click next, enter project name and click finish.

Once java project structure is created, add kotlin srouce folder src/main/kotlin by right clicking the project, then clicking new and source folder menu items and entering folder name. So the project will contain both java and kotlin source. If you want to use only kotlin in the project, remove src/main/java folder.

Then create kotlin file by right clicking the kotlin source folder, then clicking new and other menu items, selecting kotlin file, entering package and kotlin file name and clicking ok.

Then add kotlin gradle plug-in by adding plug-in and its dependency entries to build.gradle file of your project.

apply plugin: "kotlin"

buildscript {
    ext.kotlin_version = '1.2.51'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

After adding kotlin plugin and its dependency, you need to add dependency on the kotlin standard library.

compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

Here is the complete contents of the build.gradle file.


apply plugin: 'java-library'
apply plugin: "kotlin"

repositories {
    jcenter()
}

buildscript {
    ext.kotlin_version = '1.2.51'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}


dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
	implementation 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.12'
    
    compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}

After adding kotlin gradle plugin and kotlin library dependency to grade build file, refresh the project by right clicking the project and then clicking Gradle and Refresh Gradle Project menu items.

Then add java and kotlin source files to src/main/java and src/main/kotlin folders respectively. In our example, kotlin file splits the given string and delimiter, and formats the strings using java utility class.

Kotlin file:

package com.zoftino.kotlin.records
import java.util.*
import com.zoftno.java.records.ProcessUtl

fun main(args: Array<String>) {
	println("Enter record: ")
    val input = Scanner(System.`in`)
    val rec = input.next()
    println("Enter delimiter: ")
    val deli = input.next()
	
    processRecord(rec, deli)
}

fun processRecord(record: String, delimeter: String){
	val recs = record.split(delimeter)	
	for (rec in recs) println(ProcessUtl.formatRec(rec))
}

Java class:

package com.zoftno.java.records;

public class ProcessUtl {
	public static String formatRec(String s) {
		return s.toLowerCase();
	}
}

After adding the source, here is the structure of the project.

eclipse gradle kotlin jar project structure

Then buid the project by double clicking the build task in gradle tasks eclipse view at bottom. It will create classes and jar file in build folder of the project.

Kotlin Web Application Using Eclipse and Gradle

To create kotlin web application, first you need to create kotlin project following the steps described in the previous section.

Then create src/main/webapp, src/main/webapp/WEB-INF and src/main/resources folders. Then add sources and resource files to the project.

In our example, we add kotlin http servelt.

package com.zoftino.kotlin.records

import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


@WebServlet(name = "process", value = "/process")
class ProcessController : HttpServlet() {
    override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {
		
		val rec = "amazon|upto 5% off on laptops|ccoupon|2333";
		processRecord(rec, "|")
		
        res.writer.write("processed");
    }
}

Then add war and tomcat plugins and its dependencies to build.gradle file as explained in building war using gradle and deploying it to tomcat using kotlin tomcat plugin tutorial.

Here is the gradle.build properties file after adding war and gradle tomcat plug-ins.

 apply plugin: 'kotlin'
 apply plugin: 'java'
 apply plugin: 'war'
 apply plugin: 'com.bmuschko.tomcat'
 apply plugin: 'eclipse-wtp'

repositories {
    jcenter()
}

buildscript {
    ext.kotlin_version = '1.2.51'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

buildscript {
  repositories {
	jcenter()
  }
  dependencies {
    classpath 'com.bmuschko:gradle-tomcat-plugin:2.5'
  }
 }
 

dependencies {
 
    testImplementation 'junit:junit:4.12'
    
    compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    
	def tomcatVersion = '8.0.53'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
         "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
         "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

Then refresh kotlin web application project by right clicking it and then clicking gradle and refresh gradle project. Then double click build task in gradle tasks view to create war, which is created in build folder.

To deploy the kotlin war to tomcat, create run configuration and run tomcatRun task. Our kotlin web application deployed on tomcat locally can be accessed using http://localhost:8080/recordProcessor/process url.

kotlin war deployed to tomcat in eclipse using gradle