Maven Build & Project Testing
In the previous chapter, we learned how to use Maven to create a Java application. Next, we will learn how to build and test this project.
Go to the C:/MVN folder and open the consumerBanking folder. You will see a pom.xml file, with the following code:
From the above xml code, you can see that Maven has added JUnit as a test framework.
By default, Maven adds a source fileC:\MVN\consumerBanking\src\main\java\com\companyname\bank\App.javaand a test fileC:\MVN\consumerBanking\src\test\java\com\companyname\bank\AppTest.java。
Open the command console, navigate to the C:\MVN\consumerBanking directory, and execute the following mvn command to start building the project:
C:\MVN\consumerBanking>mvn clean package
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------
[INFO] Building consumerBanking
[INFO] task-segment: [clean, package]
[INFO] -------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory C:\MVN\consumerBanking\target
...
...
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: C:\MVN\consumerBanking\target\
consumerBanking-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Jul 10 16:52:18 IST 2012
[INFO] Final Memory: 16M/89M
[INFO] ------------------------------------------------------------------------
After execution, we have built our project and created the final jar file. The following are key concepts to learn:
- We gave Maven two goals: first clean the target directory (clean), then package the project build output as a jar (package) file.
- The packaged jar file can be found in the consumerBanking\target directory, with the name consumerBanking-1.0-SNAPSHOT.jar.
- Test reports are stored in the consumerBanking\target\surefire-reports folder.
- Maven compiles the source files, as well as the test source files.
- Then Maven runs the test cases.
- Finally, Maven creates the project package.
C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App
You can see the result:
Hello World!
Add Java Source File
Next, let's see how to add other Java files to the project. Open the C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder and create the Util class Util.java in it.
Util.java
Update the App class to use the Util class:
App.java
Now open the command console, navigate to the C:\MVN\consumerBanking directory, and execute the following mvn command.
C:\MVN\consumerBanking>mvn clean compile
After Maven builds successfully, navigate to the C:\MVN\consumerBanking\target\classes directory and execute the following java command.
C:\MVN\consumerBanking\target\classes>java -cp . com.companyname.bank.App
You can see the result:
Hello World!Other Extensions