Enroll in Selenium Training

There are two ways of creating Maven project. One by using command prompt and other from with in the eclipse IDE. Let's just see both of it one by one.

Create a New Maven Project from Command Prompt

  1. Go to Run and type 'cmd' to open Command Prompt.

Project_1

  1. Browse to the folder where you want to set up your project and then type the below command:

mvn archetype:generate -DgroupId=ToolsQA -DartifactId=DemoMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Project_2

Note: 'cd..' is used to go back to previous folder and 'cd foldername' used to go into the folder.

Here 'DartifactId' is your project name and 'DarchetypeArtifactId' is the *type *of Maven project. There are different types of maven projects like web projects, java projects, etc. You can go through the complete list here.

Once you press Enter after typing the above command, it will start creating the Maven project.

Project_3

Note: In case of build failure, please check the Maven version number in the pom.xml file. It should match the version of Maven installed on your machine. Look for this in the POM 'http://maven.apache.org/POM/3.2.3'. Correct the version from all the places it is mentioned in the POM.

  1. Go to the project location to see the newly created maven project. Now open the pom.xml file, which resides in the project folder. By default the POM is generated like this:
<project xmlns="https://maven.apache.org/POM/3.2.3" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/3.2.3 http://maven.apache.org/maven-v3_2_3.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ToolsQA</groupId>
  <artifactId>DemoMavenProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>DemoMavenProject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  1. Look at the default folder structure of the Maven project.

Project_4

Note: Test cases reside under the src > test > java > ToolsQA will only be considered as a test by Maven, rest will be ignored if you put your test cases in some other folder.

Maven Project to work with Eclipse

  1. Open command prompt and browse to your Maven project and type this 'mvn eclipse:eclipse'

Project_5

Your project is now compatible with Eclipse IDE.

Import Maven Project into Eclipse

  1. Open Eclipse and go to File > Import.

Project_6

2) Click on Existing Projects into Workspace.

Project_7

  1. Browse to Maven project folder and click on Finish.

Project_8

4) Project Explorer will now look like this:

Project_9

  1. Modify the POM. The default pom.xml s too simple, oftentimes you need to add the compiler plugin to tell Maven which JDK version to compile your project.
<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.3.2</version>
		<configuration>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
</plugin>

Update the jUnit from 3.8.1 to latest 4.11

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.11</version>
	<scope>test</scope>
</dependency>

Now your POM will look like this:

<project xmlns="https://maven.apache.org/POM/3.2.3" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/3.2.3 http://maven.apache.org/maven-v3_2_3.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ToolsQA</groupId>
  <artifactId>DemoMavenProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>DemoMavenProject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
	  <plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>1.6</source>
				<target>1.6</target>
			</configuration>
		</plugin>
	  </plugins>
	</build>
</project>

Once you modify the POM, do the Maven Project to work with Eclipse step again. Open command prompt and browse to your Maven project and type this 'mvn eclipse:eclipse'

Project_10

Run your first Maven Test

  1. Right-click on the pom.xml and go to Run As > Maven test.

Project_11

  1. In the console window of Eclipse, you would see the information like this:

Project_12

  1. Go to 'SureFire Reports' folder and open the xml file.

Project_13

  1. It will display the result of the JUnit test.

Project_14

How to Install Maven on Mac
How to Install Maven on Mac
Previous Article
How to Create a New Maven Project in Eclipse
How to Create a New Maven Project in Eclipse
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback