How to start a new CAP Java project and how to run it locally.
The CAP Java SDK enables developing CAP applications in Java. While the SAP Application Studio provides excellent support to develop CAP Java applications, you can also develop locally with your tool of choice, for example Eclipse or Visual Studio Code.
The CAP Java SDK supports lean application design by its modular architecture, that means you pick the required features and add them to your application dependencies on demand.
It enables local development by supporting in-memory or file-based SQLite databases. At the same time, the CAP Java SDK enables switching to a productive environment, using, for example, SAP HANA as a database, easily by simply switching the application deployment configuration.
If you use Spring Boot, you find yourself directly at home when using the CAP Java SDK, as the framework integrates nicely with Spring Boot features like transaction handling, auto-wiring and test support. While the CAP Java SDK is framework agnostic, it’s also possible to develop plain Java applications or even integrate with other frameworks.
The CAP Java SDK comes with an OData V4 protocol adapter, but it’s openly designed. You can add more protocol adapters in the future or provide even custom protocol adapters by the application.
It supports SAP Cloud Platform features like authentication and authorization based on XSUAA tokens. But you aren’t locked in to SAP Cloud Platform using a CAP Java application.
Excited? The following sections describe how to set up a development environment to get you started.
Setting Up Local Development
This section describes the prerequisites and tools to build a CAP application locally, for example with Eclipse.
Install the CDS Compiler and Tools
- Follow the steps in ‘Getting Started / Local Setup’ to set up the CDS tools.
Install Java & Maven
- Install a Java VM. At least, Java 8 is required.
For example, download and install SapMachine 11.
- Install Apache Maven.
Execute the following commands on the command-line to check whether the installed tools are setup correctly:
Starting a New Project
Take the following steps to set up a new CAP Java application based on Spring Boot from scratch.
As a prerequisite, you’ve setup your development environment.
To bootstrap a CAP Java application based on Spring Boot, a Maven archetype exists. Run the following Maven command:
mvn archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds -DarchetypeVersion=RELEASE -DincludeModel=true
When prompted, specify the group and artifact ID of your application, for example:
... Define value for property 'groupId': org.example Define value for property 'artifactId': myapp ...
The artifact ID also specifies the name of your projects root folder that is generated in your current working directory. For other values prompted, it’s enough to simply confirm the default values.
Eventually, this archetype generates a minimal “Hello World” CAP Java application based on a simple CDS model that you can run and enhance. The generated project has the following folder structure:
<PROJECT-ROOT>/ |-- db/ `-- srv/ |-- src/main/java/ |-- src/gen/java/ |-- node/ `-- node_modules/
db: contains content related to your database. A simple CDS model is located in the file
srv: contains the CDS service definition and Java back-end code. A simple service model is located in
cat-service.cdsand a small example application in
srv/src/main/java: contains Java application logic
srv/src/gen/java: contains compiled CDS model and compiled Java PoJos for model entities
node_modules: contain a local Node.js installation and dependencies for the CDS compiler
To not be distracted by CDS tools-specific folders in Eclipse, you can define resource filters. Open the context menu on the project’s root folder and select “Properties”. Go to Resource > Resource Filters” and exclude folders with the name
Build and Run
To build and run the generated project from the command line, execute:
cd <PROJECT-ROOT> mvn spring-boot:run
To test whether the started application is up and running, point your browser to http://localhost:8080
- Download the Eclipse package for Java developers.
- Unzip and start.
Add Spring Plug-Ins
The following optional steps make development in Eclipse more convenient:
- Install “Spring Tools 4”: From the Eclipse Marketplace (Help > Eclipse Marketplace…) search and install the Spring Tools 4. These tools provide some convenience when running Spring-based applications and unit tests.
Add the SAP Cloud Business Application Tools for Eclipse
Install the Eclipse plug-in called SAP Cloud Business Application Tools for Eclipse that supports convenient editing of CDS files.
In Eclipse open Help > Install New Software….
Add the following update site location:
Select SAP Cloud Business Application Tools > SAP Cloud Business Application Tools for Eclipse.
Click Next and Finish, accept the license agreement and warning about unsigned content, restart Eclipse.
Import the Project
Select File > Import… > Existing Maven Projects.
PROJECT-ROOTfolder and click Go. Finally, select the project that was found.
Compile the Project
Right-click on the
pom.xmlfile in the project root folder and select Run as -> Maven build
Enter the string
clean installto the field labeled with Goals and click Run. This step compiles your CDS artifacts. Repeat this step once your CDS model changes.
Run and Test the Application
Right-click on the project root in the Package Explorer and select Run as > Spring Boot App
Call the application by pointing your browser to http://localhost:8080/