Apache Maven

Last Updated on

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:

First, it describes how software is built, and second, it describes its dependencies. Contrary to preceding tools like Apache Ant it uses conventions for the build procedure, and only exceptions need to be written down.

An XML file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins.

It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging.

Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache.

This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated. More

Maven Installation

To begin with, download the latest Maven from http://maven.apache.org/download.html.
Apache Maven

I downloaded apache-maven-3.2.1-bin.zip and extracted it to C:apacheapache-maven-3.2.1

Apache Maven

Maven Configuration

Now Need to setting Environment Variables

Go to your System Properties → Environment Variables.

Create a JAVA_HOME System Variable and point it to your Java installation. I pointed mine to C:Javajdk1.7.0_45.

Apache Maven

Create an M2_HOME System Variable and point it to your Maven installation.

I pointed mine to C:apacheapache-maven-3.2.1.

Apache Maven

You should now have JAVA_HOME and M2_HOME System Variables.

Add %JAVA_HOME%bin;%M2_HOME%bin; to your Path System Variable. This puts your Java and Maven executable in the System Path so that they can be executed without their fully qualified paths.

Apache Maven

Open a command prompt window, and at the command prompt, type

 mvn -version

You should see a message displaying the version of MavenSW and the version of JavaSW.

Apache Maven

Next, at the command prompt, try typing a command

mvn clean

You’ll probably see that some jarW files downloaded from the Maven central repository to your machine. After that, we see a ‘BUILD ERROR’. This message is fine.

This is telling us that we ran the command but that there was no pom.xml file present in the directory where we ran the command.

After running the mvn clean, if we go to our user home directory ( for me, C:Usersaamsuzon.m2 ), we can see that an .m2 directory has been created by mavenSW. Within .m2, we can see a repository directory. This is the default location for your local maven repository.

This local repository directory contains things such as the jar files that your projects use. In addition, it contains the jar files and things that maven itself needs.

When we ran the ‘mvn clean’ command, we asked maven to do a ‘clean’ command, which requires the maven-clean-plugin jar file.

If you look back at the command prompt output after the ‘mvn clean’ command, you can see that the maven-clean-plugin jar file was downloaded, since maven realized that it needed this jar file in order to do the ‘clean’.

As an Amazon Associate I earn from qualifying purchases.