Software DevelopmentLearn How to Build and Test Projects in Maven

Learn How to Build and Test Projects in Maven

Test Projects

In the last article, we discussed about the use of Maven’s archetype plugins which we have used to create The JAVA project structure. In this article, we are going learn how to build and test that JAVA application project created through Maven before.

Step 1: – Navigate to C:/MVN_JAVA folder, where we have created our JAVA application project structure in the last article and open project folder. There you will notice the pom.xml file which has the following contents.

<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.eduonix</groupId>
  <artifactId>project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>project</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

In the above pom.xml file, you will notice that Maven has automatically added the Junit test framework dependency. Also, Maven had added the source files App.java and AppTest.java in the main and test folders respectively.

Step 2: – We need JDK/JRE to be present on our machine, where we are building the JAVA project in order to compile the JAVA project through Maven. When building through command line, we may need to modify our pom.xml file and provide the javacpl.exe path (Java Compiler) as shown below.

<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.eduonix</groupId>
  <artifactId>project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>project</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
<build>
<plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
                <executable>C:\Program Files\Java\jre1.8.0_91\bin\javacpl.exe</executable>
            </configuration>
        </plugin>
    </plugins>
</build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Note: – The javacpl.exe path may vary depending on the JRE (Java Runtime Environment) version. Here, we are using JRE 1.8.0_91 version and therefore, our javacpl.exe (Java compiler) path is C:\Program Files\Java\jre1.8.0_91\bin\javacpl.exe

Step 3: – Open a window’s command line console and change directory to C:\MVN_JAVA\project

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\MVN_JAVA\project

C:\MVN_JAVA\project>

Step 4: – Execute the following Maven command in order to build the project.

mvn clean package

After executing above command on console, you can observe that Maven has started building the JAVA application project as shown below.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MVN_JAVA\project\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\MVN_JAVA\project\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project ---
[INFO] Surefire report directory: C:\MVN_JAVA\project\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.eduonix.project.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ project ---
[INFO] Building jar: C:\MVN_JAVA\project\target\project-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.862 s
[INFO] Finished at: 2017-09-23T19:53:02-04:00
[INFO] Final Memory: 10M/108M
[INFO] ------------------------------------------------------------------------

Explanation: –
After execution of the Maven command, the following goals will be executed and the package can be procured in the manner as explained below.
• Here, we have given two goals for the Maven plugin. The first goal is to clean the target directory (i.e. clean) and the second goal (i.e. package) is to package the project build output as JAR file.
• The Packaged JAR file can be procured from C:\MVN_JAVA\project\target folder as project-1.0-SNAPSHOT.jar.
• The test reports can be procured from C:\MVN_JAVA\project\target\surefire-reports folder.
• Here, the Maven first compiles the source code file and then tests those source code file i.e. both compilation and validation are taking place in the sequence.
• Maven runs the test cases present in the AppTest.java file.
• Finally, if no has error occurred during compilation and validation process, then Maven will generate the package.

Execute the package: –
The next step is to execute the package and get the output. Open the command line console and navigate to C:\MVN_JAVA\project\target where package project-1.0-SNAPSHOT is present. Execute the following command on the console.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\MVN_JAVA\project\target

C:\MVN_JAVA\project\target>java -cp project-1.0-SNAPSHOT.jar com.eduonix.project.App
Hello World!

C:\MVN_JAVA\project\target>

Source Code for JAVA application project created through Maven

Conclusion: –
In this article, we discussed on how to build and test the JAVA application project created through Maven. Post building package, we have executed the package to get the output.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -