Jan 23, 2014

Setting up Java and Maven with environment variables

This is an extension post to Java Tutorial: Setting up Java, Maven, and eclipse. Setting up Java and Maven is the first step any beginner has to do. So, understanding this will save so much pain.

Q. What are environment variables?
A. An environment variable defines some aspect of a user's or a program's environment. After you have installed the Java Runtime Environment (JRE) in Windows, you must set the JAVA_HOME environment variable to point to the JRE installation directory. Same applies for other applications like Maven. These variables are then used to set the "PATH" variable so that when you type "java" or "mvn" on a dos command line, the application can be found.

In a Windows platform, you can write a batch file to set up your environment variables as shown below:

@echo off
REM This is sample to set up your Java and Maven environment variables
SET JAVA_HOME=%TOOLS_HOME%\java\jdk1.6.0_45
SET M3_HOME=%TOOLS_HOME%\maven\apache-maven-3.1.0


echo ******** Java Toolkit setup ****************
echo M3_HOME     = %M3_HOME%
echo PATH        = %PATH%
echo ********************************************

Q. How do you list the environment variables on DOS command prompt?
A. by typing the "set" command.

Q. How do echo a particular variable?
A. by typing the "echo %M3_HOME%" command display M3_HOME variable. In Unix use echo $M3_HOME. You can also do "set path" to display the program path or "set M3_HOME" to display maven env variable.

Q. How will you set your environment variables on an account or System level on Windows 8 or later?
A. On Windows 8, search for "Edit environment variables".

Then, you can set it via the pop up GUI

Q. Maven needs a settings.xml file. How do you find out which settings.xml file is used by Maven?
A. With the debug option mvn -X command.

[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from C:\TOOLS\maven\apache-maven-3.1.0\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\akumaras\.m2\settings.xml
[DEBUG] Using local repository at c:\tools\home\.m2\repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for c:\tools\home\.m2\repository

You can get both the settings.xml file and the repository location "c:\tools\home\.m2\repository" where the artefacts will be downloaded to.

Maven uses the user.home property set via MAVEN_OPTS


Q. How do you know that your Java and Maven are set up correctly?
A.Typing Java or MVN on a command prompt should recognize the command and give you an appropriate display with its version numbers, etc.

Q. How does maven know where to download the jar files from? in other words, how do you specify the Maven repository location?
A. Following snippets in the settings. xml file. The repo location is


  <!--Enable snapshots for the built in central repo to direct -->
  <!--all requests to nexus via the mirror -->

Q. If I already have the jar file, can I manually install into my Maven repository pointed by user.home property as demonstrated above?
A. Yes, use the following command

mvn install:install-file \
  -DgroupId=org.springframework \
  -DartifactId=spring-core \
  -Dpackaging=jar \
  -Dversion=3.1.0.RELEASE \
  -Dfile=jta-1.0.1B.jar \
Q. How do you search for artifacts on the central repository?
A. Go to central repository website and search. Prefix with g: for groupid, a: for artifactid, and v: for version number.

g:org.springframework a:spring-core  v:3.1.0.RELEASE

You may also like

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home