Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

How to Build and Test a Maven Project?

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

You must know that Maven is a robust build management tool for making Java and Web-related projects. This article will teach you how to build and test a Maven project. Before learning how to build and test a Maven project, you must know how to create a Maven project. In case you need to learn about it, refer creation of the Maven project in NetBeans.

How to build and test a Maven project?

Eclipse IDE

Eclipse is an open-source Integrated Development Environment written in Java for Java applications like Java SE and Java EE. With the help of extensible plug-ins, it supports other programming languages like - C, C++, Perl, Python, etc. 

This article uses Eclipse to create, build and test the Maven project. To learn how to integrate Maven with Eclipse.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Building a Maven Project

Once you create and complete the project's coding, you must build, test and deploy it.

Screenshot of Java code in Maven.

Here is a simple project named - MavenProject. It consists of the codingNinjas package; democlass is the class having a method called MyMethod, which returns a string. It has a JUnit test case that tests the behavior of methods in our classes and ensures that our expectations are met. The dependency is added in the pom.xml file of the JUnit test scope, as shown below:

Dependencies Screenshot

It is just an example; you can create any project you want. We'll emphasize only the main topic of this article, i.e., building and testing of the Maven project.

You can build and test the project on the command line and Eclipse (any IDE). 

Using Command Line

Open the command prompt and go to the directory where you have created the project. 

Changing the directory on the console.

Enter the command:

mvn clean package


This command gave two goals to Maven. clean in the command is to clean the target directory, and the package is for packaging the project build output as a JAR. The packaged jar is present as MavenProject-1.0-SNAPSHOT.jar in the target directory.  

Wait for the project to build and test. If you get BUILD SUCCESS, as shown in the image below, congratulations, you have successfully created and tested your Maven project. If not, then there might be some errors - can be compilation errors, etc. which you need to resolve first. After that, it will build and test successfully.

cln package command in console.

Test reports of the project are stored in the src/test/java folder. 

Maven first compiles (builds) the source code, tests the source code, runs the test cases, and, at last, creates the package.

The testing, cleaning, etc., of the project, can be done using individual commands like  

mvn testmvn clean

Using Eclipse IDE

The GUI of Eclipse helps in building and testing the project. Create the project and save it. For building and testing - 

Right Click on the project → Run as → Maven Clean.

First, clean your project, and after successful cleaning, you'll get the following result:

Result of cleaning the project on Eclipse IDE.

After cleaning, build your project using the same method - Right Click on the project → Run as → Maven Build. A dialog box will appear in which you need to mention the goals. For instance, it can be compile. Click Run.

Screenshot of dialog box while building the project.

After successfully building your project, you’ll get the results - 

Result of building the project in Eclipse IDE

If there are no errors in your code, the build will be successful; else, you need to resolve those errors. 

At last, for testing your Maven project, Right click on the project → Run as → Maven test.

In case your Maven project does not get tested, and the following error is shown:

Screenshot of unsuccessful testing of the Maven project.

Then add the following code in your pom.xml, save and rebuild it

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>


The error comes on using an older version of the Maven compiler. Version 7 or above helps in resolving this error. With the help of the above code, the compiler is brought into a newer version, i.e., 8. On testing the project again, you'll find that your project has been successfully tested.

Result of successful testing of the Maven project.

Yes, you are thinking the right way; it is that simple to build and test a Maven project. With the addition of this topic to your skillset, create more and more projects.

Frequently Asked Questions

How to check if Maven is installed or not?

On Windows and Mac, the installation is checked using the mvn -version command, whereas on Linux mvn -v command is used for the same.

Do I need to learn all the Maven commands?

No, you can remember the mvn help command, which will list all the Maven commands.

What if I miss the testing part of the Maven project?

Creating, cleaning, building, and testing phases are interrelated. You will only be able to deploy your project if you test it.

For what purpose is the build tool used?

It compiles and packages the code as a JAR/ZIP file and places it in the local, central, or remote repository.

What happens when the mvn package command is run?

Package is one of the phases of the Maven build lifecycle. When the mvn package is run, it executes every phase, which is in sequence till the phase defined.

Conclusion

You learned about the Maven project's building and testing in this article. You can build and test the Maven project on any Java IDE which supports the Maven example - NetBeans. First, you create a project on Maven, clean, build and test it.

Check out other related articles:

Also, check out some of the Guided Paths on topics such as Data Structures and Algorithms, Competitive Programming, Operating Systems, Basics of Java, System Design, etc., as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy learning :)

Topics covered
1.
Introduction
2.
Eclipse IDE
3.
Building a Maven Project
4.
Using Command Line
5.
Using Eclipse IDE
6.
Frequently Asked Questions
6.1.
How to check if Maven is installed or not?
6.2.
Do I need to learn all the Maven commands?
6.3.
What if I miss the testing part of the Maven project?
6.4.
For what purpose is the build tool used?
6.5.
What happens when the mvn package command is run?
7.
Conclusion