Maven - Installation and Getting Started
05 May 2014What is Maven?
From their page:
“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) -
“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.
Go to your System properties page
-> Advanced system settings
-> Advanced
-> Environment Variables
Add the following variables:
-
JAVA_HOME -> Point to your Java installation directory e.g
C:\Program Files\Java\jdk1.7.0_51
-
MAVEN_HOME -> Point to your extracted Maven directory e.g
C:\Users\Name\Java\apache-maven-3.2.1
-
PATH -> Locate the existing
PATH
variable andEdit
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:
mvn -version
You should see some output that looks something like this:
> C:\>mvn -version
> Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:5
> 2+00:00)
> Maven home: C:\Users\Name\Java\apache-maven-3.2.1\bin\..
> Java version: 1.7.0_51, vendor: Oracle Corporation
> Java home: C:\Program Files\Java\jdk1.7.0_51\jre
> Default locale: en_GB, platform encoding: Cp1252
> OS name: "windows 8", version: "6.2", arch: "amd64", family: "windows"
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 that’s it. You are ready to use Maven in your projects!
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.
Read More