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  
-DartifactId=simple-maven-project  
-Dpackage=com.companyname.projectname  
-Dversion=1.0-SNAPSHOT
Note that whole command should be single line. After build success, we will see below output in command line console.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[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:
simple-maven-project
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
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.

App.java

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="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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.companyname.projectname</groupId>
  <artifactId>simple-maven-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-maven-project</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
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] BUILD SUCCESS
[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
Output:
Hello World!

Conclusion

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

Comments