Learn How to Build and Test Projects in Maven

0
1640
Test Projects

Test Projects

In the last article, we discussed about the use of Maven’s archetype plugins which we have used to create The JAVA project structure. In this article, we are going learn how to build and test that JAVA application project created through Maven before.

Step 1: – Navigate to C:/MVN_JAVA folder, where we have created our JAVA application project structure in the last article and open project folder. There you will notice the pom.xml file which has the following contents.

In the above pom.xml file, you will notice that Maven has automatically added the Junit test framework dependency. Also, Maven had added the source files App.java and AppTest.java in the main and test folders respectively.

Step 2: – We need JDK/JRE to be present on our machine, where we are building the JAVA project in order to compile the JAVA project through Maven. When building through command line, we may need to modify our pom.xml file and provide the javacpl.exe path (Java Compiler) as shown below.

Note: – The javacpl.exe path may vary depending on the JRE (Java Runtime Environment) version. Here, we are using JRE 1.8.0_91 version and therefore, our javacpl.exe (Java compiler) path is C:\Program Files\Java\jre1.8.0_91\bin\javacpl.exe

Step 3: – Open a window’s command line console and change directory to C:\MVN_JAVA\project

Step 4: – Execute the following Maven command in order to build the project.

After executing above command on console, you can observe that Maven has started building the JAVA application project as shown below.

Explanation: –
After execution of the Maven command, the following goals will be executed and the package can be procured in the manner as explained below.
• Here, we have given two goals for the Maven plugin. The first goal is to clean the target directory (i.e. clean) and the second goal (i.e. package) is to package the project build output as JAR file.
• The Packaged JAR file can be procured from C:\MVN_JAVA\project\target folder as project-1.0-SNAPSHOT.jar.
• The test reports can be procured from C:\MVN_JAVA\project\target\surefire-reports folder.
• Here, the Maven first compiles the source code file and then tests those source code file i.e. both compilation and validation are taking place in the sequence.
• Maven runs the test cases present in the AppTest.java file.
• Finally, if no has error occurred during compilation and validation process, then Maven will generate the package.

Execute the package: –
The next step is to execute the package and get the output. Open the command line console and navigate to C:\MVN_JAVA\project\target where package project-1.0-SNAPSHOT is present. Execute the following command on the console.

Source Code for JAVA application project created through Maven

Conclusion: –
In this article, we discussed on how to build and test the JAVA application project created through Maven. Post building package, we have executed the package to get the output.

LEAVE A REPLY

Please enter your comment!
Please enter your name here