hachyderm.io is one of the many independent Mastodon servers you can use to participate in the fediverse.
Hachyderm is a safe space, LGBTQIA+ and BLM, primarily comprised of tech industry professionals world wide. Note that many non-user account types have restrictions - please see our About page.

Administered by:

Server stats:

9.7K
active users

#SAPBTP

0 posts0 participants0 posts today

A Kotlin-based CAP Java Application (Part 1)

Introduction

I recently asked myself whether it is possible to develop a full-fledged cloud application using Kotlin and the SAP Cloud Application Programming Model for Java (CAP Java). Instead of only thinking about it, I decided to start a project to check this.

I'm developing the application in several steps, each step is accompanied by a small blog article. I won't go into every detail when it comes to CAP Java, so knowledge in this area is beneficial to follow the steps.

The application will be a link aggregator where users can add URLs to interesting information on the web. Links can be marked as private, mutual (only visible for other logged-in users), and public (visible for all).

Creating and Adjusting the Project

The first step is to create a CAP Java project as described in CAPire. I'm not going to go into depth here but only show what needs to be changed to use Kotlin for developing the microservice. Make sure that you have a model and database for local development. I chose SQLite to be on the safe side when it comes to integration of multi-tenancy in a later step.

Let's start with the required changes in the parent pom file.

1. Add a property for the Kotlin version in the <properties>:

<kotlin.version>2.1.0</kotlin.version>

2. Add the dependency for the Kotlin standard library to the dependency management:

<!-- KOTLIN -->  
<dependency>  
    <groupId>org.jetbrains.kotlin</groupId>  
    <artifactId>kotlin-stdlib</artifactId>  
    <version>${kotlin.version}</version>  
</dependency>

3. Add the Kotlin compiler plugin:

<!-- KOTLIN PLUGIN -->
<plugin>
	<groupId>org.jetbrains.kotlin</groupId>
	<artifactId>kotlin-maven-plugin</artifactId>
	<version>${kotlin.version}</version>
	<executions>
		<execution>
			<id>compile</id>
			<goals>
				<goal>compile</goal>
			</goals>
			<configuration>
				<sourceDirs>
				<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
				<sourceDir>${project.basedir}/src/main/java</sourceDir>
				</sourceDirs>
			</configuration>
		</execution>
		<execution>
			<id>test-compile</id>
			<goals>
				<goal>test-compile</goal>
			</goals>
			<configuration>
				<sourceDirs>
				<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
				<sourceDir>${project.basedir}/src/test/java</sourceDir>
				</sourceDirs>
			</configuration>
		</execution>
	</executions>
</plugin>

4. Adjust the Java compiler plugin:

As we still need Java compilation of the POJOs generated by the CDS compiler we have to adjust the settings of the Java compiler plugin. This is the change entry:

<!-- JAVA COMPILER -->
<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.13.0</version>
	<configuration>
		<release>${jdk.version}</release>
		<encoding>UTF-8</encoding>
	</configuration>
	<executions>
		<execution>
			<id>default-compile</id>
			<phase>none</phase>
		</execution>
		<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>

As the perent pom is now complete, some changes need to done in the srv/ directory.

1. Add the dependency for the Kotlin standard library:

<!-- KOTLIN -->  
<dependency>  
    <groupId>org.jetbrains.kotlin</groupId>  
    <artifactId>kotlin-stdlib</artifactId>  
</dependency>

2. Create Kotlin source directory:

Create the directory srv/src/main/kotlin, create the base package for your project and add the Kotlin class Application in the base package with the following content:

package io.github.linkaggregator  
  
import org.springframework.boot.SpringApplication  
import org.springframework.boot.autoconfigure.SpringBootApplication  
  
@SpringBootApplication  
open class LinkAggregatorApplication  
  
fun main(args: Array<String>) {  
    SpringApplication.run(LinkAggregatorApplication::class.java, *args)  
}

3. Delete the Java source directory:

As we want to develop the service in Java, the directory srv/src/main/java can be deleted now.

Now you can build and run the service!

That's it for the first part of this series. You can find the sources for this part on Github.

In the next part we're going to switch to Groovy and Spock to develop unit tests. So stay tuned!

@sap @sapcap #sapcap #cap #capjava #java #kotlin #cloud #sapbtp #btp

cap.cloud.sapGetting Started | capireDocumentation for SAP Cloud Application Programming Model

Hey, you. Do you like learning-by-doing? Networking with fellow tech enthusiasts in the SAP ecosphere? Are you in or near NL 🇳🇱?

Then how about this double bill of events, brought to you by the Developer Advocates, this September? Check them out and register before it's too late 👉

On the btp CLI and APIs, in Utrecht (25 Sep): community.sap.com/t5/sap-codej

On CAP and HANA Cloud, in Eindhoven (26 Sep): community.sap.com/t5/sap-codej