Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Beginners Guide to Creating a Project With Maven

DZone's Guide to

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!

· Java Zone ·
Free Resource

Automist automates your software deliver experience. It's how modern teams deliver modern software.

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!

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,maven ,build tool ,spring ,artifact

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}