YouTip LogoYouTip

Maven Build Test Project

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. Navigate to the C:/MVN folder and open the consumerBanking folder. You will see a pom.xml file with the following code: 4.0.0com.companyname.bankconsumerBankingjar1.0-SNAPSHOTconsumerBankinghttp://maven.apache.orgjunitjunit3.8.1test From the above XML code, we can see that Maven has already added JUnit as the testing framework. By default, Maven adds a source file **C:MVNconsumerBankingsrcmainjavacomcompanynamebankApp.java** and a test file **C:MVNconsumerBankingsrctestjavacomcompanynamebankAppTest.java**. Open the command console, navigate to the C:MVNconsumerBanking directory, and execute the following mvn command to start building the project: C:MVNconsumerBanking>mvn clean package Scanning for projects... ------------------------------------------------------------------- Building consumerBanking task-segment: [clean, package] ------------------------------------------------------------------- [clean:clean {execution: default-clean}] Deleting directory C:MVNconsumerBankingtarget ......... [jar:jar {execution: default-jar}] Building jar: C:MVNconsumerBankingtarget consumerBanking-1.0-SNAPSHOT.jar ------------------------------------------------------------------------ BUILD SUCCESSFUL ------------------------------------------------------------------------ Total time: 2 seconds Finished at: Tue Jul 10 16:52:18 IST 2012 Final Memory: 16M/89M ------------------------------------------------------------------------ After execution, we have built our project and created the final jar file. Here are the key concepts to learn: * We gave Maven two goals: first, clean the target directory (clean), then package the project build output into a jar (package). * The packaged jar file can be found in consumerBankingtarget, named consumerBanking-1.0-SNAPSHOT.jar. * Test reports are stored in the consumerBankingtargetsurefire-reports folder. * Maven compiles the source files and the test source files. * Then Maven runs the test cases. * Finally, Maven creates the project package. C:MVNconsumerBankingtargetclasses>java com.companyname.bank.App You will see the result: Hello World! Adding Java Source Files Next, let's see how to add other Java files to the project. Open the C:MVNconsumerBankingsrcmainjavacomcompanynamebank folder and create a Util class named Util.java. ## Util.java package com.companyname.bank; public class Util{public static void printMessage(String message){System.out.println(message); }} Update the App class to use the Util class: ## App.java package com.companyname.bank; public class App{public static void main(String[]args){Util.printMessage("Hello World!"); }} Now open the command console, navigate to the C:MVNconsumerBanking directory, and execute the following mvn command. C:MVNconsumerBanking>mvn clean compile After Maven builds successfully, navigate to the C:MVNconsumerBankingtargetclasses directory and execute the following java command. C:MVNconsumerBankingtargetclasses>java -cp . com.companyname.bank.App You will see the result: Hello World!
← Maven SnapshotsMaven Build Profiles β†’