SAP Cloud Application Programming Model in Java
Initialize the application using maven arcetype cds-services-archtype
.
> mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds \
-DarchetypeVersion=RELEASE \
-DgroupId=com.sap.cap -DartifactId=products-service -Dpackage=com.sap.cap.productsservice
Alternative.
> cds init products-service --add java --java:package com.sap.cap.productsservice
Install SQLite.
> npm install --save-dev sqlite3
Initialize the database with the defined domain model.
> cds deploy --to sqlite
Configure CAP application to use SQLite database.
-
Go to
srv/src/main/resources
, locate and open theapplication.yaml
and modify the file as follow: -
For the field
url
, replace the string"jdbc:sqlite::memory:"
with a reference to your local database file"jdbc:sqlite:/home/user/projects/products-service/sqlite.db"
-
Set the value of
initialization-mode
fromalways
tonever
, because you've already initialized the database when runningcds deploy --to sqlite
.
---
spring:
profiles: default
datasource:
url: "jdbc:sqlite:/home/user/projects/products-service/sqlite.db"
driver-class-name: org.sqlite.JDBC
initialization-mode: never
hikari:
maximum-pool-size: 1
Initialize the application using maven arcetype cds-services-archtype
.
> mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds \
-DarchetypeVersion=RELEASE \
-DgroupId=com.sap.cap -DartifactId=bookstore
Alternative.
> cds init bookstore --add java --java:package com.sap.cap.bookstore
Install the reusable service project as npm dependency
> npm install $(npm pack ../products-service -s)
Install all other packages and simplify the overall dependency structure npm dedupe
.
> npm install && npm dedupe
Install SQLite.
> npm install --save-dev sqlite3
Initialize the database with the defined domain model.
> cds deploy --to sqlite
Configure CAP application to use SQLite database.
-
Go to
srv/src/main/resources
, locate and open theapplication.yaml
and modify the file as follow: -
For the field
url
, replace the string"jdbc:sqlite::memory:"
with a reference to your local database file"jdbc:sqlite:/home/user/projects/products-service/sqlite.db"
-
Set the value of
initialization-mode
fromalways
tonever
, because you've already initialized the database when runningcds deploy --to sqlite
.
---
spring:
profiles: default
datasource:
url: "jdbc:sqlite:/home/user/projects/bookstore/sqlite.db"
driver-class-name: org.sqlite.JDBC
initialization-mode: never
hikari:
maximum-pool-size: 1
-
Login to Cloud Foundry with CF CLI.
-
Install hdi-deploy node package:
> npm install --save-dev @sap/hdi-deploy
-
Add following configuration to file
.cdrsc.json
in root folder ofbookstore
project.{ "hana" : { "deploy-format": "hdbtable" } }
.hdbtable
and.hdbview
files will be generated in the(gen/)db/src/gen/
folder. -
Create an SAP HANA service instance and implicitly push all artifacts to the database using:
> cds deploy --to hana:bookstore-hana
-
Configure application to use SAP HANA locally. Edit
pom.xml
in thesrv
directory and add the<dependencies>
tag.<dependency> <groupId>com.sap.cds</groupId> <artifactId>cds-feature-hana</artifactId> </dependency>
-
Restart the application with SAP HANA connectivity.
> mvn spring-boot:run -Dspring-boot.run.profiles=cloud
-
Create a CF app manifest called
manifest.yml
file inbookstore
project folder.--- applications: - name: bookstore path: srv/target/bookstore-exec.jar random-route: true services: - bookstore-hana
-
Enable auto-configuration of SAP HANA db connection. Add the following dependency under
<dependencies>
tag and save it topom.xml
file.<dependency> <groupId>com.sap.cds</groupId> <artifactId>cds-feature-cloudfoundry</artifactId> </dependency>
-
Build the application.
> mvn clean install
-
Push the application to the cloud.
> cf push
-
To retrieve the application URL.
> cf app bookstore
Locate in ".vscode/launch.json"
.
{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - Application",
"request": "launch",
"mainClass": "com.sap.cap.bookstore.Application",
"projectName": "bookstore"
},
{
"name": "Run bookstore",
"type": "java",
"request": "launch",
"mainClass": "com.sap.cap.bookstore.Application",
"projectName": "bookstore",
"preLaunchTask": "Build bookstore",
"cwd": "${workspaceFolder}",
"args": [
"--spring.profiles.active=default"
],
"env": {
"run.config": "{\"handlerId\":\"cap_run_config_handler_id\",\"runnableId\":\"{\\\"projectPath\\\":\\\"/path_to_bookstore\\\",\\\"profileName\\\":\\\"default\\\"}\"}"
}
}
],
"version": "0.2.0"
}
Locate in ".vscode/tasks.json"
.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build products-service",
"type": "shell",
"command": "mvn clean install",
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}