Guide to Create a Simple Maven Project

In this guide, we learn a simple project created from scratch using the Maven Archetype plugin. This elementary application provides us with the opportunity to discuss some core Maven concepts while you follow along with the development of the project.

This guide aims to introduce you to the simplest possible Maven project and then presents some of the core concepts that make Maven a solid build platform. After reading it, you’ll have a fundamental understanding of the build lifecycle, Maven repositories, dependency management, and the Project Object Model (POM).

You can also create Maven projects in Eclipse IDE. Check out below articles:

Creating a Project

Let's start a new Maven project, use the Maven Archetype plugin from the command line.

You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
mvn archetype:generate -DgroupId=com.companyname.projectname  
Note that whole command should be single line. After build success, we will see below output in command line console.
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:23 min
[INFO] Finished at: 2018-06-20T10:50:40+05:30
[INFO] ------------------------------------------------------------------------
You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
cd simple-maven-project

Maven Standard Directory Layout

Once we’ve generated a project, take a look at the directory structure Maven created under the simple directory:
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `--
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
This generated directory adheres to the Maven Standard Directory Layout. let's understand these few basic directories:
  1. The Maven Archetype plugin creates a directory simple-maven-project that matches the artifactId. This is known as the project’s base directory.
  2. Every Maven project has what is known as a Project Object Model (POM) in a file named pom.xml. This file describes the project, configures plugins, and declares dependencies.
  3. Our project’s source code and resources are placed under src/main. In the case of our simple Java project this will consist of a few Java classes and some properties file. In web project, this could be the document root of a web application or configuration files for an application server. In a Java project, Java classes are placed in src/main/java and classpath resources are placed in src/main/resources.
  4. Our project’s test cases are located in src/test. Under this directory, Java classes such as JUnit or TestNG tests are placed in src/test/java, and classpath resources for tests are located in src/test/resources.

The Maven Archetype plugin generated a single class com.companyname.projectname.App,which prints 'Hello World!'
package com.companyname.projectname;

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

Simple Project Object Model(pom.xml)

<project xmlns="" xmlns:xsi=""
Let's Building a Simple Project using maven command mvn install or mvn package The created directory simple-maven-project contains the pom.xml and you can easily build the project:
> cd simple-maven-project
> mvn install
The command line will print out various actions, and end with the following:
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.640 s
[INFO] Finished at: 2018-06-20T11:53:12+05:30
[INFO] ------------------------------------------------------------------------
You’ve just created, cleaned, compiled, tested, packaged, and installed the simplest possible Maven project.

Run Packaged JAR

You may test the newly compiled and packaged JAR with the following command:
java -cp target/simple-maven-project-1.0-SNAPSHOT.jar com.companyname.projectname.App
Hello World!


We hope this quick overview has piqued your interest in the versatility of Maven. The source code for this simple maven web project is available on GithubGithub Repository : Simple Maven Application