0

I have qa.properties and uat.properties file in the project. I have added profiles tag, env tag in properties tag and environment in configuration tag of the maven surefire plugin in the pom.xml file. I have created envProperties.java class to load the properties based on the env value passed from the pom.xml or mvn test -Pqa from command prompt but it is always executing using the deafult value passed in the envProperties.java class.

I have invalidated the caches and loaded the project, executed mvn clean, mvn install but still facing the same issue. How can I handle the issue?

pom.xml:

<properties>
    <env>flex</env>
</properties>

<profiles>
    <profile>
        <id>uat</id>
        <properties>
            <env>uat</env>
        </properties>
    </profile>
    <profile>
        <id>qa</id>
        <properties>
            <env>qa</env>
        </properties>
    </profile>
    <!-- Add profiles for other environments as needed -->
</profiles>

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <includes>
                    <include>**/*DemoRunner.java</include> 
                </includes>
                <testFailureIgnore>false</testFailureIgnore>
                <systemPropertyVariables>
                    <environment>${env}</environment>
                </systemPropertyVariables>
            </configuration>
        </plugin>

envProperties.java:

public class ConfigurationReader {
    private static Properties properties = new Properties();
    static {
        String env = System.getProperty("env","qa");
        try {
            FileInputStream file = new FileInputStream(env+".properties");
            properties.load(file);
            file.close();

        } catch (IOException e) {
            System.out.println("Properties file not found.");
        }
    }

   
    public static String getProperty(String keyWord){
        return properties.getProperty(keyWord);
    }

}

1 Answer 1

1

You still need to pass the argument: You mvn install command doesn't do this.

But you don't need surefire for this.

You can simply run:

mvn install -Denv='qa'

or

mvn install -Denv='uat'

Then System.getProperty("env") will return the equivalent string you passed.

1
  • Hello Joao, Can I set the env value in the pom.xml file and run the Cucumber Runner class to execute the tests based specific to the env value? Commented Dec 19, 2023 at 15:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.