Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Apache Maven Builds and Tests Engineering


May 26, 2021 Maven


Table of contents


Maven - Build and test engineering

What we learned in the Create Engineering section is how to create Java apps using Maven. Now we'll see how to build and test the app.

Jump to the C:/MVN directory, both in your java app directory. O pen the consumerBanking folder. You'll see the .xml pom file in the file.

<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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.companyname.projectgroup</groupId>
      <artifactId>project</artifactId>
      <version>1.0</version>
      <dependencies>
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
         </dependency>
      </dependencies>  
</project>

As you can see, Maven has added JUnit as a test framework. The default Maven adds a source file App.java and a test file, AppTest.java into the default directory structure we mentioned in the previous section.

Open the command console, jump to the C:\MVN\consumerBanking directory, and execute the following mvn command.

C:\MVN\consumerBanking>mvn clean package

Maven will begin construction.

[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] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\
resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\
resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\consumerBanking\target\
surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[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] ------------------------------------------------------------------------

Now that you've built your project and created the final jar file, here are the key concepts to learn:

  • We gave maven two goals, first to clean up the target directory, and then to package the output of the project build as a jar file.
  • The packaged jar file is available at consumerBanking-target and is named consumerBanking-1.0-SNAPSHOT.jar.
  • The test report is stored in the consumerBanking, target, surefire-reports folder.
  • Maven compiles the source file, as well as tests the source file.
  • Maven then runs the test case.
  • Finally, Maven creates the project package.

Now open the command console, jump to the C:\MVN\consumerBanking\target\classes directory, and execute the java command below.

C:\MVN\consumerBanking\target\classes>java com.companyname.bank.App

You can see the results:

Hello World!

Add a Java source file

Let's see how to add additional Java files to the project. Open the C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder, where you create the Util class 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.

package com.companyname.bank;

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

Now open the command console, jump to the C:\MVN\consumerBanking directory, and execute the following mvn command.

C:\MVN\consumerBanking>mvn clean compile

After the Maven build is successful, jump to the C:\MVN\consumerBanking\target\classes directory and execute the java command below.

C:\MVN\consumerBanking\target\classes>java -cp com.companyname.bank.App

You can see the results:

Hello World!