Simple kotlin sparkjava project with gradle.
Lets start by creating the directory structure for simple kotlin sparkjava project.
Assuming we have java and gradle on the path
The version of gradle 2.12 and java 1.8
If you don't have them on your path, set it as
set JAVA_HOME=your_java_home_dir
set GRADLE_HOME=your_gradle_home_dir
set path=%JAVA_HOME%/bin;%GRADLE_HOME%/bin;
you can now test if they are working properly by typing command
%> java -version
%> gradle -v
Lets move to workspace directory, for me it is c:\workspace
Lets create a project named "myspark" by creating a directory under c:\workspace
mkdir myspak
cd myspark
now you are into myspark folder (c:\workspace\myspark)
Lets create source folders as per maven standard directory structure for java and kotlin.
mkdir src\main\java
mkdir src\main\kotlin
mkdir src\test\java
mkdir src\test\kotlin
mkdir src\main\resources
mkdir src\test\resources
create a new file name build.gradle under myspark directory
paste the content below into build.gradle
// c:\workspace\myspark\build.gradle
buildscript {
ext.kotlin_version = '1.0.1'
ext.gson_version = '2.6.2'
ext.sparkjava_version = '2.3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application' // to run main file
apply plugin: 'idea' // for intellij project
// note 'Kt', static members are grouped in a generated class with 'Kt' here MainKt
mainClassName = 'com.mmk.MainKt'
defaultTasks 'run'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "com.google.code.gson:gson:$gson_version"
compile 'com.sparkjava:spark-core:$sparkjava_version'
testCompile 'junit:junit:4.11'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
On your command prompt, change your current directory to src\main\kotlin
ie c:\workspace\myspark\src\main\kotlin
make directory com\mmk
ie on command prompt %> mkdir com\mmk
You can have any package, make sure it reflects the mainClassName above to run the app.
under com\mmk create a Main.kt file
edit Main.kt and paste the below content,
package com.mmk
import com.google.gson.Gson
import spark.Spark
import spark.Spark.get
fun main(args: Array<String>) {
val main = Main()
main.run()
}
data public class Name(val fname: String, val lname: String)
class Main{
val gson = Gson();
fun run(){
// this is folder under src/main/resources to serve static files.
Spark.staticFileLocation("/public");
get("/hello", { req, res -> "Hello World" });
get("/hello/:name", { req, res -> "Hello " + req.params(":name") });
get("/hello/:fname/:lname", {
req, res ->
val fname = req.params(":fname")
val lname = req.params(":lname")
Name(fname,lname)
},
{obj -> gson.toJson(obj)}
);
}
}
save the file, now on the terminal or command prompt, go to the root directory
here c:\workspace\myspark
run the below command to compile & build.
gradle build
then to run the app, type the below command,
gradle run
this will start jetty at port 4567 by default,
you can test the app by opening the browser with the url below
http://localhost:4567/hello
http://localhost:4567/hello/fazal
press Ctrl+C to exit the server.
you can test the app by opening the browser with the url below
http://localhost:4567/hello
http://localhost:4567/hello/fazal
press Ctrl+C to exit the server.
Hi Fazal, thx for your tuto
ReplyDeleteTo make the gradle build work I had to replace
compile 'com.sparkjava:spark-core:$sparkjava_version' with compile "com.sparkjava:spark-core:$sparkjava_version"