I have a profile in my pom.xml which should be always active unless it is explicitely deactivated (-P !firstProfile). I solved this by using the activeByDefault flag:
我有个人资料。除非是显式停用(-P !firstProfile),否则应该一直处于活动状态的xml。我通过使用activeByDefault标志解决了这个问题:
<profiles>
<profile>
<id>firstProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
</profiles>
Now in the same pom.xml I have a second profile defined this should only be active if the profile is really activated (-P secondProfile). So the default behaviour is: firstProfile active, secondProfile inactive. At some other point I would like to activated the second profile in addition to the first profile. Now the problem is that if I do that with "-P secondProfile" the firstProfile unfortunately gets deactivated. The Maven documentation states this:
现在还是一样。我定义了第二个概要文件,只有当概要文件真正被激活时它才应该是活动的(-P secondProfile)。默认行为是:firstProfile active, secondProfile inactive。在另一个点上,除了第一个概要文件之外,我还想激活第二个概要文件。现在的问题是,如果我用"-P secondProfile"做这个,firstProfile很不幸地会被禁用。Maven文档说明如下:
... This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config. ...
…除非使用前面描述的方法激活同一POM中的另一个概要文件,否则该概要文件将自动为所有构建激活。当POM中的概要文件在命令行或通过其激活配置文件被激活时,默认激活的所有概要文件都将被自动停用。
Is there somehow a possibility how to keep the firstProfile always active (without having to declare it in the settings.xml)?
是否有可能使firstProfile始终保持活动状态(而不需要在setter .xml中声明它)?
5 个解决方案
#1
121
One trick is to avoid activeByDefault
, and instead activate the profile by the absence of a property, eg:
一个技巧是避免默认的activeByDefault,而是通过没有属性来激活配置文件,例如:
<profiles>
<profile>
<id>firstProfile</id>
<activation>
<property>
<name>!skipFirstProfile</name>
</property>
</activation>
...
</profile>
</profiles>
You should then be able to deactivate the profile with -DskipFirstProfile
or with -P !firstProfile
, but otherwise the profile will be active.
然后,您应该能够使用-DskipFirstProfile或用-P !firstProfile来禁用配置文件,但否则该配置文件将是活动的。
See: Maven: The Complete Reference, Profile Activation - Activation by the Absence of a Property
参见:Maven:完整的引用、配置文件激活——由于没有属性而激活
#2
18
I wish there was such a possibility, I have often missed it. The only relevant JIRA issue I could find is this one:
我希望有这样的可能性,我经常错过它。我能找到的唯一与JIRA相关的问题是:
MNG-4917: Profile not active even though it has activeByDefault set to true
MNG-4917:配置文件不活动,即使它的activeByDefault设置为true
And it's been resolved as Not A Problem
.
这个问题已经解决了。
I've stopped using activeByDefault
, because this "all or nothing" approach made it worthless for me.
我已经不再默认使用activeByDefault了,因为这种“全有或全无”的方法对我来说毫无价值。
The only way to change this behavior is to write your own replacement for DefaultProfileSelector
, register it as a plexus component with @Component( role = ProfileSelector.class )
and put it in ${MAVEN_HOME}/lib/ext
(that way it will be picked as default profile selector). (If you are using Maven 3.0.2 or older you will also have to edit ${MAVEN_HOME}/bin/m2.conf
to load lib/ext
before it loads lib
)
改变这种行为的唯一方法是为DefaultProfileSelector编写自己的替换,将其注册为具有@Component(role = ProfileSelector)的plexus组件。类)并将其放入${MAVEN_HOME}/lib/ext(这样将选择它作为默认配置文件选择器)。(如果您正在使用Maven 3.0.2或更老的版本,您还必须编辑${MAVEN_HOME}/bin/m2。conf在载入lib/ext之前加载lib)
#3
3
You can simply list all the profiles you want activated on the command line as such:
您可以简单地列出您希望在命令行中激活的所有配置文件,如下所示:
-P profile-1,profile-2
- p profile-1,profile-2
maven was designed to allow multiple profile activation automatically, if you however override that with the -P then only the profiles listed in the parameter are activated.
maven被设计为允许自动激活多个概要文件,如果您使用-P覆盖它,那么只有参数中列出的概要文件才会被激活。
#4
1
This question is ancient, but it appears the problem is solvable by using activeProfile
rather than activeByDefault
. I'm on Maven 3.3.9, but the solution may work on earlier versions.
这个问题很古老,但问题是通过使用activeProfile而不是activeByDefault来解决问题。我在Maven 3.3.9上,但是解决方案可能会在早期版本中工作。
Simply list out your activeProfiles
in your settings.xml
, like so:
只需在设置中列出activeProfiles。xml,如下所示:
<settings>
<profiles>
[...]
</profiles>
<activeProfiles>
<activeProfile>my-awesome-profile</activeProfile>
</activeProfiles>
</settings>
In my-awesome-profile
I have settings like database URLs and so on, so they always apply. Here, I activate a second profile, resolve-from-central
:
在my-awesome-profile中,我有诸如数据库url之类的设置,所以它们总是适用的。在这里,我激活了第二个配置文件,从-central解析:
$ mvn help:all-profiles -P resolve-from-central
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:all-profiles (default-cli) @ standalone-pom ---
[INFO] Listing Profiles for Project: org.apache.maven:standalone-pom:pom:1
Profile Id: resolve-from-central (Active: true , Source: settings.xml)
Profile Id: my-awesome-profile (Active: true , Source: settings.xml)
Profile Id: resolve-from-internal (Active: false , Source: settings.xml)
Notice how my-awesome-profile
is still active. Yay!
注意my-awesome-profile是如何保持活动的。耶!
#5
0
You can't keep the default profile active, but you can take the contents of that profile (the ... in your example) and just move it to the main section of the pom.
您不能保持默认配置文件处于活动状态,但是您可以获取该配置文件的内容(在您的示例中)并将其移动到pom的主要部分。
Just because you are using profiles, it does not mean everything you are doing needs to be within a profile.
仅仅因为您正在使用概要文件,并不意味着您所做的一切都需要在概要文件中。
#1
121
One trick is to avoid activeByDefault
, and instead activate the profile by the absence of a property, eg:
一个技巧是避免默认的activeByDefault,而是通过没有属性来激活配置文件,例如:
<profiles>
<profile>
<id>firstProfile</id>
<activation>
<property>
<name>!skipFirstProfile</name>
</property>
</activation>
...
</profile>
</profiles>
You should then be able to deactivate the profile with -DskipFirstProfile
or with -P !firstProfile
, but otherwise the profile will be active.
然后,您应该能够使用-DskipFirstProfile或用-P !firstProfile来禁用配置文件,但否则该配置文件将是活动的。
See: Maven: The Complete Reference, Profile Activation - Activation by the Absence of a Property
参见:Maven:完整的引用、配置文件激活——由于没有属性而激活
#2
18
I wish there was such a possibility, I have often missed it. The only relevant JIRA issue I could find is this one:
我希望有这样的可能性,我经常错过它。我能找到的唯一与JIRA相关的问题是:
MNG-4917: Profile not active even though it has activeByDefault set to true
MNG-4917:配置文件不活动,即使它的activeByDefault设置为true
And it's been resolved as Not A Problem
.
这个问题已经解决了。
I've stopped using activeByDefault
, because this "all or nothing" approach made it worthless for me.
我已经不再默认使用activeByDefault了,因为这种“全有或全无”的方法对我来说毫无价值。
The only way to change this behavior is to write your own replacement for DefaultProfileSelector
, register it as a plexus component with @Component( role = ProfileSelector.class )
and put it in ${MAVEN_HOME}/lib/ext
(that way it will be picked as default profile selector). (If you are using Maven 3.0.2 or older you will also have to edit ${MAVEN_HOME}/bin/m2.conf
to load lib/ext
before it loads lib
)
改变这种行为的唯一方法是为DefaultProfileSelector编写自己的替换,将其注册为具有@Component(role = ProfileSelector)的plexus组件。类)并将其放入${MAVEN_HOME}/lib/ext(这样将选择它作为默认配置文件选择器)。(如果您正在使用Maven 3.0.2或更老的版本,您还必须编辑${MAVEN_HOME}/bin/m2。conf在载入lib/ext之前加载lib)
#3
3
You can simply list all the profiles you want activated on the command line as such:
您可以简单地列出您希望在命令行中激活的所有配置文件,如下所示:
-P profile-1,profile-2
- p profile-1,profile-2
maven was designed to allow multiple profile activation automatically, if you however override that with the -P then only the profiles listed in the parameter are activated.
maven被设计为允许自动激活多个概要文件,如果您使用-P覆盖它,那么只有参数中列出的概要文件才会被激活。
#4
1
This question is ancient, but it appears the problem is solvable by using activeProfile
rather than activeByDefault
. I'm on Maven 3.3.9, but the solution may work on earlier versions.
这个问题很古老,但问题是通过使用activeProfile而不是activeByDefault来解决问题。我在Maven 3.3.9上,但是解决方案可能会在早期版本中工作。
Simply list out your activeProfiles
in your settings.xml
, like so:
只需在设置中列出activeProfiles。xml,如下所示:
<settings>
<profiles>
[...]
</profiles>
<activeProfiles>
<activeProfile>my-awesome-profile</activeProfile>
</activeProfiles>
</settings>
In my-awesome-profile
I have settings like database URLs and so on, so they always apply. Here, I activate a second profile, resolve-from-central
:
在my-awesome-profile中,我有诸如数据库url之类的设置,所以它们总是适用的。在这里,我激活了第二个配置文件,从-central解析:
$ mvn help:all-profiles -P resolve-from-central
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:all-profiles (default-cli) @ standalone-pom ---
[INFO] Listing Profiles for Project: org.apache.maven:standalone-pom:pom:1
Profile Id: resolve-from-central (Active: true , Source: settings.xml)
Profile Id: my-awesome-profile (Active: true , Source: settings.xml)
Profile Id: resolve-from-internal (Active: false , Source: settings.xml)
Notice how my-awesome-profile
is still active. Yay!
注意my-awesome-profile是如何保持活动的。耶!
#5
0
You can't keep the default profile active, but you can take the contents of that profile (the ... in your example) and just move it to the main section of the pom.
您不能保持默认配置文件处于活动状态,但是您可以获取该配置文件的内容(在您的示例中)并将其移动到pom的主要部分。
Just because you are using profiles, it does not mean everything you are doing needs to be within a profile.
仅仅因为您正在使用概要文件,并不意味着您所做的一切都需要在概要文件中。