Creating a Java Project using Maven Archetypes With Examples

There are many ways to create a maven project. We can use built-in plugins from the popular IDEs such as Eclipse and IntelliJ IDEA. We can also create a maven project from the command line. In this tutorial, we will learn how to create a simple Java project using Maven archetypes through the command line.

Maven Command to Create a Java Project

First of all, create a directory where you want to create the java project.

Then, run the following command to create a simple java project.

If you look at the output, the maven is confirming project configurations. We can skip this using the -DinteractiveMode=false argument.

Now, let’s try to understand what is happening behind the scene of this command.

  1. Archetype is a maven project templating toolkit. It’s a great way to create maven projects from the command line. The archetype:generate tells maven that we have to generate a maven project from the additional information provided in the command.
  2. Every maven project requires some specific configurations, such as groupId, artifactId, packaging, version, and name. The specifies the groupId of the project we are creating.
  3. Similarly, -DartifactId=java-app specify the artifactId of the project.
  4. The -DarchetypeArtifactId=maven-archetype-quickstart specify the template to use for the project. The maven-archetype-quickstart artifact generates a simple JAR based maven project. The version of the project is set to 1.0-SNAPSHOT.

Directory Structure of the Maven Java Project

We can use tree command in Linux systems to get the directory hierarchy.

Notice that the maven archetype has created a simple Java class and a JUnit test case for it. We can extend this project to add more functionalities to it. The class has the following code.

It has a main method, so we can execute this class after building the JAR from the maven project.

Building the Maven Project

Let’s try to build the maven project using the mvn package command.

The maven build failed because it’s using an older version of maven-compiler-plugin that doesn’t support the latest version.

We can fix this issue by adding below configurations to the pom.xml file.

This tells maven to use maven-compiler-plugin version 3.8.1 and Java 13 release to compile the project.

Now, let’s build the project once again.

First of all, all the required JARs are downloaded from the maven central repository.

Then, the project code is compiled and test cases are executed.

Finally, the JAR is built and its location is printed with the BUILD SUCCESS message.

If you want to skip the test cases from executing, pass -Dmaven.test.skip=true argument to the maven.

Running the Maven Project JAR

Let’s run the App class from the project JAR file.

We can also set the Main-Class parameter in the JAR manifest file. If you unpack the JAR file, you will notice that there is a file META-INF/MANIFEST.MF. This file contains the project information and it’s auto-generated by maven.

We can add the Main-Class parameter to it by adding the following plugin configuration in the pom.xml file.

If you build the maven project with this configuration, there will be a new configuration in the manifest file for the main class.

Now, we can run the JAR file using java -jar command and it will look for the Main-Class parameter to run the class.


We learned how to create a maven project using archetypes. It’s a simple and effective way to create a starter project.


By admin

Leave a Reply

%d bloggers like this: