Use Kotlin with SAP Hybris

Hybris Logo

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.

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.

kotlin dependecies jars

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">
		<fileset dir="${env.KOTLIN_HOME}" includes="*.jar"/>

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" />
            <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}" />

2. Create an ant macrodef with the same name as the entry: extension_kotlin_core.

<macrodef name="extension_kotlin_core">
    <attribute name="extname" />
        <echo message="## compiling Kotlin source code for ext : @{extname}"/>
            <isset property="ext.@{extname}.coremodule.available" />
                    <available file="${ext.@{extname}.path}/ksrc" />
                        <kotlinc moduleName="@{extname}" src="${ext.@{extname}.path}/ksrc" output="${ext.@{extname}.path}/classes" failOnError="true" verbose="true">
								<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" />
								<pathelement path="${HYBRIS_TEMP_DIR}/log4j" />
								<fileset dir="${env.KOTLIN_HOME}">
									<include name="*.jar" />
  • (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.

kotlin on sap hybris

2. Create a Kotlin class. e.g. KotlinHelloWorld.kt.


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]


  1. Get inspired from the extension_compile_weband extension_kotlin_core microdefs to enable Kotlin for web module extensions too.
  2. Update the outdated check to detect changes of Kotlin files.

Happy Kotling 😀

Find the source code of this example in GitHub.


4.5 2 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

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 ?

Would love your thoughts, please comment.x