The Project Object Model and POM File

Matt Speake
A free video tutorial from Matt Speake
Java Technologist and Trainer
4.0 instructor rating • 8 courses • 11,947 students

Learn more from the full course

Mastering Apache Maven

Master Apache Maven from the ground up by practical demonstration

02:19:28 of on-demand video • Updated April 2020

  • Be able to create and build Java applications using Maven
  • Understand dependency resolution, how Maven resolves dependencies ad where to get dependencies for your own projects
  • Understand the build lifecycle and phases of Maven builds
  • Know how to use plugins to customize their builds with Maven
  • Feel confident using Maven at the commandline and within the IDE
  • Know how Maven works under the hood and feel confident to troubleshoot any problem builds
  • Have the knowledge to know where to look for further information to do more advanced types of builds
English [Auto] In this video we're going to talk about the Maven Pom or projects object model. So let's just open up a sample project and have a look at what's in the pond. So here's a sample project opened up so all maven projects have a Project Object Model and this describes the view of the project as maven would like to see it. So by this we mean it includes configuration and properties and build customizations which customize the build for a given project. So we just have a look at this project and see what properties we have in this is a project which was created with the MAVEN quick star archetype as a default project but it's a good place to get started. So first off we have a model version. So this is the version of the Pom that's used in general this hasn't changed between maven versions though. Either way it's generated by the archetype anyway so you don't really have to worry about the next we have three very important properties in Melbourne which are known as the gamma coordinates which collectively stand for group Heidi artefact Tony and virgin. So we can see these three properties the group Heidi is basically kind of a namespace for where this particular project will live kind of synonymous with the idea of a job package. So it's a way of being able to group and aggregate different projects together under the same location which corresponds to the same section in a Melbourne repository. Next up we have the artifact I.D.. This is the name of the artefact that will be produced by that we mean the end build artefacts that's produced as the output of the MAVEN build. So this is the best name for the artefact. Next we have the version which in this case is 1 0 snapshot. So these three properties taken together the group Heidi artefacts holy conversion specify the location that this artefacts will be located in when it's deployed to a maven repository. This is either an external repository such as one of the many public open source ones there are like maven central for example which is the main default repository for maven artefacts or you won't be allowed to publish here unless you have a proper open source project or it could be an internal repository which is normally a repository which is part of a corporate infrastructure. So here we're talking about tools like Nexus or arty factory which house all the Maven artefacts across an organisation and third party dependencies as well in most cases or the local maven repo cache which is actually on your system. Just quickly demonstrate how the group Heidi artefacts only inversion map to the location of the repository by going to the command line and building and installing the artefact into the local repo cache so let's just do that now so let's just quickly catch this complex email file. And so this project should be located in a group idea which is condo's engine not training that demo. This means that in the local maven repo cache which is located here we'd expect to see a folder structure like this comms engine training demo and we don't have that. So because we don't have that it means it's not installed on this system currently. So if we quickly install this will compile the source code makes the jar file and then install it into the local repository then we should see that it's populated that particular directory and you can see here on the bottom where it's installed two items we see my up 1.0 snapshot the jar and we also see the pom file which is deployed as well. So this means now if we re execute that command we will see the artefact there. So the first part of the path after M2 repository is a group idea that's condescension the training demo that under there we have a sub directory which is named of the artefact tidy which is my. And if we go into my app there was a 1 0 snapshot. So under the artifact tidy directory we see the versions which are available there's only one version here because it's the version we just installed. And then finally inside that version folder we have the actual artifact itself which is my 1.0 snapshot of char. You'll notice as well it's also deployed in my app 1.0 snapshot Pom. So let's just quickly take a look at that and you should recognize this because this is actually the pond excel file which was in the root of the project. So whenever you do an install with MAVEN to install to the local Maven Repository cache or you deploy to deploy to an external repository it also pushes along the pond X and all file such that when this particular artifact is used as a dependency in other projects never knows which are the dependencies it needs to download as well and a few other aspects to. So we see that the group idea artifacts had a inversion this so-called and it is used both in the publishing or installing of an artifact into a maven repository to define its location. And the gaff coordinate can also be used to use an artifact as a dependency in the dependencies section of the pond ex email file too. So let's just go back to the popular excel file and continue reviewing this. So whenever you create a maven project at the very least you have a complex email file which is at the root of the project and this needs to include at least the first five properties the model version through party artifacts any inversion and the packaging type. And this constitutes a so-called minimal Pom. In other words it's the absolute bare minimum that you need in maven to be able to describe a project you can add other sections as well as we can see by looking at this page on the Maven website so this page in the Melbourne Web site gives you an overview of the MAVEN Pom structure for the project object model. If we scroll down the section about what is the problem but just underneath say we have a description of what else is in here so we can see in the first section we have the basics which are the group hiding artifacts are the version we saw before the gaff coordinates along with the packaging type which can be jar or war. This determines what actual type of build artifacts you want to generate. So a war packaging type to generate a war file which can be deployed into a web container a jar packaging type to create a jar file which can be used as a dependency. Then in other projects or maybe as a standalone executable jar file for example and there's some other bits and pieces there as well related to more advanced use cases of maven relating to when you create multi module builds and how you manage dependencies within there. We also see in the dependency section this is where you specify the dependencies that the build should use and then underneath you have the build settings so that when we're concerned about in this course is the build tag and it's inside the build tank that you actually define customizations of the current build. So let's just dig into that little bit further so the build section within the pom file can house a few different elements but in this cause for concern with the plugins section this is where you define plugins you're going to use within the build or provide configuration and customize existing plugins which are being used in the default build. We'll see more about that later on when we look at build life cycles but feel free to have a look at this particular section which describes the pom to dig in for a few more details the port we've covered here should give us enough of an idea of what the pump does and what it is. In this introductory course.