"a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project."

Essentially Maven makes it much easier for developers to manage their Java projects. Where it really shines is with the build process (particularly when handling dependencies, but we'll get onto that in a later tutorial) -

Quote

"Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system. Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects."

Sounds pretty cool huh? In addition, it turns out that Maven is really easy to install and get started with using on all your Java projects.

Installation -

First step is to download the current Maven build from their website http://maven.apache.org/download.html. Pick whichever archive format you prefer and download and extract the most recent release to somewhere on your machine.

Windows -

Now if you are on Windows, all we have to do is set up some environment variables and we are good to go.

PATH -> Locate the existing PATH variable and Edit it. At the end add the this: ";%MAVEN_HOME%\bin;" (make sure all the entries are separated by one semi-colon).

That's all you need to do. You should now be ready to get started with using Maven.

Unix -

Run the command sudo apt-get install maven and you should be good to go straight away! Skip to the verify your installation stage to make sure you are good to go.

Verifying your installation -

Once you have followed the installation steps depending on on your operating system, open up a command line/terminal window and enter the following command to make sure everything is working correctly:

If not then you may need to try the above steps again or search online for solutions.

If on the other hand you did get output that looked like the above, then you are ready to get started with using Maven!

Creating a Project

To create a project with Maven, you make use of its archetype mechanism. This is basically a large collection of pre-made project templates that you can base your project from. There are a ton of different types of archetypes available so you there should already be one that matches your requirements. In this tutorial we will be using a quickstart archetype which is one of the simpler models of a basic Java application.

To construct this project first open up a terminal or command prompt window and navigate to the location on your machine where you want your project to be stored. In this example I am using a 'Projects' folder sitting on the root C: drive directory.

Once there issue the command:

mvn archetype:generate

This will first of all download a few things and then display a massive list of available archetypes you can base your project from.

As you can see there are quite a few. We are prompted by Maven to input the index of the archetype we wish to use. In this tutorial we are using a simple quickstart archetype with the index 383. Handily Maven enters this by default so we can just hit return at this point.

We are then prompted by Maven to choose which version of the archetype to use:

In this case we want to use the latest version (1.1 in this case) so we choose option 6. Again Maven fills this in by default so we can hit return again.

We are then prompted by Maven to supply a 'groupId'. From the docs:

Quote

groupId - This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.

The groupId is essentially the same thing as what you would normally use as a root package for your application. In this example we will use 'net.dreamincode.app'.

We are then asked to give an artifactId. Again from the docs:

Quote

artifactId - This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).

This property is essentially just the name of your project. Simple enough. In this example I will use 'MavenTutorial'.

We are then prompted to give a version. Maven includes some good version management tools, but we won't cover that in this tutorial. The general convention is to use SNAPSHOT when the project is still in development, and to then switch to a solid version number when deployed. For now we will use the default value of 1.0-SNAPSHOT. This can of course be changed later on in the development cycle.

Finally Maven wants a package name from us. This should be pretty familiar to any Java programmer. By default Maven gives us the groupId value we previously entered (net.dreamincode.app) which is fine for us.

Maven then gives as a summary of the project properties we just entered:

Type 'Y' to accept these properties and get Maven to create the project. After a short while you should see a 'BUILD SUCCESS' message if the project was created successfully.

Analysing what we just created

Once created you should notice that a new folder has been created at your location with the same name as the artifactId you supplied earlier. 'cd' into that directory and have a look around. It should have this rough structure which follows Maven's Standard Directory Layout:

Maven has also created the 'pom.xml' (Project Object Model) file. This is an essential file in Maven and basically contains every important piece of information about your project. It is a single configuration file that Maven uses to build your application. This is the core component of any Maven project and is essential to come familiar with in order to make the most out of Maven.

Most of this should be pretty self-explanatory. At the root we have the project node which will be the base of all pom.xml files. Under that we have the version of the object model Maven is using (shouldn't need to change this). Then we have the groupId, artifactId and version just as we entered them when we created the project in the command line.

Next however we have the packaging type. We will cover this more in the next tutorial, but it basically tells Maven what type of file to create when we 'package' the project. This is a .jar file for this simple Java application, but may be a .war file for example in web applications.

Following the packaging tag we have the name and url of the project. The name is the display name used for the project and the url is indicates where the website of the project can be found. Both of which are used when Maven generates documentation for us (we will again see this in action in a later tutorial).

We then have a property stating the encoding of a source files, and finally a set of dependencies for our project. This is where Maven really shines. Instead of having to manually download an external library (and all of its dependencies!), we can simply add it as a dependency in the pom.xml file, and Maven will automatically download that library and all needed dependencies it needs to operate. Not only that but it will also configure the build path to add these libraries which cuts out a lot of potential hassle for us developers.

We will cover dependencies more in the next tutorial, but for now just know that at the moment we have one dependency (jUnit that gets used in the test source tree).

Compiling our Project

Now, at this point to compile our application we could use the javac command and play around with the build path to handle jUnit (and any other dependencies we may have). But we don't want to do that now do we?

Handily Maven will take care of all that for us, after all it is a build tool. To compile our project simply make sure you are in the main project directory (the one with pom.xml) in it, and issue the command:

mvn compile

If this is the first time you run the command, then this may take a while to complete. Maven is essentially downloading all of the dependencies and plugins it needs to compile your project (there are quite a few to get through). The good news is, once you've downloaded them once, they will remain in your local repository, so the command will take much less time in the future.

A new folder 'target' has also been created which houses all of the .class files for your project. To make sure it works we will run the project (this archetype gives us a 'Hello World' example to execute):

Replies To: Maven Tutorial 1 - Installation and Getting Started

Run the command sudo apt-get install maven and you should be good to go straight away!

This command is for Debian based distributions of Linux only. If you're using another Linux distribution, it's of course advisable to see if Maven is available via the package management system. If it isn't, then download one of the binary packages on the download page Ryano121 links to and then it's again just a matter of setting up environment variables. See the bottom of the download page for details.