Maven Eclipse User Preference - Crunchify

Are you running multiple Eclipse Environments on your developer workstation? Or on your laptop? I do. Let’s consider a scenario with two different Eclipse workspace with different custom maven settings.

Why is that case? Let’s understand.

  • What if your Work development environment or framework is based on Eclipse? In company environment, all of your required project’s .jar files must be located in private repositories and you have custom settings.xml file with very specific repositories.
  • And on opposite side, for standard eclipse/maven usage – all public maven repositories are available to us for free without any restriction.

Definitely we need two different settings.xml files for above usage. If you are new to maven and eclipse setup then follow these steps to setup maven correct way on you laptop/desktop.

NOTE: How to add settings.xml in eclipse?  settings.xml is not required (and thus not auto created in ~/.m2 folder) unless you want to change the default settings. In our case we do have different settings for work and personal workspace.

If you also have below questions then you are at right place:

  • eclipse – Why maven settings.xml file is not there?
  • How do I link to my settings.xml file from Eclipse
  • Development project with Eclipse and Maven
  • m2e users – where is settings.xml file?
  • maven settings.xml file download
  • eclipse embedded maven
  • eclipse maven settings.xml location

What is settings.xml file?

settings.xml is a file which provides all configuration for maven which internally referenced in your maven project‘s pom.xml file. When you add dependencies into pom.xml file maven internally downloads required .jar file and put it under the folder which is mentioned in settings.xml file.

Where can I find settings.xml file?

  • Windows machine: C:\Users\ashah\.m2\settings.xml
  • On Mac OS X:  /home/ashah/.m2/settings.xml

What is a default content of settings.xml file?

Just update <UserName> in below file with your system’s username. Here is a maven settings.xml download file.

<?xml version="1.0" encoding="UTF-8"?>
 
<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 | |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
          
  <!--  Change username in below line  -->        
  <localRepository>/Users/ashah/.m2/repository</localRepository>
 
  <interactiveMode>true</interactiveMode>
 
  <offline>false</offline>
 
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>
 
  <proxies>
    <!--
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>
 
  <servers>
    <!--
    <server>
      <id>deploymentRepo</id>
      <username>crunchify</username>
      <password>crunchify</password>
    </server>
        -->
  </servers>
 
  <mirrors>
    <!--
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>mirror description</name>
      <url>http://my.repository.com/repo/path</url> 
    </mirror>
    -->
  </mirrors>
 
  <profiles>
  </profiles>
 
</settings>

Above is a sample settings.xml for maven eclipse.

How can I use two different settings.xml files for personal and work related repository?

Step-1

  • Go to ~/.m2 folder
  • Put work related settings.xml file and provide localRepository path:
  • <localRepository>/Users/<UserName>/.m2/work</localRepository>

Step-2

  • Go to ~/.m2 folder
  • Put local eclipse related crunchify-settings.xml file and provide localRepository path:
  • <localRepository>/Users/<username>/.m2/repository</localRepository>
Use Two different settings.xml file - Crunchify Tips

What is you don’t have correct settings.xml file?

Eclipse throws number of different errors:

Eclipse throws number of different errors in case of missing settings.xml file

Once you have correct settings.xml file. Try performing below steps and all errors should go away.

  1. Click on Project Menu
  2. Click on Clean... and clean you project
  3. Right click on project
  4. Click on Maven
  5. Click on Update Project... (below diagram)
Force Update of Snapshot and Releases for Maven Project in Eclipse

That’s it. After performing above steps, Eclipse will import all of your project dependencies under path provided in settings.xml file. You shouldn’t see any compilation issue in Eclipse after import process completes.

Based on your project dependencies and internet download speed, dependency import process may take between ~5 minutes to 30 minutes.

The post Missing Maven settings.xml file for your Eclipse? What if you need two settings.xml files for Work and Personal Workspace? appeared first on Crunchify.