Activating Maven Profiles
Fast recall
Quick revision
Core answer
Practical hands-on Maven experience — can the candidate actually use profiles in a build process
Not knowing about automatic activation mechanisms — thinking manual -P flags are the only way to trigger profiles
Mention that CI/CD pipelines typically use environment variable activation rather than -P flags because it's more declarative and traceable in build logs
Interview-ready guide
Interview answer
There are a few ways to activate Maven profiles depending on your needs.
The most direct is using the -P flag, like mvn clean package -Pprod. That explicitly activates the prod profile for that build. But in real projects, you don't want to rely on developers remembering flags.
So you can set activeByDefault to true in the profile definition, which means that profile runs automatically unless you activate a different one.
For example, our dev profile might be activeByDefault so when someone clones the repo and runs mvn package, it just works with local settings. Then there's automatic activation based on conditions. You can activate a profile when an environment variable is set, like BUILD_ENV equals production.
This is huge for CI/CD because your Jenkins or GitLab pipeline just exports BUILD_ENV equals production, and Maven activates the right profile automatically. The build logs show exactly what happened without relying on someone passing the right flag. You can also activate based on JDK version, which is useful if you're supporting multiple Java versions with different compiler settings.
properties exists. One thing to watch out for: if you use -P to explicitly activate a profile, all activeByDefault profiles get deactivated. So if you have a dev profile with activeByDefault true and you run mvn package -Pprod, only the prod profile is active.
The dev one shuts off. That surprises freshers who expect both to run. In production environments, we typically use environment variable activation rather than -P flags because it's clearer in audit logs and less prone to human error.
Activating Maven Profiles
Direct Answer
You activate Maven profiles using the -P flag like mvn package -Pprod, by setting activeByDefault to true in the profile definition, or automatically through activation conditions like environment variables, JDK versions, OS detection, or file presence checks. Once activated, the profile's configuration overrides or extends the base build.
Code Example
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- automatic for local builds -->
</activation>
<properties>
<environment>development</environment>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>env.BUILD_ENV</name> <!-- activated when BUILD_ENV env var exists -->
<value>production</value>
</property>
</activation>
<properties>
<environment>production</environment>
</properties>
</profile>
<profile>
<id>integration-tests</id>
<activation>
<jdk>[17,)</jdk> <!-- only on JDK 17+ -->
<file>
<exists>src/test/resources/integration-test.properties</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals><goal>integration-test</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Common Mistakes & Pitfalls
- Only knowing the -P flag: Many freshers think mvn package -Pprod is the only way to activate profiles, so they end up requiring every developer and CI script to remember the flag. This is fragile — one forgotten -P and you build the wrong artifact. Better to use automatic activation mechanisms so the environment controls the profile, not human memory.
- Forgetting activeByDefault gets overridden: If you set activeByDefault=true on the dev profile but then explicitly activate another profile with -Pprod, the dev profile gets deactivated. Freshers expect both to run, but Maven's rule is: explicit activation disables all activeByDefault profiles.
- Using ! (negation) incorrectly: Maven's activation syntax is picky. Writing <property><name>!env.PROD</name></property> doesn't work the way you'd expect. If you want a profile to activate when an environment variable is missing, you'd use <property><name>env.PROD</name></property> with no value, but negation is tricky and often leads to confusion.
Check your understanding
Answer these without looking back at the article.
- Can multiple profiles be active at the same time?
- How do you see which profiles are defined in a pom.xml?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.