Welcome to a new series of posts where we'll explore the Apache Maven build tool and how to make the most of it. Let's begin with the basics.
The goal of a build tool such as Apache Maven is to apply a series of transformations unto source code to turn it into a deliverable artifact; the most obvious transformation for Java sources is compilation into Java byte code for example. Transformations may be applied one after the next, with intermediate results combined by other transformations, such as pre- and post-processing of resources files, generation of source code via annotation processors, generation of metadata in various formats (typically but not only limited to XML or JSON), to later be packaged as single JAR file.
As the name implies, Apache Maven is a project harbored by the Apache Software Foundation and as such it does not come bundled with a JDK distribution; you must install Apache Maven in a way that's compatible for your platform. There are many ways to make this work, the typical way is to download and install Apache Maven yourself, however you can also use popular package formats (.rpm, .deb) and package managers (rpm, yum, apt-get, brew) to achieve this goal. My personal choice is to use SDKMAN! as it works in all major platforms.
Assuming you've successfully installed Apache Maven on your system the next step is to create a project. You also have several choices to make this happen, such as using your IDE of choice or a project bootstrapping tool such as Apache Maven archetypes (to be discussed at a later date), JBoss Forge, Lazybones, or other. To keep things simple I'll assume a simple text editor and the command line are the only tools we'll need for the time being. Open up a command prompt and execute the following commands (works on Linux and MacOSX, for Windows you'll have to find compatible commands).
The last command creates and empty file named pom.xml. This file contains the instructions that Apache Maven follows in order to apply transformations to the source code. It's name is derived from Project Object Model, you can find more information about the format itself at this link. For now we'll add the minimum required content to this file, mainly what are known as the GAV (Group, Artifact, Version) coordinates for this project however bear in mind that these GAV coordinates are important as they become part of the identity of the generated artifacts, also you must follow a series of rules for picking GAV coordinates if you intend to publish artifacts to the Central Repository. We'll cover repositories in a follow up post. These are the contents of our pom.xml file:
Apache Maven is configured by default to build Java projects, and it does so by providing a lot of behavior out of the box that relies on conventions, this lets you define as little configuration as needed in the pom.xml file. The default conventions for Java projects is to place source code under the src/main/java folder, thus adding a class such as com.acme.Sample shown in the following screenshot
Means that the following file structure must be put in place
All that's left for now is to invoke Apache Maven and let it build our project. Apache Maven relies on commands to perform its duties. Commands are intrinsically related to other concepts such as lifecycles, phases, and goals which we'll cover later, but for now the command we should invoke to build the project is mvn verify. Invoking this command on the command prompt should result in an output similar to this one
Notice that Apache Maven indicates the steps it executes along the way to fulfill its duty; these steps are the actual transformations performed on the source code, of which there's a single file at the moment. Apache Maven places all intermediate and final files under a particular folder based on conventions once again. Here's how the file structure looks like after the project has been built
Notice that the compiled classes can be found under target/classes, as well as the final package whose name matches the GAV coordinates we specified in the POM file. You'll find the compiled class inside the JAR file alongside other metadata files that Apache Maven adds.
Well this conclude the first post in this series. We barely scratched the surface on what Apache Maven can do for you. In the next post we'll cover one of the most well known features that Apache Maven provides: dependency resolution.