Skip to main content
Version: v3

Developing with Java (Spring Boot)

Step 0. Creating the initial source code (optional)

We will create the example source code by using some popular frameworks.

Before we begin, we will create a new directory and cd into it.

mkdir quickstart-demo && cd quickstart-demo

This is optional and you may use an existing project instead (make sure you cd into the project directory before running any odo commands) or a starter project from odo init.

For Java, we will use the Spring Initializr to generate the example source code:

  1. Navigate to start.spring.io.
  2. Select Maven under Project.
  3. Under Spring Boot select 2.7.* version.
    caution

    For now, don't use SpringBoot 3. Currently there is no Devfile in registry.devfile.io with Java 17.

  4. Select 11 under Java.
  5. Click on "Add" under "Dependencies".
  6. Select "Spring Web".
  7. Click "Generate" to generate and download the source code.

Finally, extract the downloaded source code archive in the 'quickstart-demo' directory.

Your source code has now been generated and created in the directory.

Step 1. Connect to your cluster and create a new namespace or project

Before starting you should make sure that odo is connected to your cluster and that you have created a new namespace.

Creating a new namespace

If you are using Kubernetes, you can create a new namespace with the odo create namespace command.

odo create namespace odo-dev
Sample Output
$ odo create namespace odo-dev
✓ Namespace "odo-dev" is ready for use
✓ New namespace created and now using namespace: odo-dev

Step 2. Initializing your application (odo init)

Now we'll initialize your application by creating a devfile.yaml to be deployed.

odo handles this automatically with the odo init command by autodetecting your source code and downloading the appropriate Devfile.

Note: If you skipped Step 0, select a "starter project" when running odo init.

Let's run odo init and select Java (Spring Boot):

odo init
Sample Output
$ odo init
__
/ \__ Initializing a new component
\__/ \ Files: Source code detected, a Devfile will be determined based upon source code autodetection
/ \__/ odo version: v3.10.0
\__/

Interactive mode enabled, please answer the following questions:
Based on the files in the current directory odo detected
Language: Java
Project type: springboot
The devfile "java-springboot:1.2.0" from the registry "DefaultDevfileRegistry" will be downloaded.
? Is this correct? Yes
✓ Downloading devfile "java-springboot:1.2.0" from registry "DefaultDevfileRegistry" [3s]

↪ Container Configuration "tools":
OPEN PORTS:
- 8080
- 5858
ENVIRONMENT VARIABLES:
- DEBUG_PORT = 5858

? Select container for which you want to change configuration? NONE - configuration is correct
? Enter component name: my-java-app

You can automate this command by executing:
odo init --name my-java-app --devfile java-springboot --devfile-registry DefaultDevfileRegistry --devfile-version 1.2.0

Your new component 'my-java-app' is ready in the current directory.
To start editing your component, use 'odo dev' and open this folder in your favorite IDE.
Changes will be directly reflected on the cluster.
note

If you skipped Step 0 and selected "starter project", your output will be slightly different.

Step 3. Developing your application continuously (odo dev)

Now that we've generated our code as well as our Devfile, let's start on development.

odo uses inner loop development and allows you to code, build, run and test the application in a continuous workflow.

Once you run odo dev, you can freely edit code in your favourite IDE and watch as odo rebuilds and redeploys it.

Let's run odo dev to start development on your Java (Spring Boot) application:

odo dev

Then wait a few seconds until odo dev displays Forwarding from 127.0.0.1:... in its output, meaning that odo has successfully set up the port forwarding to reach the application running in the container.

Sample Output
$ odo dev
__
/ \__ Developing using the "my-java-app" Devfile
\__/ \ Namespace: odo-dev
/ \__/ odo version: v3.10.0
\__/

↪ Running on the cluster in Dev mode
• Waiting for Kubernetes resources ...
✓ Added storage m2 to component
⚠ Pod is Pending
✓ Pod is Running
✓ Syncing files into the container [167ms]
✓ Building your application in container (command: build) [3m]
• Executing the application (command: run) ...
✓ Waiting for the application to be ready [1s]
- Forwarding from 127.0.0.1:20001 -> 8080


↪ Dev mode
Status:
Watching for changes in the current directory /home/user/quickstart-demo

Keyboard Commands:
[Ctrl+c] - Exit and delete resources from the cluster
[p] - Manually apply local changes to the application on the cluster

You can now access the application via the local port displayed by odo dev (127.0.0.1:20001 in the sample output above) and start your development loop. odo will watch for changes and push the code for real-time updates.

You can now follow the advanced guide to deploy the application to production.