DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Find out today at 11 am ET in our "DZone 2023 Preview: DevOps Edition!"
Last chance to join
  1. DZone
  2. Coding
  3. Java
  4. Beginners Guide to Creating a Project With Maven

Beginners Guide to Creating a Project With Maven

Want to learn more about how to get started with Maven? Check out this beginner's guide to creating a project in Maven!

Sovan Misra user avatar by
Sovan Misra
·
Sep. 13, 18 · Tutorial
Like (11)
Save
Tweet
Share
18.21K Views

Join the DZone community and get the full member experience.

Join For Free

Every application — regardless of small or big or huge — we have to follow basic procedures and cycles. Configuring those steps manually and repeating that cycle is a very cumbersome job. For example, dependencies management, pre-deployment validations, checks, etc.

Maven came into existence for the purpose of eliminating most of the manual efforts and automated this process.

What Is Maven?

While it is regarded as the "Build Tool," I believe that Maven is more than just “A Build Tool,” given its capabilities, but in this tutorial, we will stick to the basics of Maven.

  • Create a Maven Project
  • Understand Build Cycle, Build Phase, and Goals
  • Understand POM
  • Understand GroupID and ArtifactID
  • Parent POM Concept: Inheritance and Aggregation
  • Simple Maven Project

Create a Maven Project

This is quite easy, so I will save it for the last part of the article, while creating a simple project, but for now, let’s understand how it works!

Understand Build Cycle, Build Phase, and Goals

Build cycles, as I said before, consist of procedures that we have to follow a build process, and these are coined as a “Build Cycle." A cycle consists of one or more “Build Phase(s),” which runs sequentially, and in turn, each phase has been assigned with one or more goals.

For example: mvn clean dependency:copy-dependencies

A build cycle is defined and can be executed as a whole. It signifies a stage of the build, for example,

site –. This is a build cycle that is responsible for documentation.

clean –.This is a build cycle that is responsible for gracefully cleaning up the Maven directory where the compiled code resides.

default – is the default process that controls some of the base functionalities, like validating the dependencies and so on.

Each build cycle contains sequential phases. We can invoke a phase directly from a build cycle. The build phase runs in a sequential manner, so when we execute a build phase, the phase(s) before that phase are also executed prior to the execution. For example:

mvn install

When this above command is executed, all the phases before “install” are executed prior to that.

Now, let's talk about the goals. There are granular level commands present in each build phase. If the goal is associated with a single phase, then we can call it directly. Otherwise, we can use the phase name and then the goal name separated by:

mvn clean dependency:copy-dependencies

Understand POM (Project Object Model)

This is an XML file that contains the information required by Maven to create the builds, like Project Name, ArtifactID, GroupID, Version, Dependencies, etc. This also defines the different build cycle and phases for building the application. Basically, this contains the what and hows for the builds.

This file has to be kept in the root folder of the project and has to be named “pom.xml." A simple pom.xml would be as follows.

Maven

pom.xml derives some information from the super POM, which can be overridden in the pom.xml at the project level as well. Later, we will see how to inherit information from Parent POM to other POMs and also how to aggregate POM into parent POM.

Understand GroupID and ArtifactID

These tags {<groupId> , <artifactId>, and <version>} are like the address or unique identifier of the POM file. When we create a POM, we define:

  • Group ID as the organization web domain, or if it is a common project, we can add the project name as well.
  • Artifact ID is mostly the project name, and this is used by Maven for naming the Jar file or War file.
  • A version is a revision done on the POM/Project.

Parent POM Concept: Inheritance and Aggregation

As Maven encourages the DRY principle, the capability is available to inherit the common properties from a parent project to sub-projects without repeating the same kinds of stuff. This is known as POM inheritance. Let’s see how to do that:

MavanParent.PNG

Also, there is the scenario where there will be multiple sub-projects, and we might have to aggregate all sub-projects' pom.xml into a parent pom.xml.

MavanModel.PNGThe <packaging>pom</packaging> method means that the parent POM will be packaged as POM and would be used by reference only.

Hint: It would be very difficult to see from which POM the dependencies are pulled in, so to find the effective POM, we can use the following command.

mvn help:effective-pom

Before jumping into the code to create a simple Maven project with inheritance and aggregation, we will see one more concept in Maven — its folder structure.

DirectoryStructure.PNG

Simple Maven Project

In this simple project, I will create a Maven Project that will be a parent project for another sub-project in the parent project. I will add a common dependency for javax.mail. I will then create a new Maven project that will inherit the parent maven project’s pom.xml and have the parent (common) dependencies in it, too.

Parent pom.xml

Child

Child pom.xml — this is a child project has the mail.jar dependency and has inherited from the parent, without mentioning the same dependencies list in the child.

ChildWithDependancies


Hope this helps you understand how to build a project in Maven!

Apache Maven Build (game engine) Dependency

Published at DZone with permission of Sovan Misra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Do the Docker Client and Docker Servers Work?
  • Project Hygiene
  • Simulate Network Latency and Packet Drop In Linux
  • A Beginner's Guide to Back-End Development

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: