我想使用不同的 Maven 配置文件来设置不同的参数以满足测试需要。参数是一个 URL。测试在 Groovy 上进行。我正在尝试这个:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<useUrl>http://url</useUrl>
</properties>
</profile>
<profile>
<id>another</id>
<properties>
<useUrl>http://url2</useUrl>
</properties>
</profile>
</profiles>
常规代码:baseUrl = System.getProperty("useUrl")
System.getProperty("useUrl")
始终返回“null”。
但是如果我像这样在 surefire 插件中进行配置:
<systemPropertyVariables>
<baseUrl>${useUrl}</baseUrl>
</systemPropertyVariables>
代码 System.getProperty("useUrl")
将完全返回我所期望的 - 来自 Activity 配置文件的值(http://url
或 http://url2
).
系统信息: maven 3.2.5 window 8.1 Intellij IDEA 14.0.2
有人可以解释为什么配置文件属性不起作用吗?或者我做错了什么? 提前致谢。
最佳答案
Maven 属性不是系统属性。
如果您需要读取/使用系统属性,您必须在适当的 Maven 插件中明确定义它,例如:
maven-surefire
单元测试插件maven-jetty-plugin
用于 Jetty servlet 容器exec-maven-plugin
用于使用 maven 运行您的应用程序properties-maven-plugin
一般用途
关于java - maven 。轮廓。属性和 System.getProperty(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28972306/