Maven 属性加载顺序

2024-03-30

我知道 Maven 属性可以在不同的位置定义:

  • ~/.m2/settings.xml在本地机器上
  • <properties>在项目父POM中
  • <properties>在项目子模块POM中
  • <properties>在项目父 POM 的 Maven 配置文件中
  • <properties>在项目子模块 POM 的 Maven 配置文件中
  • -D直接在命令行上

但目前还不清楚属性的加载顺序。有人可以解释一下它的顺序吗?


根据我的测试,属性的优先级似乎如下,其中 1. 优先于 2.; 2. 优先于 3. 等等。

  1. -D通过命令行属性
  2. <properties> in <profile> in settings.xml
  3. <properties> in <profile>在子 pom 中
  4. <properties>直接在子pom中
  5. <properties> in <profile>在父 pom 中
  6. <properties>直接在父pom中

所以一般来说:

  • 一切之前的命令行
  • 设置在子级之前,父级之前
  • 在直接定义属性之前配置文件

我使用以下设置对其进行了测试。

设置.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <custom.prop>settings</custom.prop>
            </properties>
        </profile>
    </profiles>
</settings>

父pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <custom.prop>parent</custom.prop>
    </properties>
    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <custom.prop>parent-profile</custom.prop>
            </properties>
        </profile>
    </profiles>
</project>

子pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test-child</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <parent>
        <groupId>test</groupId>
        <artifactId>test-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>parent/pom.xml</relativePath>
    </parent>
    <properties>
        <custom.prop>child</custom.prop>
    </properties>
    <profiles>
        <profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <custom.prop>child-profile</custom.prop>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="${custom.prop}" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

像这样运行它并删除属性echoed,只要还有财产就重复。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Maven 属性加载顺序 的相关文章

随机推荐

  • Python:有没有办法阻止从 int 到 long int 的自动转换发生?

    考虑这个例子 gt gt gt from sys import maxint gt gt gt type maxint
  • 如何在 Android 上使用 onPause?

    using onSaveInstanceState Bundle and onRestoreInstanceState Bundle was really good and work but it is working when click
  • Java 堆空间与 GSON

    我正在使用 GSON 创建一些大的 JSON 文件 以从 GTFS Google Transit 创建自定义 JSON 问题是当我从对象类转换为 JSON 时 Gson gson new Gson String rutasJSON gson
  • 用于测试是否正在构建应用程序扩展的预处理器宏是什么?

    这个问题纯粹基于公开发布的有关 iOS 中应用程序扩展引入的文档 随着引入应用程序扩展 https developer apple com library prerelease ios documentation General Conce
  • AppCompat v7:21 拆分操作栏损坏?

    我目前正在开发一个应用程序 其中使用经过大量修改的拆分操作栏 以下是应用程序当前状态的链接 您会注意到顶部有一个透明的操作栏 其中有一个自定义视图 底部有一个拆分在一起的操作栏 底部视图实际上是一个单独的操作项 其中包含一个自定义视图 并且
  • 如何设置gridview行颜色,我尝试过,但它显示强制转换无效?

    protected void gvcolors RowDataBound object sender GridViewRowEventArgs e if e Row RowType DataControlRowType DataRow in
  • 如何添加UIActionSheet按钮复选标记?

    我想知道如何以最简单的方式在 actionSheet 按钮右侧添加复选标记 下面是截图Podcasts app 请注意 该解决方案可能会在将来的 iOS 更新中崩溃 我是 访问未记录的私有 API 这样的解决方案非常脆弱 请参阅下面的评论
  • 如何将密码文本与 bcrypt 哈希值进行比较?

    我的应用程序中有一个用例 应该阻止用户在重置密码时选择最近 3 个密码之一 我使用 Angular 作为前端 使用 Spring Boot 作为后端 在我的场景中 用户密码存储为 bcrypt 哈希 如何将用户输入的密码与最近存储的 3 个
  • 如何替换R数据框中多列中的多个值?

    假设我有以下数据框 真正的数据框是 10 个 labelx 列 id lt c 1 2 3 4 5 6 7 8 label1 lt c apple shoe banana hat dog radio tree pie label2 lt c
  • 当焦点从角度输入中移出时触发验证?

    当我们不断在文本框中输入内容时 电子邮件验证将被触发 我希望当用户将焦点移出文本框时触发此验证 下面是我的代码
  • 有没有一种好的方法可以在Linux中禁止屏幕保护程序?

    我正在寻找一种体面的 不蹩脚的方式来抑制 xscreensaver kscreensaver 或 gnome screensaver 无论哪一个可能正在运行 最好以与屏幕保护程序无关的方式 并且它绝对必须执行fast 我读过xscreens
  • 如何使用ABAddressBook删除iPhone通讯录中的记录?

    我正在学习地址簿框架 然后我想从iPhone通讯录中删除记录 我检查了文档并发现了一个名为ABAddressBookRemoveRecord 但我找不到删除记录的方法 比如用户选择一条记录 然后点击删除按钮 然后该记录就会被删除 到目前为止
  • 安卓 |内容观察者 |内容 URI 不包含资源 ID

    我正在尝试检测 Android 应用程序上的屏幕截图 我使用 contentObserver 来检测媒体目录中的更改 而不是使用 FileObserver 因为已知问题 https code google com p android iss
  • 通过 Internet 发送 UDP 数据包

    我正在尝试了解 P2P 去中心化网络的一些细节 我的问题如下 假设我有两台名为 comp1 和 comp2 的机器 现在 comp1 设置在我的家庭网络中的路由器后面 comp2 位于我的办公室中 也位于路由器后面 我是否可以像这样在 In
  • WriteFile 返回错误代码 87

    我正在开发一个写入 HID 设备的程序 但收到错误 87 WriteFile 函数参数无效 我从 Jan Axelson 的 USB Complete 获得了这些功能 所以我不确定为什么会收到错误 我用它来查找我的设备 private vo
  • ItemsSource 绑定不更新值

    我需要列出物品清单 我将用户集合绑定到列表框 一切都运行良好 但列表框中的项目不会实时更新 它们根本不会通过此绑定进行更新 因此 当我从列表中删除任何用户时 即使正确更改了列表框的源 列表框也不会更新 源位于数据视图模型中的路径 DataV
  • 禁止 Ruby 中的特定警告

    我看过很多帖子提供 W0标记作为此问题的答案 但我不想抑制所有警告 而只是抑制特定值的警告 我正在 Ruby 1 8 7 上运行一个非 Rails 应用程序 尽管它使用 ActiveRecord 我想保留除以下弃用警告之外的所有警告 Obj
  • Android 辅助功能对讲来说出片段的标题

    有没有办法让 Android 可访问性中的对讲功能在片段事务后说些什么 我基本上希望对讲在切换后说出片段的名称 标题 这些标题设置为操作栏的标题 可以在那里访问吗 用户可以将手指移动到操作栏中的文本顶部来了解他们所在的屏幕 但除非他们已经熟
  • 应用程序池回收如何影响 ASP Net 会话状态?

    我知道当应用程序池被回收时 会启动一个新的工作进程 但我对在此过程中如何处理过期和有效会话感到困惑 哪些传递给新的工作进程线程 哪些被消除 它将做什么 用户A的会话已过期 用户B的会话有效 回收后 当用户A和用户B请求时 他们的会话状态会是
  • Maven 属性加载顺序

    我知道 Maven 属性可以在不同的位置定义 m2 settings xml在本地机器上