档案P2
未激活,因为其exists
标签下的路径无法解析为现有路径,尽管目录${project.basedir}/src/main/whatever
存在。如果将属性${project.basedir}
改写为${basedir}
,它应该激活P2
档案。
这意味着${project.basedir}
并没有像它应该的那样解析为项目基本目录。然而,help:effective-pom
显示它确实如此。我已经报告了这个问题(MNG-5516)。
此外,我认为如果P2被激活,P1将不会被激活。
没错。引用activeByDefault
文档的说法:
这个配置文件(在这个例子中是P1)将自动对所有构建生效,除非通过之前描述的方法之一激活了同一个POM中的另一个配置文件。所有默认激活的配置文件在通过命令行或激活配置时,会自动取消激活。
“继承”这个词让我感到困惑,因为“配置文件继承”在项目聚合中起作用,但在项目继承中却不起作用。
为了澄清这个问题,我模拟了这种情况。 “空的pom”表示除了标准模型、组、构件和版本标签之外,它是空的。
###简单场景
目录结构:
simple
\-pom.xml
pom内容:
<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
目录,从
child
目录运行
mvn help:all-profiles
命令都会输出以下内容:
Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
当在子目录中运行`mvn help:effective-pom`时,它显示配置文件确实没有被继承。它的行为如下所述:
合并的POM中包含以下元素:
- 依赖项
- 开发人员和贡献者
- 插件列表(包括报告)
- 具有匹配ID的插件执行
- 插件配置
- 资源
这里没有提到任何配置文件。
### 项目聚合
目录结构:
aggregation
|--module
| \-pom.xml // module pom
\-pom.xml // aggregator pom
模块pom为空,而聚合pom中的配置与简单场景中的配置相同。如果没有
aggregation/module/dir
目录,则从
module
目录运行
mvn help:all-profiles
命令会输出:
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)
当在
module
目录下运行
mvn help:effective-pom
时,它显示配置文件是继承的。这并没有
明确记录:
项目继承
如果你有多个Maven项目,并且它们都有相似的配置,你可以通过提取出这些相似的配置并创建一个父项目来重构你的项目。这样,你只需要让你的Maven项目继承那个父项目,这些配置就会应用到所有项目中。
注意:
- 这不适用于配置文件,如已经显示的那样。
- 从
inheritance
目录运行Maven构建将只运行父项目构建。
项目聚合
如果你有一组需要一起构建或处理的项目,你可以创建一个父项目,并将这些项目声明为其模块。这样一来,你只需要构建父项目,其他项目也会跟随一起构建。
注意:
- 从
聚合
目录运行maven构建将运行每个模块和聚合器的构建(实际顺序由maven根据不同的标准确定)。
###结论
配置文件可以在全局、用户或项目级别上进行定义。由于聚合项目是一起构建的(在同一个构建中),必须运行某种配置文件解析来计算活动配置文件。因此,这是令人困惑的部分:
- 当项目继承配置文件时,不会从父pom继承到子pom。
- 当项目聚合时,配置文件会从聚合器pom继承到模块pom。
这是使用Maven 3.1.0和3.0.5进行测试的结果。
P2
的激活条件中引用的目录是否存在?提问时请提供完整信息。 - Björn Pollex