Maven - SPLessons

Maven Project

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Maven Project

Maven Project

shape Introduction

In the previous chapters, configurations and architecture of Maven are covered. Maven Project chapter explains how to create a project using Maven.

Project Creation

shape Step-1

While creating any project, users should create an archetype first. An archetype is a tool used to create other things of the same kind. So, initially create archetype plug-in. For that, the user should create a folder in any drive(here D:MavenProject is taken) and enter the below command in command prompt and click on enter.

mvn archetype:generate -DgroupId=com.java.samples -DartifactId=JavaSamples
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Then following will be initiated.

shape Step-2

The following window appears asking for inputs such as

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 725: 725 (Enter 725 as shown)

Finally when entered, the following output appears.

A JavaSamples folder will be created. The following structure appears when an user imports the project in Eclipse.

Below mentioned AppTest.java file can be seen under src/test/java.

package com.java.samples;

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 );
    }
}

In the above code, Rigourous test will be automatically generated when the java project is created. This method normally returns TRUE. One can also add other new packages if required.

shape Step-3

Compile and build the project. To build a project, go to root folder in command prompt and enter mvn package. The following phases will be executed.

Validate Phase

Compile Phase

Test Phase

Packaging

shape Step-4

Packaging is created by tags that consist jar in pom.xml file.

The packaged jar file will be in target folder of JavaSamples.

<groupId>com.java.samples</groupId>
 <artifactId>JavaSamples</artifactId>
 <version>1.0</version>
 <packaging>jar</packaging>

shape Step-5

Now, run the java file App.java as shown in the following window.

The next steps of the project are shown in further chapters.

Summary

shape Key Points

  • In Maven Project, archetype has to be created first.
  • mvn package command is used to compile, build and test the project.
  • Packaging can be done using <packaging> tags in pom.xml file.

shape Programming
Tips

To test the project, use mvn test command. The reports will also be available in target\surefire-reports.