Eclipse + Maven – Setting M2_REPO classpath variable in IDE

In previous article, we have integrated Apache Maven – a popular build tool with Eclipse IDE

By default, M2_REPO classpath variable doesn’t come added with Eclipse IDE in older versions

With newer versions like Kepler, Luna or Mars OR after integrating maven plugins (m2eclipse) with Eclipse IDE, M2_REPO classpath variable gets added –> pointing to default locations (for example c:\Users\<user_name>\.m2\repository) and this variable is non-modifiable –> means we can’t edit it to point to the correct local maven repository

Lets us concentrate on setting M2_REPO classpath variable and in the succeeding article we will see how can we edit this non-modifiable variable and correct it to point local maven repository

1. Setting M2_REPO classpath variable in IDE :

There are two ways

  • Manual approach via Eclipse IDE
  • Maven command approach

1.1 Manual approach via Eclipse IDE

Step 1: Open Eclipse IDE –> Windows –> Preferences

1_Eclipse-Maven_Setting_M2_REPO_open_eclipse_windows_preferences

Step 2: Go to Java –> Build Path –> Classpath Variables

2_Eclipse-Maven_Setting_M2_REPO_java_build_path_classpath_variables

Step 3: Click New –> Enter following details –> Click OK

Name: M2_REPO
Path: D:\M2_HOME\.m2\repository (pointing to local maven repository location)

3_Eclipse-Maven_Setting_M2_REPO_add_local_repository_location

Step 4: Done “M2_REPO” classpath variable added to Eclipse IDE successfully

4_Eclipse-Maven_Setting_M2_REPO_added_verify

That’s all !! completed walking to Way-1. Lets us move on to check the Way-2

1.2 Maven command approach

Step 1: Run below maven cmd in Windows command prompt

Syntax:

mvn -Declipse.workspace="eclipse_workspace_location" eclipse:configure-workspace

Example:

mvn -Declipse.workspace="D:\WORKSPACE\TEST_WORKSPACE" eclipse:configure-workspace
5_Eclipse-Maven_Setting_M2_REPO_maven_cmd_aproach_prompt

Note: Before moving to Step 2 to verify, close eclipse IDE if it’s already opened and open again (restart required to take effects of new changes)

Step 2: Verify …..Whether M2_REPO classpath variable added or not –> pointing to correct local maven repository location

6_Eclipse-Maven_Setting_M2_REPO_maven_cmd_aproach_prompt_verify

That’s pretty clear now, we can add M2_REPO classpath variable using mvn command

2. Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Eclipse + Maven - M2_REPO is Non Modifiable
Eclipse IDE + Maven - Integration