Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
Building a Maven Project
Once you create and complete the project's coding, you must build, test and deploy it.
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:
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.
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.
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 test, mvn 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:
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.
After successfully building your project, you’ll get the results -
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:
Then add the following code in your pom.xml, save and rebuild it
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.
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.