Use Kotlin with SAP Hybris
1. Overview
Kotlin is a new modern programming language built by JetBrains (IntelliJ IDE vendor), Kotlin is starting to become very popular and to be the favourite programming language for the majority of developers including me.
As far as I know, Hybris does not support Kotlin yet, it’s so unfortunate to not be able to use such a lovely/powerful programming language on Hybris.
So, I’ve decided to give it a try.
2. Prepare Kotlin
1. Download the last recent versions of Kotlin compiler dependencies.
- kotlin-ant.jar : https://jar-download.com/artifact-search/kotlin-ant
- kotlin-preloader.jar :Â https://jar-download.com/artifact-search/kotlin-preloader
- kotlin-compiler-embeddable.jar :Â https://jar-download.com/artifact-search/kotlin-compiler-embeddable
- kotlin-compiler-runner.jar : https://jar-download.com/artifact-search/kotlin-compiler-runner
- kotlin-compiler.jar :Â https://jar-download.com/artifact-search/kotlin-compiler
- kotlin-reflect.jar :Â https://jar-download.com/artifact-search/kotlin-reflect
- kotlin-script-runtime.jar : https://jar-download.com/artifacts/org.jetbrains.kotlin/kotlin-script-runtime
- kotlin-stdlib.jar : https://jar-download.com/artifact-search/kotlin-stdlib
To download all the Kotlin compiler dependencies together, refer to this link.
2. Extract Jars in a new directory inside your project, this directory is going to be the KOTLIN_HOME
directory.
3. Export the KOTLIN_HOME
as an environment variable. e.g.
$ export KOTLIN_HOME=/Users/mouadelfakir/Desktop/hybris/hybris/config/kotlin-ant
3. Prepare Hybris
Here, I got inspired from the existent ant config of the javacompile
 task located in the compiling.xml and util.xml.
I prefer to add Kotlin compiler task to the compiling.xml file too, to make it available to all the extensions.
You can enable Koltin for a specific extensions only, to do so use buildcallbacks.xml to configure Koltin compiler.
1. Add the following lines to the compiling.xml file, to load Koltin Ant tasks from kotlin-ant.jar.
<property environment="env" />
<taskdef resource="org/jetbrains/kotlin/ant/antlib.xml">
<classpath>
<fileset dir="${env.KOTLIN_HOME}" includes="*.jar"/>
</classpath>
</taskdef>
1. In the compiling.xml file as well, add a new sequential entry called extension_kotlin_core
 (5).
<macrodef name="extension_compile">
<attribute name="extname" />
<sequential>
<extension_compile_core extname="@{extname}" />
<extension_kotlin_core extname="@{extname}" />
<extension_generate_deployment extname="@{extname}" />
<extension_compile_web extname="@{extname}" />
<extension_compile_hmc extname="@{extname}" />
<extension_compile_backoffice extname="@{extname}" />
<extension_compile_hac extname="@{extname}" />
</sequential>
</macrodef>
2. Create an ant macrodef
with the same name as the entry:Â extension_kotlin_core
.
<macrodef name="extension_kotlin_core">
<attribute name="extname" />
<sequential>
<echo message="## compiling Kotlin source code for ext : @{extname}"/>
<if>
<isset property="ext.@{extname}.coremodule.available" />
<then>
<if>
<available file="${ext.@{extname}.path}/ksrc" />
<then>
<kotlinc moduleName="@{extname}" src="${ext.@{extname}.path}/ksrc" output="${ext.@{extname}.path}/classes" failOnError="true" verbose="true">
<classpath>
<pathelement path="${build.classpath}" />
<pathelement path="${HYBRIS_BOOTSTRAP_BIN_DIR}/models.jar" />
<fileset dir="${bundled.tomcat.home}">
<include name="lib/jsp-api.jar" />
<include name="lib/servlet-api.jar" />
<include name="lib/el-api.jar" />
<include name="lib/wrapper*.jar" />
</fileset>
<pathelement path="${HYBRIS_TEMP_DIR}/log4j" />
<fileset dir="${env.KOTLIN_HOME}">
<include name="*.jar" />
</fileset>
</classpath>
</kotlinc>
</then>
</if>
</then>
</if>
</sequential>
</macrodef>
- (6) Check whether the current extension is a core module extension.
- (9) Check if the ksrc directory exists in the current extension (ksrc refers to Kotlin source), is where the Kotlin sources need to be.
- (11)Â
kotlinc
: is a task for Ant provided by Kotlin to perform the compilation.- moduleName : name of the extension.
- src :Â Kotlin source file or directory to compile.
- output : destination directory.
Some other useful params for kotlinc
.
- nowarn : suppresses all compilation warnings.
- failOnError : build fails if any compilation error.
- verbose : enable verbose mode handy for debugging.
Note that the proper way to update compiling.xml file is using ant customize.
4. Kotlin in action
Let’s try our first Kotlin class in Hybris.
1. First, in your custom extension create a ksrc folder and mark it as Sources Root using your IDE.
2. Create a Kotlin class. e.g. KotlinHelloWorld.kt
.
package com.stackextend.training.core
import de.hybris.platform.servicelayer.user.UserService
import org.springframework.beans.factory.annotation.Autowired
import java.lang.IllegalArgumentException
class KotlinHelloWorld(@Autowired private val userService: UserService, uid: String = "default") {
private var greeting: String;
init {
val user = userService.getUserForUID(uid) ?: throw IllegalArgumentException("No user with Uid ${uid}")
greeting = if (userService.isAdmin(user)) {
"Hello Admin"
} else {
"Hello Customer"
}
}
fun showGreeting() : String = greeting
}
3. Build the project using ant all command and start the server, if everything goes ok you should be able to see the following lines in the console.
[echo] building extension 'trainingcore'...
[yjavac] Compiling 1 source file to /Users/mouadelfakir/Desktop/hybris/hybris/bin/custom/training/trainingcore/classes
[echo] compinling kotlin source code for ext : trainingcore
[kotlinc] Compiling [/Users/mouadelfakir/Desktop/hybris/hybris/bin/custom/training/trainingcore/ksrc] => [/Users/mouadelfakir/Desktop/hybris/hybris/bin/custom/training/trainingcore/classes]
5. TODO
- Get inspired from theÂ
extension_compile_web
andÂextension_kotlin_core
 microdefs to enable Kotlin for web module extensions too. - Update the outdated check to detect changes of Kotlin files.
Happy Kotling 😀
Find the source code of this example in GitHub.
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
Brilliant, thank you for sharing,
Besides that kotlin developers will enjoy coding with their favourite language in the SAP Hybris platform, how can using kotlin in Hybris be useful and interesting ?
Hi Ayoub, thanks for you comment,
I think the reason why we should use Koltin is the same reason why we prefer to use C language over assembly language and also the same reason why we prefer to use JAVA over C 🙂