【问题标题】:Maven. Profile. Properties and System.getProperty()马文。轮廓。属性和 System.getProperty()
【发布时间】:2015-03-10 19:20:27
【问题描述】:

我想使用不同的 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>

还有 groovy 代码:baseUrl = System.getProperty("useUrl")

System.getProperty("useUrl") 始终返回“null”。

但是如果我像这样在surefire插件中进行配置:

<systemPropertyVariables>
    <baseUrl>${useUrl}</baseUrl>
</systemPropertyVariables>

代码System.getProperty("useUrl") 将完全返回我的预期 - 来自活动配置文件的值(http://urlhttp://url2)。

系统信息: Maven 3.2.5 视窗 8.1 Intellij IDEA 14.0.2

有人能解释一下为什么配置文件属性不起作用吗?或者我做错了什么? 提前致谢。

【问题讨论】:

    标签: java maven groovy


    【解决方案1】:

    Maven 属性不是系统属性。

    如果您需要读取/使用系统属性,则必须在适当的 maven 插件中明确定义它,例如:

    • maven-surefire 单元测试插件
    • maven-jetty-plugin 用于 Jetty servlet 容器
    • exec-maven-plugin 使用 maven 运行您的应用程序
    • properties-maven-plugin 一般用途

    【讨论】:

    • 感谢您的解释!
    猜你喜欢
    • 1970-01-01
    • 2011-11-20
    • 2019-09-30
    • 2014-11-21
    • 2016-12-24
    • 1970-01-01
    • 2022-01-19
    • 2021-07-09
    • 2018-03-18
    相关资源
    最近更新 更多