How to create and run a Maven project using command line

Maven is more than just a build tool. It’s a project management tool that encompasses a project object model ( POM ), a set of standards, a dependency management system, and many more. In this post, we will look at how we can create a simple maven project from the command line, and then we will run the same using the command line.

Run archetype:generate goal with the below command, which will create a new maven project for us.

mvn archetype:generate -DartifactId=DemoProject -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn generate command

Now, let’s see what happened here?

  • mvn is the Maven2 command, and archetype:generate is a Maven goal. The -Dname=value pairs are the arguments passed to the goal, similar to what we do while passing system property options to the JVM via the command line
  • archetype:generate helps create a project from an archetype that we have defined using -DarchetypeArtifactId=maven-archetype-quickstart. If we do not define the archetype using this argument, then it will prompt us to choose an archetype from a list of archetypes. Many archetypes are available in Maven almost for anything like creating a web application
  • Maven archetype plugin will create a folder or directly named DemoProject, the same as we passed in the artifactId argument in our command
  • -DinteractiveMode=false argument will disable the interactive mode. If we don’t use this argument, then the interactive mode will be enabled, and it will ask us to confirm the project settings before creating our project. The project will only be created after we give our confirmation
confirming properties in maven

The project has some default project structures shown in the screenshot below.

Maven default project struture

Note: In earlier versions, we could have used the archetype:create instead of archetype:generate, but now the archetype plugin has removed the support for the create goal.

Now. Let’s try to understand the project structure created using the command.

  • Every project will have a pom.xml( Project Object Model ) file. This file describes the project, declares dependencies, etc.
  • Java classes are placed under the src/main/java directory, and the resources files are placed under the src/main/resources directory.
  • The test cases are located in the src/test/java directory, and the resources for the test are placed under the src/test/resources directory.

Run the Maven project

We will now change the App class located in the src/main/java directory to print the message “Our first Maven program“.


public class App {
	public static void main(String[] args) {
		System.out.println("Our first maven program");
Build the maven project

We can quickly build our project using mvn install command from the directory containing the pom.xml file. The output of this command will be something as shown below.

building the maven project

This just created, compiled, tested, packaged, and installed the most straightforward maven project.

If you are getting an error after running “mvn install” command

You might be getting the below error after running the “mvn install” command.

[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
Error in compliation

To fix this, we must add the below properties in our pom.xml file.


So, your pom.xml will look like this now –

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Now, run the same “mvn install“, and the error shouldn’t come anymore.

Run the program

We can quickly run our program using the below command.

java -cp target/DemoProject-1.0-SNAPSHOT.jar

It will produce “Our first maven program” as the output on the command prompt screen.

Running the maven program

Related ArticleMaven build Lifecycle, Phases, goals, and Plugins

Github link for this article’s project

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at

Liked the article? Share this on

Leave a Comment

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