Execute Cucumber from Command Line Maven

Profile picture for user devraj

To execute Cucumber command line from terminal using Maven you need to add Maven surefire plugin in your pom.xml file. Compiler version is also recommended.

Maven surefire Plugin: The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in two different file formats Plain text files (.txt) XML files (.xml). To know the latest version and to get the more information about the Surefire plugin click here.

Maven Compiler Plugin: The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse. To read more click here.

<build>
    <pluginManagement>
      <plugins>
            <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>  
      
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M4</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

To execute cucumber project, navigate to directory where pom.xml is located and type below command:

$ mvn test

Make sure your Runner class filename should starts or ends with Test keyword. Example filename: TestRunner.java or RunnerTest.java.