关于java:Maven-Profile配置activeByDefault和activeProfiles的区别

8次阅读

共计 1093 个字符,预计需要花费 3 分钟才能阅读完成。

activeByDefault

<activeByDefault>既能用在 settings.xml 也能用在 pom.xml 里

其作用 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.

也就是说配置了 <activeByDefault> 标签的 profile 只会在没有其余激活的 profile 的状况下被激活

activeProfiles

<activeProfiles>只能用在 settings.xml 文件

作用也看一下 maven 官网文档的说法:

The final piece of the settings.xml puzzle is the activeProfiles element. This contains a set of activeProfile elements, which each have a value of a profile id. Any profile id defined as an activeProfile will be active, regardless of any environment settings. If no matching profile is found nothing will happen. For example, if env-test is an activeProfile, a profile in a pom.xml (or profile.xml with a corresponding id will be active. If no such profile is found then execution will continue as normal.

<activeProfiles> 里配置的 profile 会忽视其激活条件默认激活,如果指定的 profile id 不存在则疏忽

总结

当咱们须要配置一个所有环境都激活的 profile,用 <activeProfiles> 配置,而须要配置一个其余 profile 都没有激活的兜底 profile 时,用 <activeByDefault> 配置
例如:

  • 咱们要在 settings.xml 文件里配置应用阿里云的仓库作为默认仓库,那这个仓库对应的 profile 就能够用 <activeProfiles> 激活
  • 我的项目的 pom.xml 通常会配置多个环境,线上 prod 和线下 dev,线下环境的 profile 就能够用 <activeByDefault> 激活,这样编译就能够不必指定 -P dev 参数而默认就用 dev 环境
正文完
 0