Getting started with Java
The CAP Java SDK enables developing CAP applications in Java. While the SAP Web IDE provides excellent support to develop CAP Java applications, you can also develop locally with your tool of choice, for example Eclipse.
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 are not locked in to SAP Cloud Platform using a CAP Java applications.
Excited? The following sections describe how to set up a development environment to get you started.
Using Local Development
This section describes the prerequisites and tools to build a CAP application locally with Eclipse.
Install the CDS Compiler
Also for Java services, you need the CDS command-line tools.
→ Follow the steps in ‘Getting Started’
Install Java & Maven
- Download and install SapMachine 11.
- Install Apache Maven. Download the Binary zip archive version and save it to a local folder.
- Set environment variables for these tools:
- Windows in
setx M2_HOME "<maven folder>" setx PATH "%M2_HOME%\bin;%PATH%"
- Linux/Mac in
export JAVA_HOME=... export M2_HOME=... export PATH="$JAVA_HOME/bin:$M2_HOME/bin:$PATH"
- Windows in
- Log off and on again for these changes to become effective.
- Verify that
java -versionresults in a correct version output
mvn -versionresults in a correct version output
- Download the Eclipse package for Java developers.
- Unzip and start.
Add SAP and 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”. This provides some convenience when running Spring-based applications and unit tests.
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 URL
- Expand XS Tools for Eclipse, select SAP Cloud Business Application Tools for Eclipse.
- Click Next and Finish, accept the license agreement and warning about unsigned content, restart Eclipse.
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 simple CAP application based on Spring Boot, run the following Maven command:
mvn archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds -DarchetypeVersion=RELEASE -DincludeModel=true
This command generates a runnable “Hello World” CAP application containing a simple CDS model ready for enhancement with the following directory structure.
<PROJECT-ROOT>/ db/ srv/
dbfolder contains database-related content and is prepopulated with a simple CDS model located in the file
srvfolder contains the CDS service definition and Java back end code. Therefore, it’s prepopulated with a simple service model located in
cat-service.cdsand a small example application in
Work in Eclipse
- Import your newly generated project in Eclipse.
- Select “File -> Import… -> Existing Maven Projects”.
- Select your
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.
- Right-click on the
- 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: http://localhost:8080/odata/v4/CatalogService/$metadata
Java Project Structure
The imported Java project has the following structure:
srv/ src/main/java Contains Java application logic src/gen/java Contains compiled CDS model and compiled Java PoJos for model entities node Local Node.js installation to run the CDS compiler node_modules Node.js dependencies of the CDS compiler
Hint: To not be distracted by CDS compiler-specific folders in Eclipse, you could define Resource Filters for your project by right-clicking on the project root and selecting “Properties”. Go to “Resource -> Resource Filters” and exclude folders with the name