本文介绍了Maven属性加载顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that Maven properties can be defined in different locations:

  • ~/.m2/settings.xml在本地计算机上
  • 项目父POM中的
  • <properties>
  • 项目子模块POM中的
  • <properties>
  • 项目父POM的Maven配置文件中的
  • <properties>
  • 项目子模块POM的Maven概要文件中的
  • <properties>
  • -D直接在命令行上
  • ~/.m2/settings.xml on the local machine
  • <properties> in the project parent POM
  • <properties> in the project child module POM
  • <properties> in Maven profile of the project parent POM
  • <properties> in Maven profile of the project child module POM
  • -D directly on the command line

但是不清楚按什么顺序加载属性.有人可以解释其顺序吗?

But it's not very clear in which order the properties are loaded. Could somebody explain its order?

推荐答案

根据我的测试,属性的优先级似乎如下,其中1.优先于2. 2.优先于3.,依此类推.

Based on my tests, the precedence of properties seems to be the following, where 1. takes precedence over 2.; 2. takes precedence over 3. and so on.

    通过命令行
  1. -D属性
  2. settings.xml中的<profile>中的
  3. <properties>
  4. 子pom中<profile>中的
  5. <properties>
  6. <properties>直接在子pom中
  7. 父pom中<profile>中的
  8. <properties>
  9. <properties>直接在父pom中
  1. -D property via command line
  2. <properties> in <profile> in settings.xml
  3. <properties> in <profile> in the child pom
  4. <properties> directly in child pom
  5. <properties> in <profile> in the parent pom
  6. <properties> directly in parent pom

一般来说:

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

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

I tested it with the following setup.

settings.xml

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>

子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>

以这种方式运行并删除echo属性,只要有一个属性就重复一次.

Run it like this and delete the property which is echoed, repeat as long as there is a property left.

这篇关于Maven属性加载顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 02:54