Search code examples
javamavenseleniumpropertiesmaven-profiles

Maven Profile reads data from Properties file


I have a Selenium project which uses Maven as a build tool and I want to read different environment details (protocol, domain, subdomain, etc) from a .properties file. Would it be possible to use Maven profiles in order to run my tests on different environments such as dev, staging, prod based on the profile which I am specifying when triggering the mvn command?

dev.properties:

protocol=http
domain=domain.com
subdomain=www

pom.xml:

  <profiles>
    <profile>
      <id>prod</id>
      <activation>
       <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>pre_prod</id>
    </profile>
    <profile>
      <id>dev</id>
    </profile>
  </profiles>

mvn:

mvn clean test -Pdev

Data should then be retrieved in the java code using

System.getProperty("protocol");
System.getProperty("domain");
System.getProperty("subdomain");

Any help would be very much appreciated.

Thanks!


Solution

  • If you just want to read different properties files based on a command line argument, could you not just pass in a string for example -Denv=dev

    Then in your properties reader class have it init the properties file based on System.getProperty("env");

    Properties generalProperties = new Properties();
        String generalPropertiesFileName = "data/"+ 
          System.getProperty("env") + ".properties";
       initProperties(generalProperties, generalPropertiesFileName);
    

    Alternatively you can also pass properties from the command line to your POM in the same way -

    <properties>
      <property>
        <protocol></protocol>
        <domain></domain>
        <subdomain></subdomain>
      <property>
    <properties>
    

    And then these can be passed in from the command line as -Dprotocol=foo etc

    Hope that helps?