Description
Create a simple Node.js project and a simple Java project. Pack them in a unique MTA. Include some simple environment variables. Build and deploy the application to Cloud Foundry.
Prerequisites
For the complete execution of current exercise, you must execute the following other exercise first:
Creating your Pay-As-You-Go Account in SAP BTP
As development environment, this exercise uses the SAP Business Application Studio. In case the studio is not available for any reason, you can execute the same steps locally, using Visual Studio Code. This will require minimal intuitive differences in the environment usage, and no difference in the source code.
If you didn't do this in previous exercises, you now create the initial application project: Open your IDE (SAP Business Application Studio), then clone the course GitHub repository to a local folder.
Use the following information:
Information | Value |
---|---|
GitHub repository URL | https://github.com/SAP-samples/cloud-sdk-learning-journey.git |
choose Clone from Git, enter the repository URL, open the cloned repository
Optional: final application
In case you wish to build and deploy the application to Cloud Foundry directly in it's final status without going through the exercise steps, then complete the following actions (this is not a prerequisite to exercise execution):
- Clone GitHub repository https://github.com/SAP-samples/cloud-sdk-learning-journey.git
- Open a terminal window and change the directory to the project root folder.
- Checkout the branch that represents the final step:
- Execute the following sequence of commands:
Task 1: Create the MTA Project and Modules
Steps
Open the SAP Business Application Studio, open the training development space.
In your Web browser (for example, Google Chrome), open your SAP BTP Cockpit based on the link you received at subscription time (for example: https://cockpit.eu20.hana.ondemand.com). If requested, enter your user and password.
Choose the training subaccount, that you created in a previous exercise.
Choose Services → Instances and Subscriptions.
Choose SAP Business Application Studio.
Eventually start the training development space.
Once the space is running, click on training to enter the development space.
In your IDE (SAP Business Application Studio), you need to setup the two applications: app1 is the Node version and app2 is the Java version.
Switch to Git branch
mta_v1.0_hello
. You choose the branch in the left corner of the bottom bar, or you open a Terminal and run:Check that the two applications are present:
app1
andapp2
.
Review, install and run the Node.js application (Hello Node!)
In project explorer, go to
app1
folder.Review the
main.js
andpackage.json
files.To start the application, open a terminal window and run:
Once the application has started, a pop up window appears stating
A service is listening to port 3002.
Choose Open in New Tab. A new tab is opened showingHello Node!
.Return to the Studio. In the terminal, choose Ctrl + C until the program stops.
Review, build and run the Java application (Hello Java!)
Review the source code under
app2/main/java/com/example/demo
directory.In the terminal execute the following commands to build the application:
Once the application has started, a pop up window appears stating:
A service is listening to port 8080.
Choose Open in New Tab. A new tab is opened showingHello Java!
.Return to the Studio. In the terminal, choose Ctrl + C until the program stops.
Optional: In the Web browser, access the https://start.spring.io website, then generate and download a Spring Boot application based on the following information:
Setting Value Project Maven Project Language Java Spring Boot latest version 2 (currently 2.7.10) Project Metadata leave the defaults Packaging War Java 8 Dependencies Spring Web Note
This step is optional, it explains the necessary steps needed to create theapp2
Java application from scratch using the Spring Initializr platform. In case you are not interested, just skip the step and continue at the following one.In your Web browser, open https://start.spring.io.
Choose the settings as described in the provided table.
Choose Generate. A file named
demo.zip
is downloaded.Upload the whole content of the
demo.zip
archive to you root project folder in SAP Business application Studio (in Windows, just extract the archive to a local folder, then drag and drop the whole content to the Studio).Create a new file
demo/main/java/com/example/demo/DemoController.java
.Copy its content from GitHub at DemoController.java.
Now the
demo
folder contains the same java app as the as theapp2
folder. Build and run the application, executing the following commands:Cleanup before you continue: stop the running program, delete the
demo
folder.
The two applications are independent from each other and must have separate run configurations. Business Application Studio is based on Visual Studio Code, which means the same running configurations apply. Visual Studio Code keeps the running/debugging configuration in a launch.json file. This file is located under .vscode folder in the workspace (root folder of the project).
Choose Run → Open Configurations. The
mta-intro/.vscode/launch.json
file is opened.Note
Running configuration file can also be reviewed in GitHub repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v1.0_hello/.vscode/launch.jsonReview the two running configurations: Launch Application 1 and Launch Application 2.
Checkout now the next step branch.
Review the new file
mta.yaml
. This represents our MTA configuration for the two application projects.Note
MTA configuration file can also be reviewed in GitHub repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v2.0_mta/mta.yamlThe configuration uses the following application names:
- Name of the overall MTA:
mta-intro
- Name of the Node.js module:
mta-intro-app1
- Name of the Java module:
mta-intro-app2
- Name of the overall MTA:
Build the MTA in a
.mtar
archive.In the Studio, choose Terminal → New Terminal
In the terminal, execute the following commands:
The two module projects are built and stored in a unique file named:mta_archives/mta-intro_0.0.1.mtar
Deploy the
.mtar
archive to Cloud Foundry.In the terminal, execute the following commands:
On log-in, you may be requested to enter your Cloud Foundry API Endpoint, email, and password.
The archive is then deployed. The two module applications are created in Cloud Foundry.
Look for the running applications in the SAP BTP Cockpit and open them.
In a web browser, open your SAP BTP Cockpit based on the URL you received at subscription time (for example: https://cockpit.eu20.hana.ondemand.com. If required, enter your e-mail and password.
In the SAP BTP Cockpit, navigate to the training Sub-account. Navigate to the dev space.
Choose the mta-intro-app1 application name.
Choose the Application Route. The application shows
Hello Node!
.Go back and choose the mta-intro-app2 application name.
Choose the Application Route. The application shows
Hello Java!
.
Task 2: Define Environment Variables as MTA Properties
Steps
Checkout the next step branch, which covers the next part where we enhance the both applications to read environment variables.
Review the updated
launch.json
file, containing the run configurations we use during development.Note
Thelaunch.json
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v3.0_variables/.vscode/launch.jsonWe remark a new property
env
defined for both configurations. We want each application to read theHello!
sentence from the environment variableTITLE
. In order to achieve this, we addedTITLE
variable underenv
property (it applies for each configuration, remember the two configurations are independent from each other).Now that we saw how
TITLE
environment variable was set for each application, let's see how it can be read. In the Studio, go toapp1
folder and review themain.js
file.Note
Themain.js
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v3.0_variables/app1/main.jsNote that
Hello Node
value is not hard coded anymore. The value is now read from theTITLE
environment variable.Review the changes from java application. Open
app2/src/main/java/com/example/demo/DemoController.java
file.Note
TheDemoController.java
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v3.0_variables/app2/src/main/java/com/example/demo/DemoController.javaNote that
Hello Java!
value is not hard coded anymore. The value is now read from theTITLE
environment variable.Build the app2, running the following commands in a terminal:
In the Studio, in the Run and Debug window, launch the two applications using the corresponding run configurations (they can run in parallel as the ports are different for each app). Verify that the titles appear correctly at run time. Note the browser URL of both applications. Stop the execution.
Note
Sometimes we experienced issues when the Node.js application is started due to the port 3002 being busy after the previous run. In this case, consider closing and restarting the Studio development space.
The variables from
launch.json
are local to the development environment, which means they will not be resolved when the application is deployed. In order to define variables that can be resolved by theCF deploy
application, we need to declare them inmta.yaml
file. Open the file and review the newproperties
field under each module. It contains theTITLE
variable as a property. Build and deploy the application.Note
Themta.yaml
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v3.0_variables/mta.yamlBuild and deploy the application running the following commands in the project root folder:
If there are any issues, log in to Cloud Foundry again.
Look for the running applications in the SAP BTP Cockpit and open them. Verify that the TITLE variable appears in the User-Provided Variables for both apps.
In the SAP BTP Cockpit, navigate to the dev space.
Choose the mta-intro-app1 application name.
Choose User-Provided Variables and verify that the TITLE variable is created.
Choose Overview, choose the Application Route. The application displays:
Hello Node!
.Repeat the same steps for the mta-intro-app2 application.
Task 3: Define Module Dependencies and Exchange Variables
Steps
This task enhances each application to provide navigation to the other application. Checkout the next step branch.
Open and review the updated
launch.json
fileNote
Thelaunch.json
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v4.0_formulas/.vscode/launch.jsonThe title of each application can be the hyperlink that references the other application. Remark that
launch.json
file was enhanced with the new variables for both apps (we use the names A1URL and A2URL). Replace these values with the corresponding URLs exposed by Studio for your running applications. Check that the applications run successfully.Note
If you don't use Business Studio Application and use local environment, please note that the protocol is mandatory to be set. Example:localhost:8080
is not enough, you need to also add thehttp
protocol. Example:Open
app1
folder and check the changes frommain.js
file.Note
Themain.js
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v4.0_formulas/app1/main.jsReview how
a2url
value is set from the environment variable. Thepage
content includes now a hyperlink that points to the value set in thea2url
variable.Open
app2/src/main/java/com/example/demo/DemoController.java
file.Note
TheDemoController.java
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v4.0_formulas/app2/src/main/java/com/example/demo/DemoController.javaReview how
a1url
value is set from the environment variable. Thepage
content includes now a hyperlink that points to the value set in thea1url
variable.Build the app2, running the following commands in a terminal:
In Studio, in the Run and Debug window, run the applications in parallel using the corresponding run configurations. Verify that the hyperlinks appear correctly at run time. Stop the execution.
Each application link is determined at deploy time, so it needs to be stored in an environment variable so that it can be resolved during deployment. As we already saw, the static variables are defined in
launch.json
file, but the variables to be resolved at deploy time need to be set inmta.yaml
file configuration. Openmta.yaml
file .Note
Themta.yaml
file can be reviewed also in GitHub Repository https://github.com/SAP-samples/cloud-sdk-learning-journey/blob/mta_v4.0_formulas/mta.yamlReview the definition of
A1URL
andA2URL
. Remark that the values are not static anymore and will be dynamically resolved at deploy time.Build and deploy the application using the following commands:
In case anything goes wrong, log in to Cloud Foundry again.
Look for the running applications in the SAP BTP Cockpit and open them. Verify that the A1URL and A2URL variables appear in the User-Provided Variables with proper values.
In the SAP BTP Cockpit, navigate to the dev space.
Choose the mta-intro-app1 application name.
Choose User-Provided Variables and verify that the A2URL variable is created.
Choose Overview → Application Route. The application shows
Hello Node!
. Then it shows a link Navigate to Application 2. If you click on the link, theHello Java!
application displays.Repeat the same steps for the mta-intro-app2 application.