Develop with SAP Cloud SDK for Java

Objectives

After completing this lesson, you will be able to:
  • Identify the developer tools required to build Java applications in SAP Cloud SDK
  • Describe basic concepts to develop with the SAP Cloud SDK for Java

SAP Cloud SDK developer tools

Java development environment prerequisites checklist: Includes Java SE Development Kit, command line tools (Maven, Git, Cloud Foundry CLI), and recommended Integrated Development Environments (IDEs) like SAP Business Application Studio, Visual Studio Code, IntelliJ IDEA, or Eclipse.

A minimal set of prerequisites must be installed on our developer machine to build applications using the SAP Cloud SDK.

Such tools are already installed within SAP Business Application Studio, nevertheless they need to be installed if you want to work with a local environment running on your laptop (i.e. Visual Studio Code).

The first thing that we need is the Java Development Kit (JDK). The JDK is necessary for compiling Java applications and allows us to run Java applications locally. If you want to run your programs in the Cloud Foundry environment of SAP BTP, various Java versions are possible (default is version 8), for details, have a look at the documentation of the SAP Java Buildpack.

Next, we require Apache Maven version 3.5 or above. Maven is a build automation tool for Java projects and other programming environments. Maven manages project structures, dependencies to other libraries, and the build lifecycle of a Java project. Moreover, Maven is able to generate projects using "archetypes," which are provided by the SAP Cloud SDK.

In addition to Maven, we set up Git as the distributed version control system. A version control system like Git is a key component for realizing continuous delivery and DevOps. For instance, Git enables collaboration in development teams and forms the basis for any continuous delivery pipeline.

We also need a few additional tools as we go beyond the local development and testing stages. In particular, we need the Cloud Foundry command line interface (CLI). The Cloud Foundry CLI enables us to interact with Cloud Foundry from the command line. Using this tool, we can deploy, start and stop applications, consume services, and do much more, using simple commands on our local command line. In principle, we can perform many of these tasks also with the SAP Business Technology Platform (BTP) cockpit. However, for automating tasks, Cloud Foundry CLI is a crucial tool.

Finally, we can set up an integrated development environment (IDE). While using an IDE instead of a simple text editor to write code is entirely optional, an IDE greatly simplifies the development lifecycle. The SAP Cloud SDK does not make any assumptions about the IDE that should be used. This means that we are free to choose any IDE that we like, for example, IntelliJ IDEA or Visual Studio Code, or Eclipse. These tools are useful in any Java-based development project, whether you are using the SAP Cloud SDK or not.

Introducing Apache TomEE

Apache TomEE (pronounced "Tommy") is the Java Enterprise Edition of Apache Tomcat (Tomcat + Java EE = TomEE) that combines several Java enterprise projects including Apache OpenEJB, Apache OpenWebBeans, Apache OpenJPA, Apache MyFaces, and others. In October 2011, the project obtained certification by Oracle Corporation as a compatible implementation of the Java EE 6 Web Profile.

Screenshot of a webpage displaying a list of software components related to Apache TomEE. The list includes descriptions for various components such as Apache OpenEJB, Apache OpenWebBeans, Apache OpenJPA, Apache MyFaces, Apache Geronimo Transaction, Apache Geronimo Javamail, Apache BVal, Apache CXF, and Apache Derby. Each component is briefly explained, and the image also features the TomEE logo at the bottom.

You can find detailed information about Apache TomEE at https://tomee.apache.org.

Introducing the SAP buildpack for Java

SAP provides an enhanced buildpack for Java. The SAP buildpack provides additional containers for TomEE and TomEE7.

The standard Cloud Foundry buildpack for Java is called java_buildpack. This buildpack provides a container for Tomcat. You can find detailed information at: https://github.com/cloudfoundry/java-buildpack

SAP extended the standard buildpack and created a new one called sap_java_buildpack. This buildpack provides containers for Tomcat, TomEE, TomEE 7. You can find detailed information at https://help.sap.com/viewer/product/BTP then navigate to DevelopDevelop ApplicationsDevelopment in the Cloud Foundry EnvironmentDeveloping Applications and ServicesDevelopment LanguagesDeveloping Java in the Cloud Foundry Environment(link).

Introducing Apache Maven

Maven is a build automation tool used primarily for Java projects. The Maven project is hosted by the Apache Software Foundation.

Maven addresses two aspects of building software: how software is built, and its dependencies.

