Search code examples
mavencommand-line-interfaceproperties-file

Pass in a properties file as a command line argument


I have a test suite (jUnit, Selenium, Cucumber) Maven project.

I need to be able to run the tests from the command line, passing in different properties files as arguments to diversify the test cases. How can I do this?

I currently have a properties reader that has a path to a shared properties folder concatenated with a variable that holds the name of a given properties file. I'm wondering if that can be parameterized for use with a Maven command in the CLI?

I've been researching this for awhile and have found many questions that sound similar to what I'm trying to achieve, but none of the answers have been applicable to my situation/what I'm trying to do. Any advice, ideas, or resources given will be greatly appreciated.


Solution

  • You can simply pass java properties to Maven:

    $ mvn clean test -Dmyproperty=some-property-file.properties
    

    Then you can access the property in your test:

    @Test
    public void test() {
        String propertyFile = System.getProperty("myproperty");
        assertEquals("some-property-file.properties", propertyFile);
    }