DEV Community

Cover image for Building with Maven part 2

Building with Maven part 2

Tristan Elliott
I am not a cat
Updated on ・5 min read


  • This is the second part of my series where we walk through an online book hosted by TheNexus on how to to use Maven. Before we get started please make sure that you have Maven installed on your machine. From this point on, I will assume that you have installed Maven. I also have a YouTube version of this blog post.

Creating our simple project

  • to create our simple project we are going to run the command,

mvn archetype:generate -DgroupId=org.sonatype.mavenbook -DartifactId=simple -Dpackage=org.sonatype.mavenbook -Dversion=1.0-SNAPSHOT

  • you can just copy and paste that command into your terminal but before you do that lets walk through what that command actually does.
  • archetype is what is called a plugin and generate is what we call a goal. A plugin tells maven to do an action and a goal is a way to customize that action.

  • In Maven an archetype is a defined as being a template for our project. By using a predefined archetype(template) provided to us by Maven we can quickly and smoothly set up a software project. This means that our project will have a well defined folder structure and pom.xml file, this is a prime example of Maven's convention over configuration. Now that we have a better understanding on what archetype is doing lets get a better understanding of what generate is doing. generate is a goal that tells maven that we want to create a project. What is a goal? Well in Maven a goal represents a specific task that contributes to the building and managing of a project. If the word goal is still a little confusing for you, you can call it a task instead. For me this really helped my brain understand what Maven was doing.

What is a plugin?

  • Well at the core of Maven is a collection of plugins, in other words, plugins do all of the work. Plugins give us the ability to create JAR/WAR files, compile code, unit test code and much more. Plugins are the work horse of Maven, it is thanks to them that we can do all of the cool stuff

Introduction to the build lifecycle

  • If you are following allow with the book then you will notice this part is not in chapter 3. That is because this information comes straight out of the documentation of Maven. I highly recommend that you read the documentation yourself. It is very thorough and gives a complete introduction to the build lifecycle. With that said let us continue.

-Maven is based on a concept of a build lifecycle. What this means is that the process of building and distributing an artifact(project) are well defined. So us developers only need to learn a few basic commands and Maven will do the rest

  • In Maven there are 3 built-in lifecycles, default, clean and site.

  • 1)default lifecycle handles the project's deployment

  • 2)clean lifecycle handles the project's cleaning, which involves removing files

  • 3)site lifecycle handles the creation of a project's documentation

  • Now each of these lifecycles are made up of build phases and when one of these lifecycles are triggered the build phases are called in sequential orders.

  • The default lifecycle consists of the following build phases:

  • validate: validates all the necessary information is provided.

  • compile: compiles the source code

  • test: test the compiled using a suitable framework

  • package: takes the compiled code and packages it into a JAR, WAR or any executable file

  • verify: ensure that the test quality was met

  • install: installs the package into the local repository

  • deploy: completes the build lifecycle, code is now ready for deployment.

A build phase is made up of plugin goals

  • build phases are steps inside of a lifecycle and we can add plugins into the phases to alter the build logic. A plugin goal represents a specific task and it may be bound to zero or more build phases. A plugin goal not bound to any specific build phase may be invoked through direct invocation.
  • if a build phase has no plugin goals bound to it, it will not be executed
  • this was just the basics of the build lifecycle, so please go and read the documentation. Now lets get back to building our simple application.

Simple application

  • copy and paste the following into your command line

mvn archetype:generate -DgroupId=org.sonatype.mavenbook -DartifactId=simple -Dpackage=org.sonatype.mavenbook -Dversion=1.0-SNAPSHOT*

  • as we stated earlier, archetype:generate is just telling Maven that we want to blank maven project
  • -D tells Maven that we want to define something. In this case we are defining the groupId, the artifactId, package and the version. These make up the coordinates of our project.

  • after you hit enter you will be asked a series of prompts, since we just want the basic configuration do not enter any numbers and just hit enter. Then confirm the information that you have provided and you now have created a simple maven project.

Open in your favourite IDE

  • Now that we have created a project, go ahead and open it inside your favourite Java IDE and observe the structure.

  • Observe that Maven created a directory called "simple", this is the same as the our artifactId that we defined earlier. This simple file is now know as the base directory

  • Notice that we now have a POM.xml file, every Maven project has this and it is called the Project Object Model. This is a very important file to us, because inside this project we can configure plugins and declare dependencies

  • Also look at the structure of the files, notice how there is a specific folder for the source code and a specific folder for the test code. This is another clear demonstration of convention over configuration.

  • The last thing that we are going to do is cd in the simple folder and run the command mvn install

  • Running that command will trigger Maven to run the default lifecycle. This will run all the build phases up to and including install in a sequential order. If you look at the command prompt you should be able to to see when these build phases get called. A list of all the build phases in the default lifecycle are defined here. Long story short mvn install simply takes all the information from the pom.xml file, builds and then installs the project in our local repositories. With that you should have successfully installed and created a Maven project.


  • I want to mention a few things before I end this blog post
  • 1) the next post will be chapter 4 of the Nexus book, where we will be creating and then modifying a more complex Maven project.
  • 2) I will also be posting a YouTube video version of this blog post, so make sure to check that out as well.
  • 3) Finally, thank you for taking time out of your day to read this blog post of mine. I hope we can both become better developers because of it and if you have any questions or concerns please comment below or reach out to me on Twitter.

Discussion (0)