Guide

December 21, 2023

Launch a Java Spring Boot App

Build and Deploy a Java app using the Spring Boot framework. This Template comes configured with a Postgres database.

Overview

When using Noop, container-based Java developent is easy.

This guide is loosely based on the Spring Boot User Registration and Login Example Tutorial published by JavaGuides.

The container setup utilized for this guide is informed by the official spring guide. Ignore the comment about container complexity — Noop manages that.

In fact, if you’re interested in starting a Java Spring Boot app from scratch, all you need to do is:

The create-Template process in Workshop will automate everything outlined in this guide.

If, on the other hand, you want to modify an existing project to use Noop, the guide here can be used as reference.

The full source code constructed for this guide, including the Noop configuration, see the Template repository.

Setup

Noop Dependencies

Make sure you have Workshop installed locally or a Github account connected to Noop Cloud. The remaining setup is identical in both.

Project Setup

Ensure you have Java (preferably JDK 17 or later) and maven installed. When running maven commands from the terminal and not an IDE, make sure the JAVA_HOME environment variable resolves to the JDK 17 directory.

In the project root directory, to pull in plugins and dependencies, run:

mvn package -DskipTests

When maven has finished running, it will output an executable jar to the target folder within the project base directory.

To start the spring boot application server, run the following from the project root:

java -jar ./target/springboot-0.0.1-SNAPSHOT-exec.jar

Noop Workshop and Cloud will auto-provision a Postgres database and dynamically inject relevant environment variables into the application container. If you have your own Postgres database running through psql or a docker container, consider editing the default values in the application config in accordance with your setup:

  datasource:
    url: jdbc:postgresql://${POSTGRES_HOST:localhost}:${POSTGRES_PORT:5432}/${POSTGRES_DATABASE:}
    username: ${POSTGRES_USER:postgres}
    password: ${POSTGRES_PASSWORD:example}
    driver-class-name: org.postgresql.Driver

Template Configuration

After you have the Spring Boot project set up, you ned to add the Noop Template configuration to create a local or cloud Deployment.

Create a .noop directory in your project root.

Next, in the .noop directory create a blueprint.yaml file to define the application setup.

The content below is what is used for this app:

---
components:
  - name: SpringApp
    type: service
    image:  maven:3-eclipse-temurin-17-alpine
    port: 8080
    build:
      steps:
        - directory: /app
        - copy: [pom.xml]
          destination: ./
        - copy: src
          destination: ./src
        - run: mvn package -DskipTests
        - image: eclipse-temurin:17-jdk-alpine
          stage: spring
        - copy: /app/target/*-exec.jar
          destination: /application.jar
          from: main
    runtime:
      command: java -jar /application.jar
      resources:
        - PgDatabase
      variables:
        POSTGRES_HOST:
          $resources: PgDatabase.host
        POSTGRES_USER:
          $resources: PgDatabase.username
        POSTGRES_PASSWORD:
          $resources: PgDatabase.password
        POSTGRES_PORT:
          $resources: PgDatabase.port
        POSTGRES_DATABASE:
          $resources: PgDatabase.database
        PORT: 8080
routes:
  - target:
      component: SpringApp

resources:
  - name: PgDatabase
    type: postgresql

The POSTGRES variables in this yaml file inject the environment configuration values we need for spring-jpa autoconfiguration on application startup.

The Spring application configuration will read noop database bindings for environment variables defined for the database host, database name, user, and password. Default values are provided in case the application container starts up prior to database initialization.

Automating Cloud Setup

For Cloud users, most of the Application setup is automated, including Environment creation, database provisioning, building and deploying, etc., using a Runbook. To create a Runbook, make a file called quickstart.yaml in the .noop/runbooks directory.

Here is the Runbook content:

name: Quickstart Setup

description: Demo full stack setup of a Spring Boot application with internet endpoint

workflow:
  inputs:
    - name: EnvironmentName
      description: What should we name your new environment?
      type: string
      required: true
      default: Quickstart Demo

    - name: Cluster
      type: Cluster
      description: Which cluster should the environment launch on?
      required: true

  timeout: 300

  steps:
    - name: Environment
      action: EnvironmentCreate
      params:
        name:
          $inputs: EnvironmentName
        production: false
        appId:
          $runbook: Application.id
        clusterId:
          $inputs: Cluster.id

    - name: Build
      action: BuildExecute
      params:
        sourceCodeId:
          $runbook: SourceCode.id
        appId:
          $runbook: Application.id

    - name: Resources
      action: ResourceLaunch
      params:
        envId:
          $steps: Environment.id
        sourceCodeId:
          $runbook: SourceCode.id

    - name: Deploy
      action: DeploymentExecute
      params:
        envId:
          $steps: Environment.id
        buildId:
          $steps: Build.id

    - name: Endpoint
      action: InternetEndpointRandom
      params:
        orgId:
          $runbook: Organization.id
        routes:
          - name: 'Demo Environment'
            target:
              environments:
                - $steps: Environment.id

Extending the Blueprint

The spring-boot base setup for this Noop template, and all maven dependencies, were auto-generated using Spring Initializr. The application source is roughly based on this spring-jpa-thymeleaf tutorial.

To add additional dependencies or create your own spring microservice on Noop, the easiest way is to generate a spring-boot project through initializr and ensure you select “Project: Maven”, “Language: Java”, and “Packaging: Jar.” The Noop app manifest depends on the use of maven as a package manager and deploys using an executable jar.

The generated initializr project will be a zip file with boilerplate source and project dependencies. Follow spring best-practices and remove maven wrapper-scripts by deleting the following mvn directory and files:

  • .mvn/
  • mvnw
  • mvnw.bat

The spring-boot Noop template uses a multi-stage build process with the maven:3.8.5-eclipse-temurin-17-alpine image, so the maven wrapper scripts and jar are unnecessary.

In order to leverage the build and deployment process used in this spring-boot template’s app manifest, add the spring-boot-maven-plugin with an executable jar classifer to your pom.xml:

<plugins>
<!-- Include -->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>repackage</id>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

You can then proceed to build out your application, and even add multiple spring modules to your project, while maintaining the way in which we build and deploy the executable jar in this blueprint.

Launch on Noop

Cloud users can use the Runbook to automate most of the setup described below.

Create an Application

Select “New Application” and assign an appropriate name for your new app.

Cloud Setup

In Cloud, once the Application is created you will be asked if you want to execute a Runbook. The name of the Runbook for this Template is “Quick Start”.

Choose the default cluster and run it. Behind the scenes this Runbook is:

  • Creating an Environment
  • Setting up placeholders for required environment variables
  • Launching the database
  • Building the source code
  • Deploying the build
  • And finally, creating a public-facing endpoint to serve the application from

Once complete, visit the Application page to get the generated endpoint. Your Spring Boot app is now running at that URL.

The Noop Template Blueprint contains all the details about the application. Including build process, required resources, application routing rules, runtime configuration, etc.

Workshop Setup

While workshop doesn’t have a Runbook, the setup is also simple.

In Workshop select your Application, which should have been automatically detected by Workshop, or select the appropriate project directory (must include a .noop directory).

Workshop will automatically create a default environment with all Resources launched and an associated Endpoint.

Once done, you should see your application running on the associated endpoint.