Signup/Sign In

Creating your First Project in Maven

In the earlier lessons, we have seen about the maven project architecture and the different configurations involved for a project. Now, we will understand how a sample Java project can be created using Maven. Maven can also be used to build and manage projects developed using C#, Ruby, Scala and few other languages. But, this is primarily used for java projects, so we will be using Java for examples. Also, we will see how the project is structured when created using maven.


Maven Archetype

Maven uses archetype to create any project. Archetype is nothing but the tool provided by the maven to create a project. It is also defined as 'an organizational pattern from which all other things of the similar kind are made'.

Using archetypes provides an easy and instrumental way to create projects to the developers, where the best practices are employed as per the standards defined at the organization level. Also, archetypes enables the developers to quickly create a project and start running the same within a matter of commands.

In order to create a new simple maven java project just use the maven archetype plugin in the command line. Currently there are more than 850+ maven archetypes listed out in order to create projects under different frameworks like struts, spring, hibernate, web services (RESTful/SOAP) and many others.

So, let us create one simple project in maven. Open the command prompt and navigate to the workspace folder in your local machine (E.g. D:\Java\workspace). You can create the Workspace folder wherever you want on your PC or use your Workspace, if already created. Then type the below maven command(its a single line command) and press enter.

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

This maven command will create a Java project with the below details:

  • groupId will be com.java.samples, representing the package.
  • artifactId will be JavaSamples (on the build, JavaSamples.jar will be created)
  • archetypeArtifactId is nothing but the template used for creating this Java project.
  • interactiveMode is used when the developer is aware of the actual spelling of the artifact id. In the above case maven-archetype-quickstart is the one which is used and it is the proper one. If developer is not aware of this then the interactiveMode is set to be TRUE so that it will scan the remote repositories for all available archetypes. This might take longer time.

Creating first Maven Project

Creating first Maven Project

The above command instructs the maven to create a project with maven-archetype-quickstart template.

If you navigate to the folder D:\Java\workspace, a new project named JavaSamples will be created including all the base structure of a java project as well as shown below :

Creating first Maven Project

To see the following directory structure you need to Create a New Java Project on your Eclipse IDE, and import the following project there.

Table below will describe the main directories that are created as a part of the project.

Plugin nameDescription
JavaSamplesRoot folder of the project containing source and pom.xml
src\main\javaContains all the java code under the package com.java.samples
src\main\testContains all the java test code under the package com.java.samples
src\main\resourcesContains all the resources like xml\properties file

Under the folder D:\Java\workspace\JavaSamples\src\main\java\com\java\samples a sample java file called App.java will be created by default which will be having one print statement.

package com.java.samples;

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

And under the folder D:\Java\workspace\JavaSamples\src\test\java\com\java\samples a sample java test class (JUnit) will be created AppTest.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);
    }
}

The test class code which is marked as Rigorous Test is the code which is automatically generated when a sample java project is created using maven. This method will just return TRUE and the test will be passed after the run. This method is just included as one sample test. You can modify this method or add your own test methods.

Also, the TestCase class is an abstract class from the Junit API which is used here in the sample project. This will also be included by default automatically when a sample maven java project is created (check the dependency entry for junit in the pom.xml). The TestCase class defines the fixtures to run mutilple tests in a class.

Developers are required to add their files as described in the above mentioned table. They are very much free to add any new packages as well as required during the project development. The above said folder structure is just created as per the archetype plugin defined.


Versions in Maven

There are 2 major versions made available for any maven project:

  • SNAPSHOT: This is nothing but the development version. New version's availability will be verified during every checkout and downloaded if it is required. This version is not allowed for releases.
  • FIXED: Downloaded only once to the local repository. This is obtained whenever a major version of the software release happens.