How to create a simple Java project with Maven

In this article we will learn how to create a simple java project with Maven. We also review project structure and the commands for building the project.

1. Installation

First, download and install maven. After that, type the following at the command prompt:

This should print the current version of Maven.

2. Creating a project

Now you need to create a project. Create a directory and run the following command in the console:

You will see that a directory with the specified artifactId was created.
Inside the directory is the standard structure of the project maven.

The src/main/java  directory contains the source code for the project, src/test/java  project tests, and the pom.xml file is the Project Object Model of the project.

By the way, in addition to maven-archetype-quickstart there are other Maven Archetypes. For example, maven-archetype-j2ee-simple will create a simplifed sample J2EE application, and maven-archetype-webapp a sample Maven Webapp project.

3. Pom.xml

Pom.xml is a key project file that contains all the information you need to build a project.
Here is an example from our created project:

4. Build the project

To build the project, run:

The console displays a lot of information, and ends with the following lines:

We can run the project with a command

Which will print the famous:

 

3 Comments on “How to create a simple Java project with Maven”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.