Use Kotlin alongside Java in a Hippo CMS Project - Enterprise Java Content management system - Hippo CMS
GatsbyJS and BloomReach CMS

September 21, 2018

January 7, 2014

May 29, 2018

Use Kotlin alongside Java in a Hippo CMS Project LAB

Baris Can Vural Baris Can Vural is a Software Engineer at BloomReach. He holds an MSc in Computer Science and Engineering with a focus on Internet and Web Technologies. When he is not solving complex technical challenges you can find him at the gym or writing code for side projects. You can find his projects on his GitHub account.

 

//onehippo-prod.global.ssl.fastly.net/binaries/ninecolumn/content/gallery/blog/kotlin_hippo.png

 

Kotlin is a new programming language that’s taking the Java community by storm after its announcement at Google I/O 17’ as a first-class language for the Android platform. In fact, 20% of apps, built with Java before Google I/O, are now being built in Kotlin.

 

TL;DR All the code changes can be seen in the demo project .

 

If you are reading this, you are probably not an Android developer but there are still plenty of reasons to use Kotlin. It’s concise and null-safe. It provides features that Java language does not currently have, such as extension functions, string templates, first-class delegation, range expressions and many more. Kotlin is also fully interoperable with Java as it compiles to bytecode. If you want to use Kotlin in a Hippo project, it is entirely possible!

 

I will outline the steps you need to take to put Kotlin code in a typical Hippo project’s site module. You can add Kotlin code in the same folders as Java code. It is also possible to call Kotlin code from Java and vice versa. Note that similar steps can be applied to the cms module as well.

 

Steps

 

1. Add dependencies/plugins to pom.xml

Go to site module’s pom.xml. Add to properties the following:

 

<properties>
   <kotlin.version>1.1.51</kotlin.version>
</properties>

 

2. Add to dependencies section the kotlin stdlib and test dependencies:


 

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jre8</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-test</artifactId>
    <version>${kotlin.version}</version>
    <scope>test</scope>
</dependency>

 

3) Add to the plugins section the following.

Note that there are some changes to the maven-compiler plugin. These changes are there to make sure Kotlin and Java can both compile from the same folders and talk to each other without issues.


 

<plugin>
       <groupId>org.jetbrains.kotlin</groupId>
       <artifactId>kotlin-maven-plugin</artifactId>
       <version>${kotlin.version}</version>
       <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>
       <configuration>
         <jvmTarget>1.8</jvmTarget>
       </configuration>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <executions>
         <!-- Replacing default-compile as it is treated specially by maven -->
         <execution>
           <id>default-compile</id>
           <phase>none</phase>
         </execution>
         <!-- Replacing default-testCompile as it is treated specially by maven -->
         <execution>
           <id>default-testCompile</id>
           <phase>none</phase>
         </execution>
         <execution>
           <id>java-compile</id>
           <phase>compile</phase>
           <goals>
             <goal>compile</goal>
           </goals>
         </execution>
         <execution>
           <id>java-test-compile</id>
           <phase>test-compile</phase>
           <goals>
             <goal>testCompile</goal>
           </goals>
         </execution>
       </executions>
     </plugin>


 

This is all you need to be able to use Kotlin in site webapp. Now let’s add a simple Kotlin class that extends EssentialsNewsComponent:


 

package org.example.components

import org.hippoecm.hst.core.component.HstRequest
import org.hippoecm.hst.core.component.HstResponse
import org.hippoecm.hst.core.parameters.ParametersInfo
import org.onehippo.cms7.essentials.components.EssentialsNewsComponent
import org.onehippo.cms7.essentials.components.info.EssentialsNewsComponentInfo

@ParametersInfo(type = EssentialsNewsComponentInfo::class)
class KotlinNewsComponent : EssentialsNewsComponent() {

   override fun doBeforeRender(request: HstRequest, response: HstResponse) {
       super.doBeforeRender(request, response)
   }

}


 

If you check out the demo project, there is a sitemap item at /kotlinnews that uses the KotlinNewsComponent that we just added. You’ll see that it works exactly the same way as /news.

 

As you can see, it is very simple to configure your Hippo project for using Kotlin. You can have Kotlin code next to Java code, and experiment using both languages. Who knows, perhaps, you can convince your organization to use (more) Kotlin!

 

 

Stay up to date

 

Join our community to stay up to date with the latest Hippo CMS developments. Get the latest information on our CMS updates, ask questions and meet fellow Hippo developers.

 

Subscribe Now

 

GatsbyJS and BloomReach CMS

September 21, 2018

January 7, 2014

May 29, 2018

Use Kotlin alongside Java in a Hippo CMS Project LAB

Baris Can Vural Baris Can Vural is a Software Engineer at BloomReach. He holds an MSc in Computer Science and Engineering with a focus on Internet and Web Technologies. When he is not solving complex technical challenges you can find him at the gym or writing code for side projects. You can find his projects on his GitHub account.

 

//onehippo-prod.global.ssl.fastly.net/binaries/ninecolumn/content/gallery/blog/kotlin_hippo.png

 

Kotlin is a new programming language that’s taking the Java community by storm after its announcement at Google I/O 17’ as a first-class language for the Android platform. In fact, 20% of apps, built with Java before Google I/O, are now being built in Kotlin.

 

TL;DR All the code changes can be seen in the demo project .

 

If you are reading this, you are probably not an Android developer but there are still plenty of reasons to use Kotlin. It’s concise and null-safe. It provides features that Java language does not currently have, such as extension functions, string templates, first-class delegation, range expressions and many more. Kotlin is also fully interoperable with Java as it compiles to bytecode. If you want to use Kotlin in a Hippo project, it is entirely possible!

 

I will outline the steps you need to take to put Kotlin code in a typical Hippo project’s site module. You can add Kotlin code in the same folders as Java code. It is also possible to call Kotlin code from Java and vice versa. Note that similar steps can be applied to the cms module as well.

 

Steps

 

1. Add dependencies/plugins to pom.xml

Go to site module’s pom.xml. Add to properties the following:

 

<properties>
   <kotlin.version>1.1.51</kotlin.version>
</properties>

 

2. Add to dependencies section the kotlin stdlib and test dependencies:


 

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib-jre8</artifactId>
    <version>${kotlin.version}</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-test</artifactId>
    <version>${kotlin.version}</version>
    <scope>test</scope>
</dependency>

 

3) Add to the plugins section the following.

Note that there are some changes to the maven-compiler plugin. These changes are there to make sure Kotlin and Java can both compile from the same folders and talk to each other without issues.


 

<plugin>
       <groupId>org.jetbrains.kotlin</groupId>
       <artifactId>kotlin-maven-plugin</artifactId>
       <version>${kotlin.version}</version>
       <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>
       <configuration>
         <jvmTarget>1.8</jvmTarget>
       </configuration>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <executions>
         <!-- Replacing default-compile as it is treated specially by maven -->
         <execution>
           <id>default-compile</id>
           <phase>none</phase>
         </execution>
         <!-- Replacing default-testCompile as it is treated specially by maven -->
         <execution>
           <id>default-testCompile</id>
           <phase>none</phase>
         </execution>
         <execution>
           <id>java-compile</id>
           <phase>compile</phase>
           <goals>
             <goal>compile</goal>
           </goals>
         </execution>
         <execution>
           <id>java-test-compile</id>
           <phase>test-compile</phase>
           <goals>
             <goal>testCompile</goal>
           </goals>
         </execution>
       </executions>
     </plugin>


 

This is all you need to be able to use Kotlin in site webapp. Now let’s add a simple Kotlin class that extends EssentialsNewsComponent:


 

package org.example.components

import org.hippoecm.hst.core.component.HstRequest
import org.hippoecm.hst.core.component.HstResponse
import org.hippoecm.hst.core.parameters.ParametersInfo
import org.onehippo.cms7.essentials.components.EssentialsNewsComponent
import org.onehippo.cms7.essentials.components.info.EssentialsNewsComponentInfo

@ParametersInfo(type = EssentialsNewsComponentInfo::class)
class KotlinNewsComponent : EssentialsNewsComponent() {

   override fun doBeforeRender(request: HstRequest, response: HstResponse) {
       super.doBeforeRender(request, response)
   }

}


 

If you check out the demo project, there is a sitemap item at /kotlinnews that uses the KotlinNewsComponent that we just added. You’ll see that it works exactly the same way as /news.

 

As you can see, it is very simple to configure your Hippo project for using Kotlin. You can have Kotlin code next to Java code, and experiment using both languages. Who knows, perhaps, you can convince your organization to use (more) Kotlin!

 

 

Stay up to date

 

Join our community to stay up to date with the latest Hippo CMS developments. Get the latest information on our CMS updates, ask questions and meet fellow Hippo developers.

 

Subscribe Now