Maven中继承的配置文件 [英] Inherited profiles in Maven

查看:103
本文介绍了Maven中继承的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的父母pom中有以下个人资料

I have the following profiles in my parent 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在子POM中处于活动状态而P2不处于活动状态?

Why P1 is active in child POM and P2 isn't?

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

The directory ${project.basedir}/src/main/whatever, does not exist in the parent project, but exists in the child one.

推荐答案

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

Profile P2 is not activated because the path under its exists tag does not resolve to an existing path even though the directory ${project.basedir}/src/main/whatever exists. If you rewrite the property ${project.basedir} as ${basedir}, it should activate the P2 profile.

这应该意味着${project.basedir}不会解析为项目基础目录,因为它 MNG-5516 ).

That should mean that the ${project.basedir} does not resolve to the project base directory as it should. The help:effective-pom shows that it does, though. I have reported this (MNG-5516).

我还认为,如果P2处于活动状态,则P1将不会处于活动状态.

Also I think that P1 will not be active if P2 is.

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

That is correct. Quoting the documentation for activeByDefault:

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

This profile (P1 in this example) will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.

继承一词使我感到困惑,因为配置文件继承"在

The word inherit got me confused, because the "profile inheritance" works in project aggregation but not in project inheritance.

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

To make things clear, I simulated this situation. Empty pom means that it is empty except for the standard model, group, artifact and version tags.

目录结构:

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)

项目继承

目录结构:

Project inheritance

Directory structure:

inheritance
 |--child
 |  \-pom.xml         // child pom
 \-pom.xml           // parent pom

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

Child pom is empty while parent pom has the profiles as in the simple scenario. Regardless of the existence of the inheritance/child/dir directory running mvn help:all-profiles from child directory outputs:

Profile Id: P1 (Active: false , Source: pom)
Profile Id: P2 (Active: false , Source: pom)

child目录运行mvn help:effective-pom时,它表明确实没有继承配置文件.它的行为与已记录:

When running mvn help:effective-pom from child directory it shows that the profiles are indeed not inherited. It behaves as documented:

POM中已合并的元素如下:

Elements in the POM that are merged are the following:

  • 依赖项
  • 开发者和贡献者
  • 插件列表(包括报告)
  • 具有匹配ID的插件执行
  • 插件配置
  • 资源
  • dependencies
  • developers and contributors
  • plugin lists (including reports)
  • plugin executions with matching ids
  • plugin configuration
  • resources

这里没有提及个人资料.

No profiles are mentioned here.

目录结构:

aggregation
 |--module
 |  \-pom.xml         // module pom
 \-pom.xml           // aggregator pom

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

Module pom is empty while aggregator pom has the profiles as in the simple scenario. If there is no aggregation/module/dir directory running mvn help:all-profiles from module directory outputs:

Profile Id: P1 (Active: true , Source: pom)
Profile Id: P2 (Active: false , Source: pom)

如果module目录输出中有运行mvn help:all-profilesaggregation/module/dir目录:

If there is aggregation/module/dir directory running mvn help:all-profiles from module directory outputs:

Profile Id: P2 (Active: true , Source: pom)
Profile Id: P1 (Active: false , Source: pom)

module目录运行mvn help:effective-pom时,它表明配置文件已被继承.这不是明确记录的:

When running mvn help:effective-pom from module directory it shows that the profiles are inherited. This is not explicitly documented:

项目继承

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

If you have several Maven projects, and they all have similar configurations, you can refactor your projects by pulling out those similar configurations and making a parent project. Thus, all you have to do is to let your Maven projects inherit that parent project, and those configurations would then be applied to all of them.

注意:

  • 这不适用于个人资料,如已显示.
  • 运行inheritance目录中的Maven构建将仅运行父构建.
  • That does not apply to profiles, as it has been shown.
  • Runnnig a maven build from inheritance directory will run only parent build.

项目汇总

如果您有一组一起构建或处理的项目,则可以创建一个父项目,并使该父项目将这些项目声明为其模块.这样,您只需要构建父级,其余的就可以了.

And if you have a group of projects that are built or processed together, you can create a parent project and have that parent project declare those projects as its modules. By doing so, you'd only have to build the parent and the rest will follow.

注意:

  • 运行aggregation目录中的maven构建将运行每个模块和聚合器的构建(实际顺序由maven根据不同的标准确定).
  • Runnnig a maven build from aggregation directory will run the build of each module and the aggregator (the actual order is determined by maven based on different criteria).

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

Profiles can be defined globally, per user or per project. Since the aggregated projects are built together (in the same build) some sort of profile resolution must be run to calculate the active ones. So this is the confusing part:

  • 当项目继承时,配置文件 不是 从父pom继承到子pom .
  • 当项目是聚合配置文件时 从聚合器pom继承到模块pom .
  • When projects are inherited profiles are not inherited from parent pom to child pom.
  • when projects are aggregated profiles are inherited from aggregator pom to module pom.

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

This was tested this using Maven 3.1.0. and 3.0.5.

这篇关于Maven中继承的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