93

I have a profile activated by default in my maven setting file ~/.m2/settings.xml.

Is it possible to deactivate it from the command line by doing something like this:

mvn -P!profileActivatedByDefault
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
Calfater
  • 1,255
  • 1
  • 10
  • 19

3 Answers3

153

Yes indeed, you have the right way. From maven profiles user guide

Deactivating a profile

Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:

mvn groupId:artifactId:goal -P !profile-1,!profile-2

This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.

As noted by @Calfater in the comments, the exclamation mark needs to be escaped in most shells (bash, zsh, and others on Linux and MacOS), though not on the windows command line.

The escape mechanisms are shell-dependant, but usually you can do :

mvn groupId:artifactId:goal -P \!profile-1

Or

mvn groupId:artifactId:goal -P '!profile-1'

Or, as Shaun Morris suggested below, use - instead of !, but without whitespace between -P and the profiles:

mvn groupId:artifactId:goal -P-profile-1,-profile2
ryenus
  • 15,711
  • 5
  • 56
  • 63
GPI
  • 9,088
  • 2
  • 31
  • 38
  • 31
    This solution works but we need to escape the '!' character on Linux terminal: `mvn groupId:artifactId:goal -P \!profile-name` – Calfater May 11 '15 at 11:56
  • @Calfater : Indeed, this is worthy of editing the original post. – GPI May 11 '15 at 13:53
  • The escape character was killing me...in windows was working flawlessly Thanks. – cabaji99 Oct 26 '16 at 16:22
  • 1
    @Calfater also had problem with exclamation mark using `zsh`. I just put `-P` between single quotes like this: `-P '!dev'` – Kirill Mar 07 '18 at 10:10
  • @Derp : single quotes disable zsh / bash / ... interpolation. Thanks for the trick. – Calfater Mar 07 '18 at 13:47
37

On a Mac, I got the following error attempting to use '!'

mvn groupId:artifactId:goal -P!profile-1
-bash: !profile: event not found

Doing the following works with the '-':

mvn groupId:artifactId:goal -P-profile1

Alternatively you can do:

mvn groupId:artifactId:goal -P\!profile1
Shaun Morris
  • 381
  • 3
  • 6
6

Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:

mvn groupId:artifactId:goal -P !profile-1,!profile-2

This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config. Refer Maven Doc

Because ! Exclamation mark is a special character for most of the command line tools, you might need to escape it refer here.

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • 6
    You're right that *activating* a profile through `-P` will ignore other profiles (e.g. `activeByDefault` or ), but using only/exclusively the deactivation syntax (`!` prefix) will only deactivate those profiles, and not override the `activeByDefault` or `` declarations – GPI Aug 08 '14 at 11:21
  • 1
    The docs changed, now `-P` will add the profiles in addition to activeByDefault profiles: "This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the section in settings.xml" – Jacob van Lingen Jul 29 '19 at 09:02
  • Thanks for the up-to-date information. I do not know for how long this has changed. @SparkOn : this changes the validity-scope of your answer, you might want to edit to to reflect this. – GPI Jul 31 '19 at 10:10