Maven中的继承配置文件

Maven中的继承配置文件,maven,maven-profiles,Maven,Maven Profiles,我在我的父母pom中有以下配置文件 <profile> <id>P1</id> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>P2</id> <activation&

我在我的父母pom中有以下配置文件

<profile>
    <id>P1</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

<profile>
    <id>P2</id>
    <activation>
        <file>
            <exists>${project.basedir}/src/main/whatever</exists>
        </file>
    </activation>
</profile>

P1
真的
P2
${project.basedir}/src/main/where
为什么P1在儿童POM中活跃而P2不活跃


目录
${project.basedir}/src/main/which
,不存在于父项目中,但存在于子项目中。

一般来说,Maven配置文件不会继承(有关可能有用的讨论和博客文章链接,请参阅)。我成功地做到了以下几点:

<!-- Parent -->
<profile>
    <id>P2</id>
    <activation>
        <file>
            <exists>${project.basedir}/src/main/whatever</exists>
        </file>
    </activation>
    <!-- all the things you want to define for the child POMs -->
</profile>

<!-- Child -->
<!-- Include only the activation block, which must match parent's exactly -->
<!-- Whatever is in the parent will be inherited -->
<profile>
    <id>P2</id>
    <activation>
        <file>
            <exists>${project.basedir}/src/main/whatever</exists>
        </file>
    </activation>
</profile>

P2
${project.basedir}/src/main/where
P2
${project.basedir}/src/main/where
此外,我认为,如果P2处于活动状态,P1将不会处于活动状态。这是因为P1的
为真。在我看来,元素名有点误导。“默认情况下处于活动状态”意味着“始终处于活动状态”,而实际上它意味着“仅当此POM中没有其他配置文件处于活动状态时才处于活动状态”


使用Maven 3.0.x发现的上述问题。

Profile
P2
未激活,因为其
标记下的路径未解析为现有路径,即使目录
${project.basedir}/src/main/which
存在。如果将属性
${project.basedir}
重写为
${basedir}
,它应该激活
P2
配置文件

这意味着,
${project.basedir}
不会解析为项目基目录。但是,
help:effectivepom
显示它确实如此。我已经报告了这个()

此外,我认为,如果P2处于活动状态,P1将不会处于活动状态

这是正确的。引述:

除非使用前面描述的方法之一激活同一POM中的另一个配置文件,否则该配置文件(本例中的P1)将自动对所有构建激活。在命令行上或通过其激活配置激活POM中的配置文件时,默认情况下处于活动状态的所有配置文件将自动停用

“继承”这个词让我感到困惑,因为“概要文件继承”在中有效,但在中无效

为了说明问题,我模拟了这种情况。空pom意味着除了标准模型、组、工件和版本标记之外,它是空的

简单场景 目录结构:

simple
 \-pom.xml
inheritance
 |--child
 |  \-pom.xml         // child pom
 \-pom.xml           // parent pom
aggregation
 |--module
 |  \-pom.xml         // module pom
 \-pom.xml           // aggregator 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
directory
mvn帮助:所有配置文件
输出:

Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
项目继承 目录结构:

simple
 \-pom.xml
inheritance
 |--child
 |  \-pom.xml         // child pom
 \-pom.xml           // parent pom
aggregation
 |--module
 |  \-pom.xml         // module pom
 \-pom.xml           // aggregator pom
子pom为空,而父pom具有简单场景中的配置文件。无论是否存在
继承/child/dir
目录运行
mvn帮助:来自
child
目录的所有配置文件输出:

Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
当从
child
目录运行
mvn help:effective pom
时,它表明配置文件确实没有被继承。其行为如下:

POM中合并的元素如下:

  • 依赖关系
  • 开发人员和贡献者
  • 插件列表(包括报告)
  • 具有匹配ID的插件执行
  • 插件配置
  • 资源
这里没有提到个人资料

项目聚合 目录结构:

simple
 \-pom.xml
inheritance
 |--child
 |  \-pom.xml         // child pom
 \-pom.xml           // parent pom
aggregation
 |--module
 |  \-pom.xml         // module pom
 \-pom.xml           // aggregator pom
模块pom为空,而聚合器pom具有简单场景中的概要文件。如果没有运行
aggregation/module/dir
目录
mvn帮助:来自
module
目录输出的所有配置文件

Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
如果有
aggregation/module/dir
目录运行
mvn帮助:所有配置文件
来自
module
目录输出:

Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
module
目录运行
mvn-help:effective-pom
时,它显示配置文件是继承的。这不是:

项目继承

如果您有几个Maven项目,并且它们都有类似的配置,那么您可以通过提取这些类似的配置并创建父项目来重构您的项目。因此,您所要做的就是让您的Maven项目继承父项目,然后这些配置将应用于所有这些项目

注:

  • 这不适用于配置文件,如图所示
  • 从继承目录运行maven构建将只运行父构建
项目汇总

如果您有一组一起构建或处理的项目,则可以创建一个父项目,并让该父项目将这些项目声明为其模块。通过这样做,您只需构建父级,其余的都将随之生成

注:

  • aggregation
    目录运行maven构建将运行每个模块和聚合器的构建(实际顺序由maven根据不同的标准确定)
结论 配置文件可以是全局配置文件、每个用户配置文件或每个项目配置文件。由于聚合项目是一起构建的(在同一个构建中),因此必须运行某种配置文件解析来计算活动项目。这是令人困惑的部分:

  • 当项目被继承时,概要文件不会从父pom继承到子pom
  • 聚合项目时,概要文件从聚合器pom继承到模块pom

使用Maven 3.1.0对此进行了测试。和3.0.5.

为了澄清这一点,Maven配置文件实际上是继承的。有关其他SO问题的参考,请参阅:。我已经成功地在我的项目中继承了配置文件,不需要额外的工作

对于原始问题,您在exists元素中定义了一个变量。根据报告:

从Maven 2.0.9开始,标签和 插入。支持的变量是系统属性,如 ${user.home}和环境变量,如${env.home}。请不要