Project: make ShopLite environment-ready
60 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
$ 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.ymlreplaces the.propertiesfiles: a shared part, adevdocument and aproddocument (spring.config.activate.on-profile). - 3Every environment-specific value is a
${VARIABLE}placeholder with exactly the namesDB_URL,DB_USER,DB_PASSWORD,JWT_SECRET,CORS_ALLOWED_ORIGINS,PORT,LOG_LEVEL;devis the default profile and servers setSPRING_PROFILES_ACTIVE=prod. - 4Only safe values have defaults (
jdbc:mysql://localhost:3306/shoplite,root,8080,info/debug);DB_PASSWORDandJWT_SECREThave none, and the prod document redeclares the database URL and user without defaults. - 5
.env.example(names and safe examples only) is committed;.envis listed in.gitignore, andgit check-ignore -v .envproves it. - 6
scripts/EnvCheck.javaprints which variables are set, never their values, and runs ineclipse-temurin:25-jdkwith--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.xmlis1.2.0, so the jar istarget/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 oneList every value that differs between environments
A table in your notes with each setting, its variable name and its dev, test and prod value.
Move the shared settings into application.yml
mvn spring-boot:runstarts with the dev profile and/actuator/healthanswers{"status":"UP"}.Add the dev and prod profile documents
Dev loads sample data and logs at DEBUG; prod has no defaults and no API docs.
Commit .env.example and keep .env out of Git
git check-ignore -v .envnames the rule, and.env.exampleshows every variable without a real secret.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.
Make
EnvCheckalso print the active profile and, when it isprod, exit with code 1 and a list of every missing prod variable.· easySpeed up the container build by reusing your local Maven downloads between runs.· medium
Add a
@Profile("prod")start-up check that stops the app with a clear message whenDB_URL,DB_USER,DB_PASSWORDorCORS_ALLOWED_ORIGINSis missing.· medium
Solution
Opens when you add your repository link.
Finish up
Mark complete & continue
Next: docker run, ps, stop, rm and logs