In the tutorial, Kotlination will help you the first step for starting with Kotlin language by building a example Kotlin HelloWorld using SpringBoot, Maven and SpringToolSuite.
>>> Refer to: JavaSampleApproach
I. Technologies
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6.RELEASE
– Kotlin language
II. Practice
Step to do:
– Create SpringBoot project with Kotlin language
– Install Kotlin Plugin for Eclipse
– Run and check results
1. Create SpringBoot project with Kotlin language
Open SpringToolSuite, choose File -> New -> Other -> Spring Starter Project:
Press Next, then input project info as below image:
Press Next -> Finish, we got:
Take a look at pom.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
{{urvanov-syntax-highlighter-internal:0}} <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javasampleapproach.kotlin</groupId> <artifactId>SpringKotlinHelloWorld</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>SpringKotlinHelloWorld</name> <description>SpringKotlinHelloWorld</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <kotlin.version>1.1.4</kotlin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jre8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <jvmTarget>1.8</jvmTarget> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> |
2. Install Kotlin Plugin for Eclipse
What is Kotlin Plugin for Eclipse?
-> The Kotlin Plugin for Eclipse helps you write, run, debug and test programs in Kotlin language.
How to install it?
On main Menu, choose Help -> Install New Software….
Add kotlin plugin for eclipse with link: https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/last/
We got:
Check Kotlin as above image, then press Next.
Now follow the guides to install Kotlin plugin. After all is Done, we will get a Software Update message dialog.
Press Yes to restart SpringToolSuite for the changes to take effect.
-> Result:
3. Run and check results
Open class SpringKotlinHelloWorldApplication, then add some println line of codes as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javasampleapproach.kotlin import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class SpringKotlinHelloWorldApplication fun main(args: Array<String>) { SpringApplication.run(SpringKotlinHelloWorldApplication::class.java, *args) println("######################### Kotlination.com #########################") println("######################### Hello, world! #########################") println("######################### Have a nice day! #########################") } |
Build the project with commandline mvn clean install -Dmaven.test.skip=true
:
Run the Kotlin SpringBoot project with commandline mvn spring-boot:run
.
-> Results: