Sub topic 1
Building with Maven or Gradle and the Spring Boot plugin
Prerequisites
Assume you have a JDK, a Spring Boot application, and basic Maven or Gradle experience. You should also know how to run commands from the project directory.
Build the application
The Spring Boot plugin adds packaging tasks to your build tool. It collects your application classes and dependencies into an executable JAR. A JAR is a Java archive that can run with the Java runtime.
Use the wrapper included in your project so the build uses the expected Maven or Gradle version:
# Maven
./mvnw clean package
# Gradle
./gradlew clean bootJarclean removes older build output. package creates the Maven artifact. bootJar creates the Spring Boot executable JAR. The result is usually under target/ for Maven or build/libs/ for Gradle.
Run the executable JAR
Run the generated file with java -jar. The exact filename depends on your project version.
java -jar target/myapp-1.0.0.jarSpring Boot starts its embedded web server, so you do not need to install an application server. You can pass configuration as a command-line argument. For example:
java -jar target/myapp-1.0.0.jar \
--spring.profiles.active=prodA profile is a named set of configuration values, such as dev or prod. Command-line arguments override many lower-priority configuration sources.
Keep configuration outside the artifact
Build one artifact and configure it differently in each environment. Do not rebuild the JAR just to change a port or database address.
# application.properties
server.port=8080
spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev}
spring.datasource.url=${DB_URL}
spring.datasource.password=${DB_PASSWORD}${NAME} reads an environment variable at startup. The optional :dev value provides a default when the variable is missing.
Handle secrets safely
Passwords, API keys, and tokens are secrets. Keep them out of source control, application properties committed to Git, container images, and command histories. Supply them through a secret manager or protected environment variables. Restrict access to the process and rotate secrets when needed.
Never log a resolved secret while diagnosing configuration. Also validate required values at startup so a misconfigured service fails clearly instead of connecting to the wrong system.
Exercise
Why should a production database password not be placed directly in application.properties?
Show answer
The file may enter source control or an artifact. Reference a protected environment variable or secret manager instead.