Build and Test your First Maven Project
In the previous tutorial, we have seen how to create sample maven project using maven archetype and understood how the folder structure is organized. Now, we will see how to build a project and test the code written.
Building a Maven Project
Once the coding is completed in the project, the project needs to be compiled, built and make it to be ready for the deployment.
To achieve this, Open the Command Prompt and navigate to the root project folder where it consists of the pom.xml file. E.g. D:\Java\workspace\JavaSamples. Now, type the command
mvn package and press enter. This command will invoke the maven package phase.
As explained here, whenever a maven phase is invoked, it executes in a sequence up to the invoked phase. Hence, in this case the phases above package – validate, compile and test phases will be executed.
When mvn package command is run, maven will validates and compile the source code, execute the junit test cases and packs/bundles it as per the instructions given in the tag
<packaging> in the pom.xml file. If the packaging is specified as jar, then a jar with the package will be created.
- The packaged jar file will be available under the target directory of the project. i.e. D:\Java\workspace\JavaSamples\target.
- All the test reports (junit tests details) are available in the folder D:\Java\workspace\JavaSamples\target\surefire-reports
To run the App.test java, open the command prompt and navigate to the folder D:\Java\workspace\JavaSamples\target\classes and enter the command
To run the test classes (junits), open the command prompt and from the root folder of the project, execute the command mvn test