Maven中的继承配置文件

Adr*_*Ber 9 maven maven-profiles

我的父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>
Run Code Online (Sandbox Code Playgroud)

为什么P1在儿童POM中是活跃的而P2不是?

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

lin*_*ski 19

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

这应该意味着它${project.basedir}不会像它应该解析到项目基目录.在help:effective-pom表明,确实如此,虽然.我已经报道了这个(MNG-5516).

另外我认为如果P2是P1,P1将不会被激活.

那是正确的.引用文档activeByDefault:

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

继承这个词让我很困惑,因为"配置文件继承"在项目聚合中起作用,但在项目继承中不起作用.

为了清楚起见,我模拟了这种情况. 除了标准模型,组,工件和版本标签之外,空pom表示它是空的.

简单的场景

目录结构:

simple
 \-pom.xml
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

如果没有dir目录mvn help:all-profiles输出:

Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Run Code Online (Sandbox Code Playgroud)

如果有dir目录mvn help:all-profiles输出:

Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
Run Code Online (Sandbox Code Playgroud)

项目继承

目录结构:

inheritance
 |--child
 |  \-pom.xml         // child pom
 \-pom.xml           // parent pom
Run Code Online (Sandbox Code Playgroud)

子pom是空的,而父pom具有简单场景中的配置文件.无论从目录输出inheritance/child/dir运行mvn help:all-profileschild目录是否存在:

Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Run Code Online (Sandbox Code Playgroud)

mvn help:effective-pomchild目录运行时,它显示配置文件确实没有继承.它表现为记录:

POM中合并的元素如下:

  • 依赖
  • 开发人员和贡献者
  • 插件列表(包括报告)
  • 具有匹配ID的插件执行
  • 插件配置
  • 资源

这里没有提到配置文件.

项目聚合

目录结构:

aggregation
 |--module
 |  \-pom.xml         // module pom
 \-pom.xml           // aggregator pom
Run Code Online (Sandbox Code Playgroud)

模块pom为空,而聚合器pom具有简单方案中的配置文件.如果没有aggregation/module/dir运行的目录mvn help:all-profiles,从module目录中的输出:

Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)
Run Code Online (Sandbox Code Playgroud)

如果aggregation/module/dir目录mvn help:all-profilesmodule目录输出运行:

Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)
Run Code Online (Sandbox Code Playgroud)

mvn help:effective-pommodule目录运行时,它显示配置文件是继承的.这没有明确记录:

项目继承

如果你有几个Maven项目,并且它们都有类似的配置,你可以通过拉出那些类似的配置并制作一个父项目来重构你的项目.因此,您所要做的就是让您的Maven项目继承该父项目,然后将这些配置应用于所有这些项目.

笔记:

  • 这不适用于配置文件,如图所示.
  • inheritance目录运行maven构建将仅运行父构建.

项目聚合

如果您有一组一起构建或处理的项目,则可以创建父项目并让该父项目将这些项目声明为其模块.通过这样做,你只需要建立父母,其余的将遵循.

笔记:

  • aggregation目录运行maven构建将运行每个模块和聚合器的构建(实际顺序由maven根据不同的标准确定).

结论

可以按用户或每个项目全局定义配置文件.由于聚合项目是一起构建的(在同一构建中),因此必须运行某种配置文件解析来计算活动项目.所以这是令人困惑的部分:

  • 当项目被继承时,配置文件不会 从父pom继承到子pom.
  • 当项目集合配置文件 由聚合POM继承到模块POM.

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

  • -1这篇文章具有误导性.实际上,配置文件从父pom继承到子pom.看我的回答. (3认同)

Nor*_*mac 10

为了澄清这一点,Maven Profiles实际上是继承的.有关另一个SO问题的参考,请参阅:继承Maven配置文件.我已成功在项目中继承了配置文件,无需其他工作.

至于原始问题,你有一个在exists元素中定义的变量.根据文件:

从Maven 2.0.9开始,标签可以进行插值.支持的变量是系统属性,如$ {user.home}和环境变量,如$ {env.HOME}.请注意,POM本身定义的属性和值在此处不可用于插值,例如上面的示例激活器不能使用$ {project.build.directory}但需要对路径目标进行硬编码.

所以,我得到的是$ {project.basedir}不能使用,也不行.但是,如果将它定义为环境变量,它将起作用.

我发现一个警告是,在父pom中<plugin-management>应该用来配置插件.但是,对于配置文件,我发现<plugin-management>不能使用它来使特定于配置文件的配置起作用.


hbo*_*emy 5

问题不在于继承,而在于插值(即支持哪些值${...}):基于文件的配置文件激活仅支持有限的插值:参见http://maven.apache.org/pom.html#Activation

所以${project.basedir}不支持,但只支持${basedir}(和系统属性)。

有关更多详细信息,您可以查看模型构建算法:http : //maven.apache.org/ref/3.2.1/maven-model-builder/

完整模型插值在配置文件激活后发生:因此,即使您的有效 pom 显示 的插值值${project.basedir},在配置文件激活发生时也不会计算该值。

在Maven的3.2.2,您对这方面的多种增强功能:文档中http://jira.codehaus.org/browse/MNG-5590,在运行时警告http://jira.codehaus.org/browse/MNG-5608和更有效的 pom 结果http://jira.codehaus.org/browse/MNG-5612


use*_*849 1

一般来说,Maven 配置文件不会被继承(请参阅http://jira.codehaus.org/browse/MNG-5127了解讨论以及可能有用的博客文章链接)。我已经成功地做了这样的事情:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

另外我认为如果 P2 处于活动状态,P1 也不会处于活动状态。这是因为<activeByDefault>P1 也是如此。我认为元素名称有点误导。“默认情况下处于活动状态”意味着“始终处于活动状态”,而实际上它的意思是“仅当此 POM 中没有其他配置文件处于活动状态时才处于活动状态”。

上面是使用 Maven 3.0.x 发现的。