Create and run a maven project

In the previous post, we have learnt about the pom.xml. In this post, we will create a project and execute it through maven commands.

We have already seen how to install Maven, creating maven project and importing into eclipse. We have also seen POM.xml generated by maven. Now, let’s see a detailed tutorial how to create and run a maven project step by step.

Step# 1 Create a maven project as shown in this post and import it into eclipse.

Step# 2 You will be able to see a folder “src/test/java“. For testing purpose , we need to use this folder. We will create the packages and classes (or say test cases) inside this. Let’s create a package named as “com.learnmvn“. After that, we have created 2 classes inside the same package.

Class1: LoginClassTest

Class 2: ChangeNameTest

Step# 3 Create a package “com.resources” and under that create a testng.xml file where we will add both the classes.

The whole project will look like this.

 

Maven Project

Step# 4 Now, we need to configure pom.xml file. You will find by default junit dependency added into that. You can remove this dependency as we are running test cases with testNG and add the dependency for testNG as given below(You can change version according to requirement).

Step# 5 We will create the surefire report via this project. For that , we need to add the sure fire plugin into POM.xml.

Step# 6 As you can observe includes tags in the above code, we have defined some formats like **Test* and *Test etc. That means surefire will recognize the test cases which is having Keyword Test into its test case name i.e. LoginTest etc.

Step# 7 We can add the plugin for reporting as given below.

Step# 8 Finally the whole POM.xml file will look like as given below.

Step# 9 Now, when everything is set up so we can run the test cases. For that open a command window by going into start menu. Then navigate to project path by using the CD command.

cd c:\sample\FirstMavenProj

Step# 10  There are few commands which does the following.

  • mvn clean : It will clean the target folder under the project.
  • mvn test: It will run the test cases.
  • mvn site: It will run test cases as well as generate the surefire report under a newly create folder named as site.
  • mvn surefire-report:report  – You can generate the report by using this command as well.

We can also run the commands altogether i.e. mvn clean site.

After running the command, you should get the “Build Success” message that means everything is working fine. You will also see the test case running as given below.

Maven output

Step# 11 Now, under the folder “target –> site ” , you can see surefire-report.html. It will have all the details about the test case executed.

Maven report

Maven report


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

POM.xml in Maven
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *