Maven - 将默认参数更改为其他 pom.xml 定义

Maven - changing defaulted argument to other pom.xml defined

我有一个 testNG 运行 maven 的测试发行版。每当我需要默认测试分发时,我都可以这样做,每当我只需要几个或一个测试时,我可以在命令行中更改类似 -DoverallParams 的内容(尽管名称已更改)。

<properties>
    <param1>x</param1>
    <param2>y</param2>
    <param3>z</param3>
    <defaultParams>${param1},${param2}</defaultParams>
    <defaultParams2>${param1},${param3}</defaultParams2> <-This does not exist yet!
    <overallParams>${defaultParams}</overallParams>
</properties>

我现在需要对不同的平台使用不同的测试集,而不需要复制或分支项目。 所以这个想法是添加一个 defaultPrams2 并以某种方式在命令行中 selecting 它。

问题

有没有办法在命令行中添加一些东西,使我 select defaultParams2 成为 overallParams?更简单的是,有没有办法在命令行中引用 pom 属性? 您对如何执行此操作有其他想法吗?

Is there any way to have something in the command line which would make me select defaultParams2 as the overallParams?

是的,Maven profiles 可以帮助您。

您可以在 pom 中包含以下内容:

<profiles>
    <profile>
        <id>meaningful-name-here</id>
        <properties>
            <overallParams>${defaultParams2}</overallParams>
        </properties>
    </profile>
</profiles>

然后您可以从命令行调用 Maven,如下所示:

mvn clean install -Pmeaningful-name-here

基本上,-P<id> 选项将激活上面的配置文件,然后将覆盖 overallParams 属性 的值,因此切换到 defaultParams2 值运行时和按需。

这种方法比以下方法更不容易出错:

mvn clean install -DoverallParams=params

每次都需要键入所需参数的地方(因此会根据需要覆盖 overallParams 的值)。只需选择比 meaningful-name-here 更好(更短)的 ID :)

is there a way to reference a pom property in command line?

是,通过显式覆盖 (-D) 或通过配置文件 (-P),如上所述。