Setting up Apache Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Maven is distributed in several formats for your convenience. Simply pick a ready-made binary distribution archive and follow the installation instructions.

  1. Download a Binary zip archive of Maven and unzip it from https://maven.apache.org/download.cgi
  2. Create an M2_HOME System Variable
  3. Add %M2_HOME%\bin; to your PATH variable
  4. Open a command window cmd. Check: mvn -v
  5. Make sure you have .m2/repository (erase all the folders and files below)
  6. If you are going to use Eclipse, You will need to create the settings.xml. See below for more info on how to create it.
  7. If you have a proxy, you will need to configure it http://maven.apache.org/guides/mini/guide-proxies.html

Creating settings.xml file

The settings element in the settings.xml file contains elements used to define values which configure Maven execution in various ways, like the pom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.

There are two locations where a settings.xml file may live:

  • The Maven install: ${maven.home}/conf/settings.xml
  • A user’s install: ${user.home}/.m2/settings.xml

The former settings.xml are also called global settings, the latter settings.xml are referred to as user settings. If both files exists, their contents gets merged, with the user-specific settings.xml being dominant.

The settings file is never created automatically, you must create it yourself, whether you use embedded or “real” maven.

Tip: If you need to create user-specific settings from scratch, it’s easiest to copy the global settings from your Maven installation to your${user.home}/.m2 directory. Maven’s default settings.xml is a template with comments and examples so you can quickly tweak it to match your needs.

Create it at the following location <your home folder>/.m2/settings.xml e.g. C:\Users\YourUserName\.m2\settings.xml on Windows or /home/YourUserName/.m2/settings.xml on Linux

Here’s an empty skeleton you can use:

[cc lang=”xml”]







[/cc]

If you use Eclipse to edit it, it will give you auto-completion when editing it.

And here’s the Maven settings.xml Reference page
This link is a reference for the user-specific configuration for Maven.

Configure the Proxy

If you have a proxy, you will need to configure it http://maven.apache.org/guides/mini/guide-proxies.html

You can configure a proxy to use for some or all of your HTTP requests with Maven. The username and password are only required if your proxy requires basic authentication (note that later releases may support storing your passwords in a secured keystore – in the mean time, please ensure your settings.xml file (usually ${user.home}/.m2/settings.xml) is secured with permissions appropriate for your operating system).

The nonProxyHosts setting accepts wild cards, and each host not to proxy is separated by the | character. This matches the JDK configuration equivalent.

[cc lang=”xml”]
true https host ip host port

[/cc]

Errors

Checking Maven Installation

Got this error after entering mvn -v
[cc lang=”java”]
Exception in thread “main” java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)
[/cc]

This is because my JAVA_HOME environment variable is set to jdk1.6, and the maven version I installed is 3.9. According to maven website, the last version to support Java 6 is 3.2.5, and 3.3 and up use Java 7.

Could not calculate build plan

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved

Make sure you have connectivity (you can browse) (This kind of error is usually due to connectivity with Internet). Try the above steps to ensure Maven is installed correctly.

Create a Run Configuration for Maven

  • Define the base directory where the project repository is.
  • Set Goals as: clean package
  • Define user settings: C:\Users\your folder\.m2\settings.xml

Leave a Reply

Your email address will not be published. Required fields are marked *