Skip to main content

Maven Project

Your First Maven Project

Introduction

Now that you understand Maven’s installation and project structure, it’s time to create your first Maven project.
In this lesson, you’ll learn how to:

  • Create a Maven project using archetypes
  • Use the mvn archetype:generate command
  • Open your Maven project in IntelliJ IDEA or Eclipse

By the end of this lesson, you will have a fully functional Maven project ready for development.

1. What is a Maven Archetype?

An archetype in Maven is essentially a project template that provides a predefined structure and configuration.
Maven comes with a set of official archetypes (e.g., maven-archetype-quickstart) and also allows you to create your own.

Benefits of using archetypes:

  • Saves time by generating boilerplate code and directory structure
  • Ensures consistency across multiple projects
  • Reduces setup errors for new projects

2. Creating a Maven Project using mvn archetype:generate

The most common way to create a Maven project is by using the Quickstart archetype.

Step 1 – Open Terminal/Command Prompt

Navigate to the folder where you want to create the project.

Step 2 – Run the Command

mvn archetype:generate \
    -DgroupId=com.example \
    -DartifactId=my-first-maven-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.4 \
    -DinteractiveMode=false

Explanation of parameters:

  • -DgroupId → Your project’s unique identifier (reverse domain name style)
  • -DartifactId → Name of the project (will be the final JAR/WAR name)
  • -DarchetypeArtifactId → The archetype you want to use (maven-archetype-quickstart creates a basic Java app)
  • -DarchetypeVersion → Version of the archetype
  • -DinteractiveMode=false → Skips prompts and uses provided values

3. Verifying the Generated Project

After running the command, Maven generates a structure like:

my-first-maven-app/
│
├── pom.xml
└── src/
    ├── main/
    │   └── java/
    │       └── com/example/App.java
    └── test/
        └── java/
            └── com/example/AppTest.java

Key points:

  • App.java → Sample Java program
  • AppTest.java → Sample unit test
  • pom.xml → Project configuration file

4. Opening the Project in IntelliJ IDEA

Step 1 – Launch IntelliJ IDEA

  • Click File → Open
  • Select the project’s root folder (my-first-maven-app)

Step 2 – Auto Import Maven Project

  • IntelliJ will detect pom.xml and ask to Import Maven Project – click Yes.
  • If prompted, enable Auto-Import so IntelliJ keeps dependencies in sync.

5. Opening the Project in Eclipse

Step 1 – Launch Eclipse

  • Click File → Import → Existing Maven Projects
  • Browse to the folder containing pom.xml

Step 2 – Finish Import

  • Eclipse will read the pom.xml and set up your Maven project automatically.

6. Running the Project

To run the sample application from the terminal:

cd my-first-maven-app
mvn clean install
java -cp target/my-first-maven-app-1.0-SNAPSHOT.jar com.example.App

To run tests: mvn test

Troubleshooting Common Issues

  • Command not found: Make sure Maven is installed and mvn -v works.
  • JDK version mismatch: Update pom.xml with the correct <maven.compiler.source> and <maven.compiler.target> values.
  • IDE not detecting Maven: Ensure Maven integration/plugin is enabled in your IDE.

Key Takeaways

  • Maven archetypes provide a quick and consistent way to start new projects.
  • The maven-archetype-quickstart is perfect for simple Java applications.
  • IntelliJ IDEA and Eclipse have built-in Maven support, making project import seamless.