69

Using Kotlin on the SAP Hybris platform

 4 years ago
source link: https://blog.frankel.ch/using-kotlin-sap-hybris-platform/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using Kotlin on the SAP Hybris platform

Since I discovered Kotlin, I use it in all my personal projects. I’ve become quite fond of the language, and with good reason. However, there’s yet no integration with the Hybris platform - though there’s with Groovy and Scala. This post aims at achieving just that, to be able to use Kotlin on Hybris projects.

Generate a new extension

The first step on the journey is to create a new extension:

  1. Choose the yempty package
  2. Choose an adequate name, e.g. "kotlinfun"
  3. Choose a root package, e.g. ch.frankel.blog.kotlin
Don’t forget to add this new extension to the localextensions.xml file.

Add libraries

Kotlin requires libraries, both to compile and to run. They include:

  • kotlin-ant.jar
  • kotlin-compiler.jar
  • kotlin-preloader.jar
  • kotlin-reflect.jar
  • kotlin-script-runtime.jar

Though it would be nice to use external-dependencies.xml, some JAR are not available as Maven dependencies (e.g. kotlin-ant.jar). Thus, it’s required to manually get the relevant Kotlin compiler and unpack it.

Create folders

To help with the build, let’s create dedicated folders for sources and tests, respectively kotlinsrc and kotlintestsrc. Configure the module or the project accordingly - depending whether your IDE of choice is IntelliJ IDEA or Eclipse.

On the Hybris platform, both sources and tests will compile in the same folder.

Create simple Kotlin files in kotlinsrc and kotlintestsrc.

Configure Kotlin in IDE

While at it, configure Kotlin for the IDE. IntelliJ IDEA will pop-up an alert box to do so if a Kotlin file is created anyway. I have no clue about Eclipse…​

The fun part

Now that compiling works inside of the IDE, it should also be part of the standard command-line build. Fortunately, there are build hooks into the Hybris build pipeline. Those are configured inside the build_callbacks.xml file.

A naive first draft would look like:

The above is configured for a simple core module, not a web one. It can be added afterwards though.

Unfortunately, this doesn’t work. Launching the build with ant build will produce the following output:

/hybris/bin/platform/resources/ant/compiling.xml:530: java.lang.NoSuchMethodError:
 com.google.common.collect.Iterables.filter(Ljava/lang/Iterable;Lcom/google/common/base/Predicate;)Ljava/lang/Iterable;

Looking more closely at the kotlin-compiler.jar, one must notice it embeds Guava, which also happens to be on the hybris classpath. Hence, there’s a version conflict. Fortunately, there’s a smarter version of the compiler - kotlin-compiler-embeddedable.jar available on repo1 that shades Guava i.e. embeds it using a different package name, thus resolving the conflict. Just update the callback file with the new JAR name, and be done.

Now, the build produces a different output:

/hybris/bin/custom/kotlinfun/buildcallbacks.xml:34: java.lang.IllegalStateException:
 File is not found in the directory of Kotlin Ant task: kotlin-compiler.jar

For reasons unknown, the Ant task checks the existence of dependent libraries using their file names! Check the source code for proof. There’s an evil workaround: rename the JAR as it’s expected. And update the build file back to the initial version…​

At this point, it’s possible to build the platform with Kotlin files compiled.

Improvements

Though it works, there are a couple of possible improvements:

  • Change the lib folder to a dedicated folder e.g. compile , so as not to ship it in the distributed archive
  • Add the configuration to also compile a web module
  • Refactor the macros into a common extension, so that different Kotlin extensions can use it.

In the end, nothing prevents you from using Kotlin on SAP hybris right now!

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK