简体   繁体   中英

Maven. Profile. Properties and System.getProperty()

I want to use different maven profiles to set different parameters for tests needs. Parameter is a URL. Tests are on Groovy. I'm trying this:

<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>

And groovy code: baseUrl = System.getProperty("useUrl")

System.getProperty("useUrl") always returns 'null'.

But if I make configuration in surefire plugin like this:

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

Code System.getProperty("useUrl") will return exactly what I expected - value from active profile ( http://url or http://url2 ).

System information: Maven 3.2.5 Windows 8.1 Intellij IDEA 14.0.2

Could someone explain why profile properties don't work? Or what am I doing wrong? Thanks in advance.

Maven properties are not system properties.

If you need to read/use system properties, you have to explicitly define it in a proper maven plugin, eg:

  • maven-surefire plugin for unit tests
  • maven-jetty-plugin for Jetty servlet container
  • exec-maven-plugin for running your application using maven
  • properties-maven-plugin for general usage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM