How to Create a New Maven Project

We are going to see what is Maven and How to Create a New Maven Project in here.

If you are even remotely connected to the IT world, you might have already come across what everybody is calling Maven. They have been using it and I know it must be baffling if you haven’t encountered it yet. Don’t worry! We have got you covered in this post which will tell you everything that you need to know about Maven and how to get started with it.

What is Maven?

Maven is nothing but a project management tool that uses POM (Project Object Model) to manage every project’s build. It also sets a standard in the industry to be used by the developers and testers making their jobs relatively easy in terms of weaving up a build.

maven meme

Maven is created around the concept of build lifecycles. The following are Maven’s default build lifecycles:

  • default
  • clean
  • site

It makes the process of build easy, provides uniformity in the build system, presses on best practices and makes migration easy.

If you think this is the right tool for you to use in order to work on a project from scratch, just keep going forward where we will see How to Create a New Maven Project.

Prerequisites for Installing Maven

Since Maven is a Java tool, you need to have Java installed on your system. If you don’t have that you can always flip back to check how to do that and ensure if Java is indeed installed.

The next important thing to learn How to Create a New Maven Project is Eclipse or any other IDE.

If you are using the latest Eclipse version, Maven comes installed by default. If it isn’t there in your projects, you can always install it manually by navigating to Help > Install New Software…

Then copying in the following URL in the Work with textbox:

http://download.eclipse.org/technology/m2e/releases

and then pressing Enter.

Select Maven integration for Eclipse checkbox and then select Next, and then saying yes to the usual agreement etc.

You don’t have to go through all the hassle if you are using the latest Eclipse version as stated earlier. Here I have been using Oxygen, and so Maven comes in here by default. No worries!

How to Create a New Maven Project

So we are finally here in the How to Create a New Maven Project section. I have created a step by step guide to help you understand How to Create a New Maven Project. Just follow these and you will be fine:

Step 1: Click on File menu and then on New > Project…

A dialog box would open after that.

Step 2: Type Maven in the Wizards text box, or simply look for Maven folder. Click on it and then find Maven Project under that folder. Then click on Next:

Step 3: For now you can skip archetype selection. Just check the box for it and then click on Next.

The next screen will ask you to enter group Id and Archetype Id. They are nothing but something that helps Maven to identify your project across all projects.

Step 4: Just type the group Id and Artifact Id as something unique you have in mind. Once done click on Finish.

When you click on Finish, a new project will be created showcasing the following directory structure:

It is the standard Maven structure that’s preferred in many organizations. Working in a standard structure helps others to understand where all your crucial files are located and how to go about them.

However, we aren’t done yet. It is just the structural part of How to Create a New Maven Project. Let’s move on!

.M2 Repository

At this point of time, you will realize that Maven has created a .m2 folder from where all the required jar files will be picked from.

.m2 is basically a repository that holds all the libraries needed to run your program. It dynamically (at runtime) downloads everything necessary (and stated, of course) for the proper functioning of your code.

Here’s how to open the .m2 folder:

Step 1: Type Run in your Windows search box or simply press Windows + R to open the Run dialog box.

click run on start search bar

which might look something like this when opened:

.m2 in run

Step 2: Type .m2 and click enter.

It will take you to the .m2 folder.

repository insides of .m2 folder

Here you can see all kinds of libraries that are downloaded once you run the mvn install command.

NOTE: Don’t fret if you don’t see many folders inside the repository. Remember these will be downloaded at runtime.

Alternatively,

You can simply go to your Users folder located in C: drive, go to your username folder and then look for the folder called .m2. It is the same one!

m2 location in users folder

In .m2 all the exact versions of the jar you need to run your code will get installed directly from the internet as directed in the pom.xml file.

That takes us to the next section of How to Create a New Maven Project as to what exactly goes into the Pom.xml.

Understanding Pom.xml

Pom is an xml file where you can house all your dependencies that will get downloaded when you run your project. It is also the place where you can link up your testng.xml, and then run its Maven command to execute pom. Doing so you will not have to run your testNG every time, instead, you can simply provide the name of the suite you wish to run from the command line. More on this later.

pom location

If you open pom.xml for the first time (double-click it), here’s the Overview page that will be shown to you:

pom overview

The overview section is where you will have all your project information inscribed.

As you can see in the bottom section there are plenty of tabs provided in order for you to effectively use POM. Click on the Dependencies tab and you will get the following:

dependencies tab

Dependencies are nothing but crucial libraries that are needed by your project to run itself.

If you click on the Add… button you can add a dependency on the fly.

Let’s go ahead and add one.

Adding a Dependency

Each website will have a dependency to be added to your project. For e.g. we need Selenium to be integrated into our project, go to their website, and you will have a dependency waiting to be used.

Let’s try it for Selenium first:

Step 1: Navigate to the following website:

https://www.selenium.dev/maven/

It might look something like this:

selenium information for mamven

Look for the dependency section marked above. We will use this information and fill it in the Add dependency section like this:

Step 2: Click on Add… in the dependencies tab of pom.xml as depicted in the previous image. It will give you a dialog box like this:

select dependency

Step 3: Now copy the Group Id, Artifact Id, and Version and paste them one by one in the blank spaces like this:

enter dependency and ok

Step 4: Once done click on Ok.

You will notice that there will be an entry now in the dependencies list:

dependencies list

A Better Alternative of Adding Dependency

A better way to do that is using the pom.xml tab which is nothing but the backend version of what we just did.

Here all you have to do is click on the pom.xml tab in the bottom:

pom option

It will present the pom.xml something like this:

pom xml default appearance

Now just copy the following dependency which was on the Selenium website:

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.13.0</version>
</dependency>

and paste it in the pom.xml right before the </project> tag.

Then surround it with <dependencies> opening and closing tag like this:

dependencies in pom.xml

That’s it! Doesn’t that save you a lot of time?

In a similar fashion, all the dependencies that we wish to add will go inside the <dependencies></dependencies> tags.

Other Dependencies

Here are a couple of other dependencies that you might need in order to run a Selenium program smoothly:

  • TestNG

You can find it here:

http://testng.org/doc/maven.html

Just look for the dependency block and copy-paste it in a similar fashion.

testng dependency

I have pasted this for your perusal:

testng dependency added in dependencies section

In a similar fashion, you can copy-paste the dependency of Extent Reports if you are working with it. I plan on showing that in a separate article.

Adding Plugins in Pom.xml

Time to add some amazing plugins that will allow you to run your code smoothly. We need the following plugins for that:

  • Maven Compiler – To compile main and test source files
  • Maven Surefire – To run the unit tests of an application

In order to add the compiler and surefire plugins, you need to create a separate <plugins> tag. Place it in a <build> tag. Just open it and close it like this:

<build>

</build>

Inside the <build> tag provide the <plugins> tag like this:

<build>

<plugins>

</plugins>

</build>

Now you can add n no. of plugins inside the <plugins> tag.

Maven Compiler Plugin

Maven Compiler Plugin as mentioned earlier helps in compiling main and test source files. In order to make sure you add it to your pom, just do the following:

Simply copy-paste the following inside the <plugins> tag:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>

<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Doing so will ensure that your code gets compiled properly.

Maven Surefire Plugin

In a similar fashion, you require Surefire plugin to run unit tests of an application and to generate reports in either .txt format or .xml format. You can see a separate folder created for these reports.

You can also use the <configuration> section of this code to specify your suite XML files location. At the suite location, you can keep all your testNG files in one place. Then use them dynamically as and when needed through command prompt.

Don’t worry, I will show you how.

Before that let’s just add this as well inside the <plugins> tag:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>

<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/${suiteXml}</suiteXmlFile>
</suiteXmlFiles>
</configuration>

