Java Maven属性加载顺序

Java Maven属性加载顺序,java,maven,Java,Maven,我知道Maven属性可以在不同的位置定义: 本地计算机上的~/.m2/settings.xml 项目父POM中的 项目子模块POM中的 在项目父POM的Maven配置文件中 项目子模块POM的Maven配置文件中的 直接在命令行上执行-D 但属性的加载顺序并不十分清楚。有人能解释一下它的顺序吗?根据我的测试,属性的优先级似乎如下,其中1。优先于2。;2.优先于3。等等 -D通过命令行访问属性 设置.xml中的中的 子pom中的中的 直接在子pom中 父pom中的中的 直接在父pom中 因此,

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

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

但属性的加载顺序并不十分清楚。有人能解释一下它的顺序吗?

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

  • -D
    通过命令行访问属性
  • 设置.xml中的
    中的
  • 子pom中的
    中的
  • 直接在子pom中
  • 父pom中的
    中的
  • 直接在父pom中
  • 因此,一般而言:

    • 命令行先于一切
    • 设置在父项之前的子项之前
    • 直接定义属性之前的配置文件
    我使用以下设置对其进行了测试

    settings.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>
    
    
    4.0.0
    测试
    测试父母
    0.0.1-快照
    聚甲醛
    父母亲
    真的
    父配置文件
    
    子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>
    
    
    4.0.0
    测试
    测试儿童
    0.0.1-快照
    聚甲醛
    测试
    测试父母
    0.0.1-快照
    parent/pom.xml
    小孩
    真的
    子配置文件
    org.apache.maven.plugins
    maven antrun插件
    1.7
    验证
    跑
    

    像这样运行它并删除
    echo
    ed的属性,只要还有一个属性就重复操作。

    问题是:为什么这很重要?这很重要,因为定义的属性值可能会被另一个位置定义的相同属性覆盖。在不知道确切顺序的情况下,避免这种情况的唯一解决方案是只在一个位置定义属性@此外,同一属性可能在POM的多个配置文件中定义。如果两个配置文件都被激活,哪一个会赢?如果在同一pom的多个配置文件中定义了一个属性,则赢的值似乎是:文件中最后定义的配置文件中的一个。下面是一个非常旧的先例列表。当时,配置文件按字母顺序进行评估。这可能已经改变了。请注意,在上面的列表中,项目编号较低的页面上较高的内容优先于项目编号较高的页面上较低的内容。我知道,这是个个人问题,不知道列表的顺序。@LeeMeador谢谢你的评论,我更新了我的答案以明确顺序。这其中哪一个被认为是系统属性?