Maven Build Profiles
Fast recall
Quick revision
Core answer
Whether the candidate understands Maven's environment management features and has worked with multi-environment projects
Confusing Maven profiles with Spring profiles — they operate at different lifecycle stages and solve different problems
Clarify that Maven profiles handle build-time concerns while Spring profiles handle runtime config — using the right tool for the right job shows maturity
Interview-ready guide
Interview answer
Maven profiles are named build configurations that let me customize what gets built depending on the target environment.
So if I'm packaging for production, I'd run mvn package -Pprod, and Maven would include the PostgreSQL driver, skip development-only dependencies, and filter my properties file to inject production database URLs. The key thing to understand is these are build-time tools, not runtime tools. They determine what goes into the JAR during the Maven package phase.
Here's a real example from my projects: I had a dev profile that used an in-memory H2 database and skipped integration tests to keep local builds fast, quickly. Then I had a ci profile that our Jenkins pipeline activated, which ran the full test suite with a real PostgreSQL container and generated code coverage reports with JaCoCo. One mistake I've seen is people confusing Maven profiles with Spring profiles.
Maven profiles control the build — what dependencies you bundle, which plugins run. Spring profiles control runtime behavior — which beans get loaded when the app starts. You use both, but they solve different problems.
For interviews, I'd say Maven profiles shine when you need different artifacts for different environments but want to keep one codebase. The alternative would be maintaining separate pom files or manually editing configs before every build, which is error-prone and slow.
Maven Build Profiles
Direct Answer
Maven profiles are named build configurations that let you customize the build for different environments like dev, staging, or prod. They're activated on demand to include different dependencies, properties, or plugin configurations. Think of them as build-time switches that change what gets compiled into your JAR or WAR.
Code Example
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<db.url>jdbc:h2:mem:devdb</db.url>
<skip.integration.tests>true</skip.integration.tests>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<db.url>jdbc:postgresql://prod-db.example.com:5432/orders</db.url>
<skip.integration.tests>false</skip.integration.tests>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Common Mistakes & Pitfalls
- Confusing Maven and Spring profiles: Freshers often think Maven profiles and Spring profiles are interchangeable because both have the word profile. Maven profiles are for build-time decisions (what dependencies to include, which plugins to run), while Spring profiles are for runtime decisions (which beans to activate). Using Maven profiles to manage runtime config like database credentials is wrong — that's Spring's job.
- Hardcoding secrets in profile properties: Another mistake is defining <db.password>prod123</db.password> directly in the pom.xml and committing it to Git. Secrets should never be in version control. Use Maven properties as placeholders and inject actual values via environment variables or a secrets manager at deployment time.
- Over-engineering with too many profiles: Creating profiles for every tiny variation leads to a maintenance nightmare. Keep it simple — typically dev, staging, prod is enough.
Check your understanding
Answer these without looking back at the article.
- What is the difference between Maven profiles and Spring profiles?
- How do you activate a Maven profile automatically?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.