Maven 配置文件属性在检索时始终给出 null

问题描述 投票: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>org.country</groupId>
    <artifactId>CountryProfileMavenDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <id>australia</id>
            <properties>
                <country.code>AU</country.code>
                <capital>Canberra</capital>
                <currency>AUD (Australian Dollar)</currency>
            </properties>
        </profile>
        <profile>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <id>newzealand</id>
            <properties>
                <country.code>NZ</country.code>
                <capital>Wellington</capital>
                <currency>NZD (New Zealand Dollar)</currency>
            </properties>
        </profile>
    </profiles>
</project>

对于以下代码,

package codes;

import org.testng.annotations.Test;

public class EnvironmentProfile {

    /*
    Based on the active Maven profile, Maven will set system properties (country.code, currency, and capital) accordingly.
      mvn clean install -P australia test
     To see which profile is active mvn help:active-profiles
     */
    @Test
    public void testPrintEnvironmentDetails() {
        System.out.println("Country Code: " + System.getProperty("country.code"));
        System.out.println("Capital City: " + System.getProperty("capital"));
        System.out.println("Currency: " + System.getProperty("currency"));
    }
}

从 IntelliJ Studio 我选择了配置文件并运行了代码,但我得到的输出为

国家代码:null 首都:null 货币:null

我尝试使用以下命令使用命令行,但没有成功。

mvn clean install -P australia test -DskipTests=false
java maven
1个回答
0
投票

按照@khmarbaise的建议,通过引用systemPropertyVariables中maven配置文件中定义的属性来解决。

在此 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>org.country</groupId>
    <artifactId>CountryProfileMavenDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
        <country.code>AU</country.code>
        <capital>Canberra</capital>
        <currency>AUD (Australian Dollar)</currency>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <country.code>${country.code}</country.code>
                        <capital>${capital}</capital>
                        <currency>${currency}</currency>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>australia</id>
            <!-- No need to define properties here -->
        </profile>
        <profile>
            <id>newzealand</id>
            <properties>
                <country.code>NZ</country.code>
                <capital>Wellington</capital>
                <currency>NZD (New Zealand Dollar)</currency>
            </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>org.country</groupId>
    <artifactId>CountryProfileMavenDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>australia</id>
            <properties>
                <country.code>AU</country.code>
                <capital>Canberra</capital>
                <currency>AUD (Australian Dollar)</currency>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <systemPropertyVariables>
                                <country.code>${country.code}</country.code>
                                <capital>${capital}</capital>
                                <currency>${currency}</currency>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>newzealand</id>
            <properties>
                <country.code>NZ</country.code>
                <capital>Wellington</capital>
                <currency>NZD (New Zealand Dollar)</currency>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <systemPropertyVariables>
                                <country.code>${country.code}</country.code>
                                <capital>${capital}</capital>
                                <currency>${currency}</currency>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
© www.soinside.com 2019 - 2024. All rights reserved.