Download, Install and Configure Maven
Introduction
Apache Maven is a powerful build automation tool for Java based projects, it is built based on the concept POM (Project Object Management).
Download and Install Maven
To download Maven, go to
- Download the binary zipped file apache-maven-<VERSION>-bin.zip and unzip it.
- Set environment property M2_HOME = <Maven installation directory>, this is an optional property, but it helps to find to locate Maven home by other programs.
- Add <Maven installation directory>/bin folder to PATH to access Maven commands from command prompt/terminal.
-
To check if the PATH is set correctly, use below command from command prompt or terminal, it prints the Maven version details.
$ mvn -version
Configure Maven
Once you downloaded and installed, you are good to start using Maven, but there are some configurations that you may want to make to fulfill different requirements.
-
Maven uses
as the master configuration file for different settings, it is located undersettings.xml
folder or under<Maven Home>/conf
folder.<USER_HOME>/.m2
- Let's look at some of the configuration in settings.xml file.
- localRepository: by default Maven uses
to store Maven artifacts, but if you wish to change to a different folder you can uncomment this tag and provide alternate path here.<User Home>/.m2/repository
- interactiveMode: this tells Maven whether to run in intercativeMode or not, it set to true, it prompts for user confirmation where needed, default value is is
.false
- offline: this tells Maven that whether it should connect to network to download artifacts or for deployments, default value is
false
- Proxy: you can setup a proxy for Maven for connecting to network using below settings.
- Repositories and Deployment settings: Repositories to deploy project are defined under distributionManagement section of the pom.xml, but we cannot pass security credentials in there, so we will define security related settings here and name it with an unique
and refer that in distributionManagement.id
<proxies>
<proxy>
<id>sample-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.sample.com</host>
<port>8080</port>
<username>user</username>
<password>pwd</password>
<nonProxyHosts>www.google.com|*.sample.com</nonProxyHosts>
</proxy>
</proxies>
<servers>
<server>
<id>repo1</id>
<username>repouser</username>
<!-- other optional settings:
<password>password</password>
<privateKey>/path/to/RSA key</privateKey> (default is ~/.ssh/id_rsa)
<passphrase>passphrase</passphrase>
-->
</server>
</servers>
How to install jars into local Maven repo
If you have some jars/artifacts built locally or not available through Maven repository, then you can install them into local repository using below command and use them in POM file.
mvn install:install-file -Dfile=/path/to/jar/file -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> (jar/war/zip etc);
If there is a pom file that you would like to install into local maven repository, you can install it using below command.
mvn install:install-file -Dfile=/path/to/jar/file -DpomFile=/path/to/pom/file