SAP Tutorial: Serving Data from an On Premise System in a CAP Java Application (Part 2)

Part 2: Setting Up a CAP Java Development Environment and Generating a New App

Brian Heise
4 min readApr 1, 2021
Photo by Fotis Fotopoulos on Unsplash

Contents

Welcome to Part 2 of Serving Data from an On-Premise System in a CAP Java Application. In the last section we learned how to link our Business Technology Platform (BTP) account to an on-premise system by creating a Destination. In Part 2, we will set up our development environment, generate the new application, and add the dependencies needed for connecting to the on-premise system.

Step 1

First, we need to install the required dependencies for CAP Java development and deployment. Go through the list below and make sure you have them all.

  • Node.js: installation instructions
  • @sap/cds-dk: npm i -g @sap/cds-dk
  • Java VM: At least Java 8 is required. SAP recommends their own “SAP Machine”.
  • Apache Maven: at least version 3.5.0 is required; follow these instructions
  • mtb build tool: npm i -g mbt
  • Cloud Foundry CLI: installation instructions here
  • CF CLI’s multiapps plugin: cf install-plugin multiapps

Additionally, in this tutorial we will use VS Code along with the following support packages.

Step 2

Next let’s generate the new application. Navigate to the folder where you want to generate the app and enter the following code:

mvn archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds -DarchetypeVersion=RELEASE

You will be prompted to enter some parameters. If you’re new to Java, check out their naming conventions here. The following quotes are taken from this article.

  • groupId: “uniquely identifies your project across all projects”
  • artifactId: “is the name of the jar without version”
  • package name: Maven will automatically generate a name, you can just hit enter to accept it as it is.
  • target platform cloud foundry?: Enter ‘Y’ for yes.

After this, the app will be generated. You’ll see the following output:

Let’s check the results in VS Code:

Notice the two pom.xml files. If you’re new to Java, these are like package.json in node. We need to add our dependencies to these.

Step 3

The first dependency we’ll add is SAP’s Cloud SDK. It provides some useful features for connecting to an on-premise system.

Open the pom.xml in the root folder. Find the DEPENDENCIES VERSION section and add this to the bottom of the list:

<cloud.sdk.version>3.38.0</cloud.sdk.version> 

Here we have just declared the version; we’ll add the actual dependency in the next step.

Step 4

Next, find the normal dependencies section. Add this at the bottom:

<!-- CLOUD SDK -->
<dependency>
<groupId>com.sap.cloud.sdk</groupId>
<artifactId>sdk-bom</artifactId>
<version>${cloud.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

Step 5

Next, let’s open the pom.xml file in the srv folder. Add these dependencies to the dependencies list:

   <dependency>
<groupId>com.sap.cds</groupId>
<artifactId>cds-feature-remote-odata</artifactId>
<scope>runtime</scope>
</dependency>
<!-- CLOUD SDK dependencies -->
<dependency>
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
<artifactId>resilience</artifactId>
</dependency>
<dependency>
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
<artifactId>scp-cf</artifactId>
</dependency>

Step 5

Finally, find the section labeled “cds.build”

In the section, there are two <command> tags. Replace them with the following:

<command>build --for java-cf</command>
<command>deploy --to sqlite --with-mocks --dry > ${project.basedir}/src/main/resources/schema.sql</command>

In the first command we add --for java-cf, which prepares the build for cloud foundry.

In the second command we added --with-mocks, which allows us to fake our on premise data for local development. We won’t actually use that function in this tutorial, but if you find yourself wanting to do that, you will need this line. Your app will throw errors otherwise.

Conclusion

Here in Part 2 we learned how to generate a new app and add the dependencies necessary for connecting to our on-premise system. In Part 3 we will learn how to import an oData service from our on-premise system.

Photo by Fotis Fotopoulos on Unsplash

Contents

Support

Did you like this blog? Want to make sure I can keep creating them? Then consider subscribing on Patreon!

--

--

Brian Heise

Full Stack web developer employed at Liferay Japan