Easy Tutorial
❮ Maven Manage Dependencies Maven Plugins ❯

Maven Build & Project Testing

In the previous section, we learned how to create a Java application using Maven. 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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.companyname.bank</groupId>
  <artifactId>consumerBanking</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>consumerBanking</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

From the above XML code, we can see that Maven has added JUnit as the testing framework.

By default, Maven has added a source code file C:\MVN\consumerBanking\src\main\java\com\companyname\bank\App.java and a test file C:\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 executing this, we have built our project and created the final JAR file. Below are the key concepts to learn: We have given Maven two goals: first, to clean the target directory (clean), and then to package the project build output into a jar (package) file.

The packaged jar file can be found in consumerBanking\target, named consumerBanking-1.0-SNAPSHOT.jar.

The test reports are stored in the consumerBanking\target\surefire-reports folder.

Maven compiles the source code files, as well as the test source code files.

Maven then 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!

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 a Util class Util.java in it.

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;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        Util.printMessage("Hello World!");
    }
}

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 the Maven build is successful, 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!
❮ Maven Manage Dependencies Maven Plugins ❯