如何在Java中获取当前的Maven配置文件ID

huangapple go评论71阅读模式
英文:

How to get current maven profile Id in java

问题

我正在使用 Maven 和 Selenium WebDriver 进行开发,但遇到了困难。我有一组 Maven 配置文件:

    <profiles>
        <profile>
            <id>demo</id>
            <properties/>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties/>
        </profile>
    </profiles>

我的任务很简单,但我找不到解决方案。在我调用例如 `mvn test -P prod` 后,如何获取并将当前配置文件的 id 存储到变量中?这个 id 之后在测试中会用到。
英文:

I am working with selenium webdriver with maven and struggling. I have a set of maven profiles

&lt;profiles&gt;
        &lt;profile&gt;
            &lt;id&gt;demo&lt;/id&gt;
            &lt;properties/&gt;
            &lt;activation&gt;
                &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
            &lt;/activation&gt;
        &lt;/profile&gt;
        &lt;profile&gt;
            &lt;id&gt;prod&lt;/id&gt;
            &lt;properties/&gt;
        &lt;/profile&gt;
    &lt;/profiles&gt;

My task is quite simple but i can't find a solution. How can i get and store into variable id of the current profile after i call for example mvn test -P prod? This id is needed later in tests.

答案1

得分: 4

我正在使用这个解决方案:
首先,在您的配置文件中添加属性,即:

  <profile>
        <id>prod</id>
         <properties>prod<properties>
   </profile>

然后创建属性文件 app.properties,并添加条目:

mvnprod=${prod}

在您的代码中读取属性文件。

英文:

I am using this solution:
First add property to your profile i.e.

  &lt;profile&gt;
        &lt;id&gt;prod&lt;/id&gt;
         &lt;properties&gt;prod&lt;properties&gt;
   &lt;/profile&gt;

Than create properties file app.properties with entry

mvnprod=${prod}

read the property file in your code.

huangapple
  • 本文由 发表于 2020年8月21日 01:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定