How To Create Maven Project Using CLI

Introduction

Here I will show you how to create maven project using CLI. CLI means command line interface, so you need to use command line tool (cmd prompt) in Windows or Shell terminal in Unix based system.

You can use any IDE (for example, Eclipse) or tool to create a new maven project. During project creation you need to mention Group Id, Artifact Id, etc.

Group Id determines your related projects under a particular group, for example, you may mention your organization name. For example, if your organization’s name is company.com then you can mention Group Id as com.company.

Artifact Id signifies your project’s name. For example, the name of the project might be spring-boot-rest-api-crud-example.

You might be more comfortable to create maven project using CLI than using any IDE or tool. Or you might be facing some issues while creating maven-based project suing tool or IDE. The issue might be Resolving Archetype for indefinite time, Error occurred during resolving archetypes, etc.

Prerequisites

Java 1.8+, Maven 3.6.3+

Create Maven Project Using Command Line Tool

Now I will use the following command to create maven project. Note that the following command is executed on the directory, from command line tool, where I want to keep my project directory.

mvn archetype:generate -DgroupId=com.roytuts -DartifactId= spring-boot-rest-api-crud-example -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

To create a new project based on an Archetype, you need to call mvn archetype:generate goal, i.e., mvn archetype:generate.

groupId is used to specify the Group Id and artifactId is used to specify Artifact Id.

Using archetypeArtifactId, you are informing maven what archetype to use to create the initial structure of the project. Maven looks it up from the archetypeCatalog. If you want to create web application then you need to mention -DarchetypeArtifactId=maven-archetype-webapp instead.

archetypeVersion is the value that goes into version when you are creating your project.

I have specified interactiveMode=false, so project structure will be created in batch mode and maven will not prompt for any answer from you. Maven will assume default answers for the questions. In interactive mode maven will prompt for answers from you and you need to input through your keyboard. The following output will be produced in the command line tool. Ideally you will see that there are many plugins being downloaded when you create project first time using maven CLI.

create maven project using command line tool

You will find that standard project structure will be created:

Now you can open or import this new project into any IDE or tool or even using notepad.

That’s all about to create a new maven project using CLI.

Leave a Reply

Your email address will not be published. Required fields are marked *