How do you activate Maven profiles from the command line with `-P`, and how does that relate to other activation mechanisms?
answer
- -P id1,id2 comma list
- ! or - to deactivate
- activation: jdk/os/property/file/default
- activeByDefault off when another POM profile active
- help:active-profiles to verify
basics
~10 s-P profile1,profile2 activates named profiles for that build. Prefix with ! (e.g. -P !prod) to deactivate one. Profiles can also auto-activate via <activation> rules or settings.xml.
solid answer
~40 s`-P` takes a comma-separated list of profile IDs to activate explicitly for the invocation: `mvn package -P prod,fast`. You can deactivate with `!` or `-` prefix: `-P !integration`. Profiles are blocks in the POM (or settings.xml) that override configuration — properties, dependencies, plugins, repositories — for specific situations like environments. Beyond `-P`, profiles activate via `<activation>` conditions (JDK version, OS, a property being set/`-Dflag`, presence/absence of a file, or `<activeByDefault>`), and via `<activeProfiles>` in settings.xml. A key gotcha: defining any profile via `-P` or property activation does NOT disable `activeByDefault` profiles unless they're in the same POM — but a profile activated explicitly in a POM deactivates that POM's activeByDefault profiles. Use `mvn help:active-profiles` to see exactly what's active.
code
bash · 12 lines# Activate two profiles
mvn clean deploy -P release,sign
# Deactivate a profile that is otherwise on
mvn package -P '!integration'
# Property activation: -Denv=prod triggers a profile whose
# <activation><property><name>env</name><value>prod</value></property>
mvn package -Denv=prod
# Inspect what's actually active
mvn help:active-profilesgo deeper
Know -P name switches on a named profile.
List the activation mechanisms and deactivation syntax.
Reason about activeByDefault interactions, property activation, and verifying effective config.
Design environment/profile strategy and avoid hidden machine-specific settings.xml activation in shared builds.
## What a profile is A **profile** is a named set of build customizations that you switch on or off. It lets one POM behave differently per environment or situation — e.g. different `<properties>`, `<dependencies>`, `<plugins>`, `<repositories>`, or `<build>` config for `dev` vs `prod`. ```xml <profiles> <profile> <id>prod</id> <properties><db.url>jdbc:postgresql://prod/...</db.url></properties> </profile> </profiles> ``` ## Activating with `-P` The `-P` (or `--activate-profiles`) flag takes a **comma-separated** list of profile IDs: ```bash mvn clean package -P prod,fast ``` To **deactivate** a profile that would otherwise be on, prefix with `!` or `-`: ```bash mvn package -P !integration # turn integration OFF ``` (In shells, `!` may need quoting/escaping.) ## Other activation mechanisms 1. **`<activation>` block** inside the profile — auto-activates when a condition holds: - `<jdk>17</jdk>` (JDK range) - `<os>` (family/name/arch) - `<property><name>env</name><value>prod</value></property>` (a `-Denv=prod` triggers it) - `<file><exists>` / `<missing>` - `<activeByDefault>true</activeByDefault>` (on unless something else is active) 2. **settings.xml `<activeProfiles>`** — always-on profiles for that machine/user. ## Gotchas - `<activeByDefault>` is **disabled within a POM** as soon as *any* profile in **that same POM** is activated by another means (including `-P`). It is not affected by profiles from settings.xml. - Property-based activation `<property><name>!flag</name></property>` (negation) means 'active when flag is NOT set'. - Order/precedence and merging of profile config can surprise people; always verify. ## Verifying ```bash mvn help:active-profiles mvn help:effective-pom -P prod # see the merged result ``` ## When to use which - `-P` for explicit, intentional per-run switching (CI stages, release). - `<activation>` for automatic context (JDK/OS). - settings.xml for machine-local concerns (corporate repo profile).
- What turns off an `activeByDefault` profile?Activating any other profile in the same POM (via -P, property, file, etc.) disables that POM's activeByDefault profiles. Profiles in settings.xml don't affect it.
- How can a `-D` property activate a profile without `-P`?Via `<activation><property><name>X</name><value>Y</value></property></activation>`; passing `-DX=Y` activates it automatically.
- How do you confirm which profiles are active in a complex build?Run `mvn help:active-profiles` (and `help:effective-pom`) to see the resolved, merged configuration.
saying these in an interview costs you the question
- Using spaces instead of commas in `-P` lists.
- Assuming any `-P` use disables all default profiles globally — only within the same POM.
- Forgetting profiles can also live in settings.xml, not just the POM.