Maven Profiles vs Spring Profiles
Fast recall
Quick revision
Core answer
Conceptual clarity on a common point of confusion — this distinction trips up many freshers who see the word profile in both contexts
Using Maven profiles to manage runtime configuration like database URLs and credentials that should be handled by Spring profiles or externalized config
Explain when to use each: Maven profiles for build variants like including different dependencies or plugins, Spring profiles for runtime concerns like which DataSource bean to activate
Interview-ready guide
Interview answer
Maven profiles and Spring profiles solve different problems at different stages of the application lifecycle.
Maven profiles are build-time tools. When I run mvn package -Pprod, Maven decides what goes into the JAR — which dependencies to include, which plugins to run, how to filter property files. The output is a JAR that's already customized for production.
Spring profiles, on the other hand, are runtime tools. The same JAR can start with different Spring profiles active depending on environment variables. active equals prod, Spring loads the production beans and configuration classes.
If I run it with dev, Spring loads the development beans instead. Here's a concrete example. Let's say I'm working on an order processing service.
I'd use a Maven profile to include the Oracle JDBC driver dependency in production builds but use H2 for local development. That's a build decision — the two JARs literally have different dependencies inside them. Then I'd use Spring profiles to switch between a ProductionDataSource bean with connection pooling and a DevDataSource bean with an in-memory database.
Both beans exist in the code, but Spring activates one or the other depending on the active profile at runtime.
A common mistake is using Maven profiles to package environment-specific config like database URLs into the JAR. That's wrong because it violates the build once, deploy everywhere principle. You should build one JAR and use Spring profiles or environment variables to inject config when the app starts.
Maven profiles are for what you build. Spring profiles are for how it runs. They're complementary, not interchangeable.
Maven Profiles vs Spring Profiles
Direct Answer
Maven profiles modify the effective build model: dependencies, plugins, properties, and resources used while building. They may change an artifact, but they do not always change compiled code. Spring profiles are runtime application configuration that selects beans and property sources when the application starts. Prefer one deployable artifact with external runtime configuration when possible.
Common Mistakes & Pitfalls
- Using Maven profiles for runtime config: Freshers often define Maven profiles with different database URLs and package separate JARs for dev and prod. This is wrong because it violates the principle of building once, deploying many times. You should build one JAR and use Spring profiles or environment variables to inject runtime config like database URLs when the app starts.
- Hardcoding secrets in Maven profiles: Another mistake is putting production database passwords in Maven profile properties and filtering them into application.properties during the build. This bakes secrets into the JAR, which is a security risk. Secrets should be injected at runtime via environment variables or a vault, not compiled into the artifact.
- Thinking Spring profiles replace Maven profiles: Some freshers assume that because Spring profiles handle environment-specific behavior, they don't need Maven profiles at all. But if you need different dependencies in different environments (like native database drivers or platform-specific libraries), you still need Maven profiles because Spring can't change what's already in the JAR.
Check your understanding
Answer these without looking back at the article.
- How do you define a Spring profile-specific properties file?
- What is @Profile annotation in Spring?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.