Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to configure Maven and create a test application

In this article, we will learn what Maven is and why do we use Maven. Also we will see the process to configure Maven and create test desktop application.

Maven is the java build tool that helps the create enterprise application in the standard directory structure and resolve the dependency to compile and run the application. Maven is used to create deployable artifact. It is project management tool that provide build capabilities.

Maven in detail:

Maven is the superset of Ant. Hence it is similar to Ant that is a build tool. But, Maven is the Project Management Build Tool that helps to generate reports, code for project management.

Before Maven, we were facing following listed problems:

  • Jar File needed to be placed into the CLASSPATH: We were required to place required jar file manually into the CLASSPATH.
  • Dependency: We had to take care of resolving dependency on other jar file required by the jar files placed into the CLASSPATH.
  • Project Structure: We had to create the enterprise project directory and file structure.
  • Building, publishing and deploying of the project needed to be done manually or using ANT.

Maven helps us to resolve these four listed problems above.

To configure Maven on our system, we are required to download a Maven runtime environment from its official website: http://maven.apache.org/download.cgi

We may download the appropriate version of maven for our system by this url. Once, we have downloaded “Maven”, unzip this and follow listed steps to configure “Maven”:

Set Environment Variable: M2_HOME that refers to the base directory path for the Maven.

  • For Linux: export M2_HOME={base-dir-path}
  • For Windows: set the environmental variable into the system

Now add the {Maven-base-dir-path} to System Path Environmental Variable.

  • For Linux: export PATH = {base-dir-path}:${PATH}
  • For Windows: append the {Maven-base-dir-path} to System Environmental Variable Path.

Now check for the configuration for Maven using command: mvn -version <-

Check Maven Configuration Status

Figure 1: Check Maven Configuration Status

The above figure shows the Maven version “Apache Maven 3.1.0-alpha-1”.

Once we have configured Maven, it ask to “Maven Repository” for “Archetype” and “Dependencies”. The ArcheType is the list of pre-defined architecture type for enterprise desktop, web applications and etc.

To create an enterprise java desktop application directory structure using Maven, Open console and place command: mvn archetype:generate <-

Download Default ArcheType from Maven Repository

Figure 2: Download Default ArcheType from Maven Repository

The above figure shows downloading list of ArcheType from Maven Repository and ask to crate Architecture Type for An Application. We are asked to place following entries to create an archetype:

  • Give an archetype number downloaded from the repository
  • Give the version number
  • Give the groupId: It is analogous to package name for an application classes.
  • Give the artifectId: It is analogous to class name to be created
  • Give the version: It gives the default version for the application that can be modified.
  • Give the package: The package name for ArcheType
  • Now confirm the creation as Y/N
Creating Default ArcheType

Figure 3: Creating Default ArcheType

The above figure shows the configuration for default ArcheType created. The default archetype number is 282, the default version number is 6. The ‘groupId’ is placed as “com.test.mvn”, the ‘artifactId’ is placed as “MvnTest”, the default ‘version’ property is “1.0-SNAPSHOT” and the default ‘package’ property is “com.test.mvn”.

Once it’s done, it creates a directory structure with the pom.xml file.

Listing 1: pom.xml

<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.test.mvnApp</groupId>
  <artifactId>MvnTestApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MvnTestApp</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>

The above code creates a pom.xml file that defines the <groupId>, <artifactId>, <version>, <packageing> and <name> contain the values that was given, while the archetype was creating in the console.

Pom.xml also defines the tag that defines the dependency on JUnit. The <dependencies> tag contains sub tag <dependency> that describes the JUnit type dependency for version 3.8.1.

Directory Structure:

The above figure shows the directory structure created for the ArcheType number 284. It creates two java class files:

  • App
  • AppTest
  • And a pom.xml file.
Directory Structure Created by Maven

Figure 4: Directory Structure Created by Maven

The App class defines the main method that displays addition operation of two numbers.

Listing 2: App.java

package com.test.mvnApp;

/**
 * Add to numbers placed as command line argument!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
		char ch = args[0].charAt(0);
		int a = Integer.parseInt(args[1]);
		int b = Integer.parseInt(args[2]);
		
		switch(ch){
			case 'a':
				System.out.println("Addition of Two Given number from command line: " + new Operation().add(a, b));
			break;
			case 's':
				System.out.println("Substraction of Two Given number from command line: " + new Operation().sub(a,b));
			break;
			case 'd':
				System.out.println("Division of Two Given number from command line: " + new Operation().div(a,b));
			break;
			case 'm':
				System.out.println("Multiplication of Two Given number from command line: " + new Operation().mul(a,b));
			break;
			default:
				System.out.println("Invalid argument passed!!");
			break;
		}
    }
}

The above code defines App class that defines the main() method, which accept two numbers as command line arguments and produce addition these two numbers using wrapper class Interger method parseInt().

Another class created by Maven defined in the test package as AppTest. This class extends the TestCase class.

Listing 3: AppTest.java

package com.test.mvnApp;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Maven tests the compiled application for defined test cases using above defined class AppTest that extends TestCase class. These test cases are defined into the TestCase class.

These are two default classes are created by the Maven ArcheType. To compile the Maven ArcheType, place command into the command prompt: mvn compile <-

Compile ArcheType application

Figure 5: Compile ArcheType application

The above figure shows, how to compile archetype application.

The above command compiles all the class files defined in the main package and applies test cases on the main package using defined class AppTest defined in test package.

The compiled files are package into new directory created “target”. To package this compiled application, use command: Mvn package <-

Package Compiled ArcheType Application

Figure 6: Package Compiled ArcheType Application

The above figure shows, how to package the compiled archetype application.

This command creates a jar file of this compiled application. To run the main class from the jar file, follow the listed command as shown into the figure:

Execute main() method of App class defined into the com.test.mvnApp package

Figure 7: Execute main() method of App class defined into the com.test.mvnApp package

java –cp MvnTestApp-1.0-SNAPSHOT.jar com.test.mvnApp.App 1 1

The above command loads MvnTestApp-1.0-SNAPSHOT.jar file and execute main() method of App class. We pass two numbers 1 and 1 as command line argument that product “Addition of Two Given number from command line: 2”.

Now define one more class Operation.java into the main.com.test.mvnApp package that will product basic operations by accepting three command line arguments:

  • 1st Argument: ‘a’ for Add or ‘s’ for Sub or ‘d’ for Div or ‘m’ for Mul
  • 2nd Argument: 1st Operand or digit
  • 3rd Argument: 2nd Operand or digit

Listing 4: Operation.java

package com.test.mvnApp;

public class Operation{
	public int add(int a, int b){
		return a + b;
	}
	public int sub(int a, int b){
		return a - b;
	}
	public int div(int a, int b){
		return a / b;
	}
	public int mul(int a, int b){
		return a * b;
	}
}

The above code defines an Operation class that declares four methods add(), sub(), div() and mul() respectively. The main() method of App class calls the appropriate method for submitted command line arguments or display the error message, if wrong set of arguments are passed.

Figure 8 shows execution of application Operation class that may perform addition, subtraction, division and multiplication operation.

Execution of main() method of App class using Operation Class

Figure 8: Execution of main() method of App class using Operation Class

The above figure shows the multiplication of two numbers using mul() method of Operation class.

Conclusion:

In this article, we learn:

  • What is Maven and why do we use Maven?
  • How do we configure Maven and create test desktop application?


Have good knowledge on Java, HTML, CSS and Android platform and is pursuing Masters in Computer Applications.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login