配置文件P2
未激活,因为exists
即使目录${project.basedir}/src/main/whatever
存在,其标记下的路径也不会解析为现有路径。如果将属性重写${project.basedir}
为${basedir}
,它应该激活P2
配置文件。
这应该意味着它${project.basedir}
不会像它应该那样解析到项目基目录。不过,这help:effective-pom
表明确实如此。我已经报告了这个(MNG-5516)。
另外我认为如果 P2 激活,P1 将不会激活。
那是对的。引用文档activeByDefault
:
此配置文件(在此示例中为 P1)将自动对所有构建处于活动状态,除非使用上述方法之一激活同一 POM 中的另一个配置文件。当在命令行或通过其激活配置激活 POM 中的配置文件时,默认情况下处于活动状态的所有配置文件都会自动停用。
继承这个词让我感到困惑,因为“配置文件继承”适用于项目聚合,但不适用于项目继承。
为了清楚起见,我模拟了这种情况。Empty pom 表示除了标准的模型、组、工件和版本标签之外它是空的。
简单场景
目录结构:
simple
\-pom.xml
内容:
<profiles>
<profile>
<id>P1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>P2</id>
<activation>
<file>
<exists>${basedir}/dir/</exists>
</file>
</activation>
</profile>
</profiles>
如果没有dir
目录mvn help:all-profiles
输出:
Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
如果有dir
目录mvn help:all-profiles
输出:
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
项目继承
目录结构:
inheritance
|--child
| \-pom.xml // child pom
\-pom.xml // parent pom
子 pom 为空,而父 pom 具有简单场景中的配置文件。无论从目录输出inheritance/child/dir
运行的目录是否存在:mvn help:all-profiles
child
Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
mvn help:effective-pom
从目录运行时,child
它显示配置文件确实没有被继承。它的行为与记录的一样:
POM 中合并的元素如下:
- 依赖关系
- 开发者和贡献者
- 插件列表(包括报告)
- 具有匹配 id 的插件执行
- 插件配置
- 资源
这里没有提到配置文件。
项目聚合
目录结构:
aggregation
|--module
| \-pom.xml // module pom
\-pom.xml // aggregator pom
模块 pom 为空,而聚合器 pom 具有简单场景中的配置文件。如果没有从目录输出aggregation/module/dir
运行的目录:mvn help:all-profiles
module
Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
如果有从目录输出aggregation/module/dir
运行的目录:mvn help:all-profiles
module
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
mvn help:effective-pom
从目录运行时,module
它显示配置文件是继承的。这没有明确记录:
项目继承
如果您有多个 Maven 项目,并且它们都有相似的配置,您可以通过提取这些相似的配置并创建一个父项目来重构您的项目。因此,您所要做的就是让您的 Maven 项目继承该父项目,然后这些配置将应用于所有项目。
笔记:
- 这不适用于配置文件,因为它已经显示。
- 从目录运行 maven 构建
inheritance
将只运行父构建。
项目聚合
如果您有一组一起构建或处理的项目,您可以创建一个父项目并让该父项目将这些项目声明为其模块。通过这样做,您只需构建父级,其余的将随之而来。
笔记:
- 从目录运行 maven 构建
aggregation
将运行每个模块和聚合器的构建(实际顺序由 maven 基于不同标准确定)。
结论
配置文件可以全局定义,每个用户或每个项目。由于聚合项目是一起构建的(在同一个构建中),因此必须运行某种配置文件解析来计算活动项目。所以这是令人困惑的部分:
- 当项目被继承时,配置文件不会 从父 pom 继承到子 pom。
- 当项目被聚合时,配置文件从聚合器 pom 继承到模块 pom。
使用 Maven 3.1.0 对此进行了测试。和 3.0.5。