Previous – Chapter 8
Maven Tutorial – Chapter 9
In the previous project, we have seen how to create sample maven project using maven archetype and understood how the folder structure is organized. This chapter will cover building and testing the maven project created in previous chapter.
Building the 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, 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 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 and 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
java com.java.samples.App
To run the test classes (junits), open the command prompt and from the root folder of the project, execute the command mvn test
Questions? Comments? Suggestions? Let us know!! Like / Subscribe / Follow for more updates.