Unlike earlier tools like Apache Ant, Maven uses conventions for the build procedure, and only exceptions need to be written down. An XML file (named pom.xml) describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories, such as, the Maven 2 Central Repository, and stores them in a local cache. This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.

The basic Maven command you'll use to build a Java application is:

Code Snippet
1
mvn clean install -U

Further information to use Maven can be found in the official web site at https://maven.apache.org.

Deploying your application to Cloud Foundry

Snippet of a YAML file named ‘manifest.yml’ used for configuring an application deployment. The file specifies settings for an application named ‘java-hello’, including memory allocation (1024M), a timeout (300 seconds), and a random route (true). It also defines the path to the application’s WAR file (‘target/java-hello.war’) and lists two buildpacks (‘sap_java_buildpack’ and ‘_sap_java_buildpack’). Additionally, environment variables are set for logging level and Java BuildPack memory sizes.

The project is deployed to Cloud Foundry based on the deployment information specified in the manifest.yml file by executing cf push. For example, given the information specified in the figure:

  1. The "java-hello.war" file is uploaded to Cloud Foundry.
  2. Based on the given buildpack, a proper runtime environment is prepared for the application (the application is "staged").
  3. The application is created and started based on provided name, memory, route, environment variables, and so on.

Introducing Spring

The Spring Framework is an open source application framework for the Java platform.

It provides comrehensive infrastructure support for developing Java applications.

It also handles the infrastructure so that the developers can focus on the application.

It is developed by Pivotal Software and distributed under the open source Apache Licence 2.0.

Spring Boot is used to build stand-alone and production-ready spring applications. In particular, Spring Boot Web applications are recognized by common development environments and run with one click, helping the developer to be more productive.

Additional information about Spring is available at http://spring.io.

Remote debugging of a Java application

Debug with Visual Studio Code

Let's examine debugging using the Visual Studio Code debugger.

Screenshot of a code editor displaying a JSON configuration file. The file includes settings related to debugging in a Java environment. Notable properties include the version (0.2.0), a configuration for attaching a Java debugger, and source paths specified as ${workspaceFolder}.

To debug using Visual Studio Code debugger, complete the following steps:

  1. Choose RunOpen ConfigurationsJava.
  2. Enter the following configuration:
    Code Snippet
    12345678910
    { "type": "java", "name": "Attach Java Debugger", "request": "attach", "hostName": "localhost", "port": 5005, "sourcePaths": [ "${workspaceFolder}" ] }
  3. Switch to the debugger by choosing the proper icon.
  4. Choose Attach Java Debugger from the drop-down control.
  5. Choose Start Debugging.

Debug with IntelliJ IDEA

Let us move on to debugging using the IntelliJ IDEA debugger.

Screenshot of the Run/Debug Configurations window in an Integrated Development Environment (IDE) showing settings for attaching a Remote Java Debugger.

To debug using IntelliJ IDEA debugger, complete the following steps:

  1. Choose RunEdit Configurations.
  2. In the Run/Debug Configurations window, add a new configuration based on the following information:
    • Name: Attach Java Debugger
    • Debugger mode: Attach to remote JVM
    • Host: localhost
    • Port: 5005
    • User module classpath: << your root project folder>>
  3. Choose Attach Java Debugger from the drop-down control.
  4. Choose Debug 'Attach Java Debugger'.

Remote Debugging of Applications Running in Cloud Foundry

We wish to debug the application running in Cloud Foundry using the debugger installed on our local computer, for example, Eclipse, IntelliJ IDEA, or Visual Studio Code.

Flowchart outlining four-step debugging process for Cloud Foundry applications using Java EE, involving enabling debug options on both Java and Cloud Foundry applications, establishing an SSH connection, and attaching a local debugger.

Use the following steps:

  1. Enable the Java application for debug by executing the following:
    Code Snippet
    1
    cf set-env <<application name>> JBP_CONFIG_JAVA_OPTS "[java_opts: '-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n']"
  2. Enable the Cloud Foundry application for debug by executing the following:
    Code Snippet
    12
    cf enable-ssh <<application name>> cf restage <<application name>>
  3. Establish an SSH connection by executing the following:
    Code Snippet
    1
    cf ssh -N -T -L <<localport>>:localhost:8000 <<application name>>
  4. Attach the Debugger.

Log in to track your progress & complete quizzes