Creating a Maven Project: Your Step-by-Step Guide

Ever wondered how to kickstart a new project with Maven? Maybe you’ve heard of archetypes but are unsure how to use them? Don’t worry, I’ve got you covered! In this post, we’ll walk through the process of setting up a new Maven project, exploring the power of archetypes along the way. Let’s dive in!

Step 1: Verify Your Maven Installation

First things first, we need to confirm that Maven is installed correctly. Open a terminal window and type the following command:

mvn -v

You should see details of the installed Maven version along with the Java version it’s using. If not, you’ll need to install Maven.

Step 2: Generate a Maven Project Using an Archetype

Maven’s archetypes are essentially project templates that give you a headstart. To create a simple Java project, we’ll use the maven-archetype-quickstart archetype.

In your terminal, navigate to the directory where you want your project to reside. Then, type the following command:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Let’s break this down:

  • mvn archetype:generate is the command to generate a new Maven project from an archetype.
  • -DgroupId=com.mycompany.app specifies the group ID, which usually represents your organization or project.
  • -DartifactId=my-app sets the artifact ID, essentially the name of your project.
  • -DarchetypeArtifactId=maven-archetype-quickstart tells Maven to use the “quickstart” archetype.
  • -DinteractiveMode=false disables interactive mode, meaning Maven won’t prompt you for input during project creation.

Once the command finishes executing, you’ll have a new Maven project in a directory named my-app.

Step 3: Understanding the Project Structure

Navigate to your new my-app directory, and you’ll see the following structure:

my-app
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── mycompany
    │               └── app
    │                   └── App.java
    └── test
        └── java
            └── com
                └── mycompany
                    └── app
                        └── AppTest.java

The pom.xml file is your project’s core. It defines your project structure, dependencies, plugins, and many other project-specific details. The src/main/java directory houses your application’s source code. In contrast, src/test/java is where your test code lives.

Step 4: Build and Run the Project

To build your project, navigate to the my-app directory in your terminal and execute:

mvn package

This command compiles your code, runs any tests, and packages the result into a JAR file within the target directory.

You can now run your project using the following command:

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

And voila! You’ve successfully created, built, and run a Maven project!

In Summary

Maven makes it incredibly easy to kickstart a new project, thanks to its archetype system. Now that you’re familiar with setting up a basic Maven project, you can further explore Maven’s comprehensive documentation and experiment with different archetypes to suit your needs. Enjoy building with Maven!

📚 Further Reading & Related Topics

If you’re exploring creating a Maven project, these related articles will provide deeper insights:

• Mastering Dependency Management with Maven – Learn how to handle dependencies effectively in your Maven projects and optimize your builds.

• Navigating Java Version Upgrades for Your Spring Boot Application – Discover how to ensure your Maven projects are configured for smooth Java version upgrades in Spring Boot applications.

Leave a comment

I’m Sean

Welcome to the Scalable Human blog. Just a software engineer writing about algo trading, AI, and books. I learn in public, use AI tools extensively, and share what works. Educational purposes only – not financial advice.

Let’s connect