≡ Menu
Apache Maven Tutorial

Apache Maven Tutorial

Apache Maven Tutorial – Maven Archetypes & Maven POM

Remember Sharing is caring! So if you get something from this Apache Maven tutorial please share it with others on facebookgoogle+, LinkedIn, Twitter  etc… so it may help them too! :)

In this Apache Maven tutorial, I want to take a little time to walk through some of the key concepts surrounding Maven Archetypes and the Maven POM. In future articles I’ll build upon these concepts and demonstrate the power of this great build automation tool.

Apache Maven, can be seen as a core framework for a collection of components and plugins. When you download and install Maven onto your local machine the Maven plugins are not included. This helps keep the initial download size minimal and the Maven plugins will be downloaded later during your first application build. Maven plugins are downloaded via the internet from ‘Maven Central’, so an internet connection is necessary.

Maven plugins are the key to Maven and how it works. It’s the Maven plugins that do the build work for you. Once downloaded, they’re cached in a repository held locally on your machine.

So the very first time you run a build you will see a lot of download activity going on, these downloads will become fewer in future builds, once the plugins and dependencies are downloaded they’re cached in your local maven repository.

Some of the terms you will come across very early on in using Maven are;

Archetypes – An archetype is like a template and quite a few come with maven at install time. They provide us with a convenient way to create a new project.

For example, the ‘maven-archetype-quickstart’ is a useful one to begin with and gets you up and running quickly. It provides a template for a very basic Java application; the result of running the ‘maven-archetype-quickstart’ should look familiar as it creates the traditional ‘Hello world’ application. It severs a purpose to give you the standard Maven directory layout for a java project and also provides a default pom.xml file.

So, what is a pom.xml file ?

Maven POM or Maven Project Object Model is at the heart of all Maven configurations.

It’s an XML representation of a Maven project and is held in a file named ‘pom.xml’.

There’s a single pom.xml for each Maven project and in the world of Maven, a project doesn’t actually need to contain any code but it does require a pom.xml file.

What does the Maven POM contain ?

It contains up-to 4 categories;

1) General Project Information; i.e. Project name, URL, developer names and contributors, License details.

2) Build settings; Customised Maven life cycles, new plugin and goal details.

3) Build Environment details; profiles are used to configure different deployment environments, i.e. development, test, production.

4) Maven POM relationships, modules and sub-modules.

The minimum XML elements a pom.xml file can contain;

  • Project 
  • modelVersion
  • groupId
  • artifactId
  • version

If you create a new Maven project using the ‘maven-archetype-quickstart’, you will see XML containing the above XML elements.

By running the Maven build process you’ll see the various phases of the lifecycles run and even a jar file will be produced, all without configuration, except; Project, modelVersion, groupId, artifactId, version.

The outer element “Project” is mandatory as everything else is contained within it.

Note the namespace is for the maven version 4 schema. The element “modelVersion”, at the time of writing is version 4.0.0  is also mandatory.

The group of three elements below; groupId + artifactId + version are known as the Maven Coordinates and are also mandatory.

groupId – will identify your project uniquely across all projects and generally follows the reverse domain name of your organisation. It follows the same naming schema as the Java package. i.e. It has to follow the package name rules.

artifactId – is the name of the jar, war or ear without version. If you created it then you can choose whatever name you want with lowercase letters and no strange symbols. Note: You could have a program that is made up of many jar files, depending on how you design you program. I see the artifact at the component level as a program can be built from 1 or many components.

So, the groupId, artifactId together identify the components location within the maven repository, as that’s where they are installed during the build process.

The version is an identifier to uniquely identify the version of a component.

If you would like to know more about installing Apache Maven check out my free youtube tutorials and even my Apache Maven course where all this covered in great detail and downloadable source is also available.

Remember Sharing is caring! So if you get something from this Apache Maven tutorial please share it with others on facebookgoogle+, LinkedIn, Twitter  etc… so it may help them too! :)

Thanks for reading,

Bruce

in Apache Maven Tutorials, Blog from the Den

About the author: Bruce is a techie, a gadget geek, programmer, mentor and all round technology nut. He holds two degrees in computer science, with over 25 years in the software business. Currently working freelance as a software engineer and programming mentor. He loves what technology can do for us. Building applications using functional, Object Orientated languages & polyglot persistence helps him reconnect with the feeling of building something tangible. To learn more about Bruce, follow him on Twitter @denofprogram

{ 0 comments… add one }

Leave a Comment