Skip to lesson

Project: make ShopLite environment-ready

An app is environment-ready when every value that changes between dev, test and prod comes from outside the build, so the very same jar runs on a laptop, in CI and on a server, and only its environment variables differ.

What you'll build

You start from shoplite-api as the Spring Security section left it: JWT sign-in, roles, owner checks and CORS on Spring Boot 4.1.1 with MySQL and Flyway. Its settings are spread over several .properties files (a shared one and one per profile), some values are written for one laptop, and a new teammate cannot tell which variables to set. That is the "works on my machine" problem in configuration form.

You list every setting that differs between dev, test and prod, then move all of them into one application.yml where each comes from an environment variable: safe dev defaults for the database URL, user, port and log level, no usable default for the password and JWT key, and no defaults at all in the prod profile. You commit .env.example, keep the real .env out of Git, add an "Environments" table to the README, and prove with docker run eclipse-temurin:25-jdk that the project builds on a computer without a JDK. The files are reference files: build and run them in your own repository.

Sample run (times and versions will differ)
$ cp .env.example .env              # then put your own DB_PASSWORD in .env
$ git check-ignore -v .env
.gitignore:2:.env	.env

# Build the jar with no JDK on the computer, only Docker
$ docker run --rm -v "$PWD":/app -w /app eclipse-temurin:25-jdk sh mvnw -q -DskipTests package
$ ls target/*.jar
target/shoplite-api-1.2.0.jar

# JWT_SECRET is still empty in .env: the app refuses to start
$ java -jar target/shoplite-api-1.2.0.jar
...
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'shoplite.jwt' to com.shoplite.auth.JwtProperties:

    Reason: java.lang.IllegalArgumentException: shoplite.jwt.secret is empty. Set the JWT_SECRET environment variable.

Action:

Update your application's configuration

# After adding JWT_SECRET=<output of openssl rand -base64 32> to .env
$ docker run --rm -v "$PWD":/app -w /app --env-file .env eclipse-temurin:25-jdk java scripts/EnvCheck.java
DB_URL                not set
DB_USER               not set
DB_PASSWORD           set
JWT_SECRET            set
CORS_ALLOWED_ORIGINS  not set
Java 25.0.1 on Linux

# Same jar, dev profile, values from .env and the dev defaults
$ java -jar target/shoplite-api-1.2.0.jar
... INFO ... com.shoplite.ShopliteApiApplication      : No active profile set, falling back to 1 default profile: "dev"
... INFO ... o.s.b.w.e.tomcat.TomcatWebServer         : Tomcat started on port 8080 (http) with context path '/'
... INFO ... com.shoplite.ShopliteApiApplication      : Started ShopliteApiApplication in 6.84 seconds (process running for 7.42)

# Another terminal
$ curl -s localhost:8080/actuator/health
{"status":"UP"}

Requirements

  • 1A list of every environment-specific value in shoplite-api: database URL, user and password, JWT key, CORS origins, profile, port and log level, with what each one is in dev, test and prod.
  • 2One src/main/resources/application.yml replaces the .properties files: a shared part, a dev document and a prod document (spring.config.activate.on-profile).
  • 3Every environment-specific value is a ${VARIABLE} placeholder with exactly the names DB_URL, DB_USER, DB_PASSWORD, JWT_SECRET, CORS_ALLOWED_ORIGINS, PORT, LOG_LEVEL; dev is the default profile and servers set SPRING_PROFILES_ACTIVE=prod.
  • 4Only safe values have defaults (jdbc:mysql://localhost:3306/shoplite, root, 8080, info/debug); DB_PASSWORD and JWT_SECRET have none, and the prod document redeclares the database URL and user without defaults.
  • 5.env.example (names and safe examples only) is committed; .env is listed in .gitignore, and git check-ignore -v .env proves it.
  • 6scripts/EnvCheck.java prints which variables are set, never their values, and runs in eclipse-temurin:25-jdk with --env-file .env.
  • 7The README has an "Environments" table and a "No JDK? Use Docker" part; the jar builds with docker run ... eclipse-temurin:25-jdk sh mvnw -q -DskipTests package.
  • 8The version in pom.xml is 1.2.0, so the jar is target/shoplite-api-1.2.0.jar, and the work is pushed on a branch with a pull request.

Milestones

0/5 done · commit and push after each one
  1. List every value that differs between environments

    A table in your notes with each setting, its variable name and its dev, test and prod value.

  2. Move the shared settings into application.yml

    mvn spring-boot:run starts with the dev profile and /actuator/health answers {"status":"UP"}.

  3. Add the dev and prod profile documents

    Dev loads sample data and logs at DEBUG; prod has no defaults and no API docs.

  4. Commit .env.example and keep .env out of Git

    git check-ignore -v .env names the rule, and .env.example shows every variable without a real secret.

  5. Prove it with Docker and document it in the README

    EnvCheck runs in eclipse-temurin:25-jdk, the jar builds there, and the README explains both.

Build it in your own GitHub repository and push after each milestone. When everything works, mark the project as complete.

Extra challenges (optional)

Do any you like, each on its own branch, so your main project keeps working.

  1. Make EnvCheck also print the active profile and, when it is prod, exit with code 1 and a list of every missing prod variable.· easy

  2. Speed up the container build by reusing your local Maven downloads between runs.· medium

  3. Add a @Profile("prod") start-up check that stops the app with a clear message when DB_URL, DB_USER, DB_PASSWORD or CORS_ALLOWED_ORIGINS is missing.· medium

Solution

Opens when you add your repository link.

Finish up

Mark complete & continue

Next: docker run, ps, stop, rm and logs