首页 > 解决方案 > 为 Maven pom 提供外部参数并在 application.properties 文件中替换相同的参数

问题描述

我正在处理 testNg 项目,我需要为 pom.xml 提供外部参数,这些参数应该在 application.properties 文件中替换,该文件将在我的项目中进一步使用。谁能列出我需要使用的步骤和插件。

标签: mavenjunittestngpom.xml

解决方案


我已经解决了这个问题。我在 pom.xml 中添加了一个配置文件并为属性替换添加了配置。

<profiles>
        <profile>
            <id>profile1</id>
            <properties>
                <url>https://www.bing.com</url>
                <search>bitcoin</search>
            </properties>
        </profile>

    </profiles>

<build>
   <testOutputDirectory>${basedir}/target/classes</testOutputDirectory>
        <filters>
            <filter>src/main/resources/runtime.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>

运行时属性

url=${url}
search=${search}

运行测试:

mvn test -Pprofile1 -Durl=https://www.google.com -Dsearch=Blockchain

url 和 search 变量的值将在 runtime.properties 文件中替换为上述命令的参数中给出的值。

    -Durl=https://www.google.com and
    -Dsearch=Blockchain 

推荐阅读