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

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

How to get current maven profile Id in java

问题

  1. 我正在使用 Maven Selenium WebDriver 进行开发,但遇到了困难。我有一组 Maven 配置文件:
  2. <profiles>
  3. <profile>
  4. <id>demo</id>
  5. <properties/>
  6. <activation>
  7. <activeByDefault>true</activeByDefault>
  8. </activation>
  9. </profile>
  10. <profile>
  11. <id>prod</id>
  12. <properties/>
  13. </profile>
  14. </profiles>
  15. 我的任务很简单,但我找不到解决方案。在我调用例如 `mvn test -P prod` 后,如何获取并将当前配置文件的 id 存储到变量中?这个 id 之后在测试中会用到。
英文:

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

  1. &lt;profiles&gt;
  2. &lt;profile&gt;
  3. &lt;id&gt;demo&lt;/id&gt;
  4. &lt;properties/&gt;
  5. &lt;activation&gt;
  6. &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
  7. &lt;/activation&gt;
  8. &lt;/profile&gt;
  9. &lt;profile&gt;
  10. &lt;id&gt;prod&lt;/id&gt;
  11. &lt;properties/&gt;
  12. &lt;/profile&gt;
  13. &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

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

  1. <profile>
  2. <id>prod</id>
  3. <properties>prod<properties>
  4. </profile>

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

mvnprod=${prod}

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

英文:

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

  1. &lt;profile&gt;
  2. &lt;id&gt;prod&lt;/id&gt;
  3. &lt;properties&gt;prod&lt;properties&gt;
  4. &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:

确定