skip to content

How do you explicitly activate and deactivate profiles from the command line?

level: juniorimportance: should knowfreq 60%

answer

  1. -P id to activate
  2. comma-separated list
  3. !id or -id to deactivate
  4. mix activate+deactivate
  5. quote ! in shell

basics

~10 s

Use -P with the profile id to turn it on (mvn package -P prod). Add several with commas. Prefix the id with ! or - to turn a profile off: mvn package -P !slow-tests.

solid answer

~30 s

The `-P` (or `--activate-profiles`) flag controls profiles explicitly. `mvn package -P prod` activates `prod`; you can list several comma-separated: `-P prod,docker`. To force a profile OFF — even one that would auto-activate — prefix its id with `!` or `-`: `mvn verify -P !integration`. Activation and deactivation can be mixed in one argument: `-P prod,!slow-tests`. Remember that simply naming any profile with `-P` also disables every `activeByDefault` profile, so explicit activation is the cleanest way to take full control of which profiles run. In a shell, quote or escape `!` (e.g. `'-P!integration'`) because some shells treat `!` as history expansion.

code

bash · 3 lines
bash
mvn package -P prod,docker        # activate two
mvn verify -P '!integration'       # deactivate one
mvn package -P prod,'!slow-tests'  # mix both

go deeper

for a junior

Know -P id activates and !id deactivates a profile.

for a middle

Know you can list and mix activations/deactivations and must escape ! in the shell.

for a senior

Use explicit activation/deactivation to make CI runs deterministic regardless of environment-based activation.

for a principal

Standardize CI invocation flags across the org so builds aren't at the mercy of machine-local auto-activation.

## Activating profiles explicitly The command-line flag is `-P` (long form `--activate-profiles`). It takes a comma-separated list of profile ids: ```bash mvn package -P prod mvn package -P prod,docker,skip-javadoc ``` Each named profile is forced active regardless of its `<activation>` conditions. ## Deactivating profiles Prefix the id with `!` or `-` to force a profile **off**, even if its activation would normally turn it on: ```bash mvn verify -P !integration mvn verify -P -integration # equivalent ``` This is how you opt out of an auto-activated profile (e.g. one keyed on a property or OS) for a single run. ## Mixing activate and deactivate Both forms can appear together: ```bash mvn package -P prod,!slow-tests ``` This activates `prod` and suppresses `slow-tests`. ## Shell escaping `!` triggers history expansion in bash/zsh. Quote it: ```bash mvn verify -P '!integration' mvn verify "-P!integration" ``` ## Side effect on defaults Using `-P` to name **any** profile turns off all `activeByDefault` profiles. If you still want a default-active profile, name it explicitly too. ## Verifying ```bash mvn help:active-profiles ``` Confirms the resulting active set after your flags are applied.

  • How do you turn off a profile that activates itself via <activation>?
    Pass `-P !profileId` (or `-P -profileId`); the leading ! / - forces it off regardless of its activation conditions.
  • Why might `-P !integration` fail in bash with an event-not-found error?
    The shell interprets `!` as history expansion. Quote it: `-P '!integration'` or `"-P!integration"`.

saying these in an interview costs you the question

  • Thinking there is no way to disable an auto-activated profile (use !/-).
  • Forgetting that any -P usage disables activeByDefault profiles.

context