</plugin>

So this will be the content

At the location mentioned above (src/test/resources) we will place our testNG.xml file which will be located by pom while running our program.

That concludes the hard part of our dependency section in How to Create a New Maven Project tutorial. Now let’s write a small piece of code to see how to execute a program in maven.

An Example Program

How to Create a New Maven Project tutorial wouldn’t be complete unless we see it in action. Isn’t that right?

Let’s get to it!

Step 1: Create a class in the Test > Java folder. To do that you can just right-click the Java folder and then click on New > Class:

class in test java

Step 2: In the opened dialog box of “New Java Class” provide the package name and class name and then click on Finish.

new class in Java

Step 3:  Quickly write a small program like this in the created Java class:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class MavenTester {

@Test
public void openGoogle() {
     WebDriver driver = new ChromeDriver();
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
driver.get("https://www.google.com");
}
}

This program will simply open Google in a new Chrome browser.

Time to create its testNG.xml file.

Step 4: Right click on src/test/resources > New > File:

Step 5: Type the name of the file as testng.xml. Then click on Finish:

creating a testng xml file

Step 6: In the newly created testng.xml file, insert content and paste the following data:

<!--<?xml version="1.0" encoding="UTF-8"?>-->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="AutomationTest">

    <test name="Opening Google">

        <classes>

            <class name="testing.MavenTester"/>

        </classes>

    </test>

</suite>

where “testing” was my package.

testng location in eclipse

Now at this point of time, we are supposed to provide the location of our testng.xml in pom.xml. But we have already done it while copying pasting the Maven Surefire Plugin info.

That’s our location mentioned in this line:

<suiteXmlFile>src/test/resources/${suiteXml}</suiteXmlFile>

In this resources folder, you could have put multiple testNG xml files. This way you can create n number of xml files and make a dynamic variable to be run from pom.

Execution of Program

There are two ways to run our little program now. First one being, simply right click the pom.xml. Look for Run As > Maven install.

Maven install in run as pom.xml

You can also do Maven build which will automatically perform the installation as well, downloading all the relevant files mentioned in your pom.xml at runtime.

You can also select the Maven Clean option when you want to delete the previous build, and start a fresh build. Doing so will basically delete and flush out the target folder where all the temp class files, properties and reports are created with every build. You can manually delete the target too and it won’t affect a thing.

The Second and Much Better Way (Maven Commands)

The second option is I think better since you can run the exact testNG file you wish to run without having to make any changes in the pom.xml file.

Using the cmd console to enter the Maven commands. Here’s a list of all the Maven commands that you can use while building your project:

  • mvn clean
  • mvn install
  • mvn clean install
  • mvn build
  • mvn clean build
  • mvn site
  • mvn compile
  • mvn pre-clean
  • mvn package

Let’s simply perform a maven install to execute our program.

Step 1: Navigate to your project folder where the pom.xml is located.

Don’t know what your project location is? You can simply right-click on your project and then on Properties.

right click project then properties

Step 2: Copy the location from the part mentioned below:

location of project maven

NOTE: Or you can simply click on the small icon next to it that will say: “Show in System Explorer”

Step 3: Then paste it in your explorer and double-click your project name to reveal the folder

double click folder

which will basically reveal the folder where our pom is located as shown in the image below:

folder where pom is located

Step 4: Just click on the address bar at the top and then type “cmd“.

type in address bar cmd

Step 5: Press Enter.

cmd pom location project

Voila! You will notice that Command Prompt has opened directly at the said location.

Step 6: Type the following to run maven:

mvn install

and then press enter.

You will notice that the downloading of all the dependency begins with that.

mvn install on cmd

Once your program gets executed you get a Build Success notification like this in Green which means there are no hiccups.

How to Create a New Maven Project results

So Google opens! That means we won!

google site opens

Did you like our How to Create a New Maven Project tutorial? If yes, tell us about it in the comments section below.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply