Maven – Settings File

Maven settings.xml file contains global configurations that are not specific to a project. It also contains secure information that is not meant to be distributed (e.g. passwords).

1. Location of Maven Settings File

Maven can have two settings files working at a time:

  • Maven installation directory: $M2_HOME/conf/settings.xml [Global settings file]
  • User’s home directory: ${user.home}/.m2/settings.xml [User settings file]

Both files are optional. If both files are present, the values in the user home settings file override the values from the global settings file.

2. Maven Default setting.xml

A default maven setting.xml look like this:

<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">
      <localRepository/>
      <interactiveMode/>
      <usePluginRegistry/>
      <offline/>
      <pluginGroups/>
      <servers/>
      <mirrors/>
      <proxies/>
      <profiles/>
      <activeProfiles/>
</settings>

A brief description of the elements is given in the below table, Read about their detailed usage in Maven documentation.

Element Name
Description
localRepository
Maven stores copies of plug-ins and dependencies locally in the C:\Users\<your_user_name>\.m2\repository folder. This element can be used to change the path of the local repository.
interactiveMode
As the name suggests, when this value is set to true, the default value, Maven interacts with the user for input.
usePluginRegistry
It decides that if Maven should use the ${user.home}/.m2/plugin-registry.xml file to manage plugin versions. Its default value is false.
offline
When set to true, this configuration instructs Maven to operate in an offline mode. The default is false.
pluginGroups
It contains a list of pluginGroup elements, each containing a groupId. The list is searched when a plugin is used and the groupId is not provided in the command line. This list automatically contains org.apache.maven.plugins and org.codehaus.mojo.
servers
Maven can interact with a variety of servers, such as Apache Subversion (SVN) servers, build servers, and remote repository servers. This element allows you to specify security credentials, such as the username and password, which you need to connect to those servers.
mirrors
As the name suggests, mirrors allow you to specify alternate locations for your repositories.
proxies
proxies contain the HTTP proxy information needed to connect to the Internet.
profiles
profiles allow you to group certain configuration elements, such as repositories and pluginRepositories.
activeProfile
The activeProfile allows you to specify a default profile to be active for Maven to use.

Drop me your questions in the comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

1 thought on “Maven – Settings File”

  1. I needed an external set of properties to be loaded by the integrated local development editor web server runtime. However, nor System properties or platform environment variables met this condition.
    There’s a way to load properties from the user settings file. To achieve this from the Java source code of a maven driven project, we need the maven-plugin-tool annotations package includes a [MavenSettingsBuilder]).
    Typically, a singleton class could load the user settings from Java code. Here’s an example :

    public class Settings {
    
            private static Settings __instance = new Settings();
    
            public static Settings getInstance() {
                    return __instance;
            }
            private final org.apache.maven.settings.Settings settings;
    
            /**
             * Creates a new instance of Settings
             */
            public Settings() {
                    settings = new org.apache.maven.settings.Settings();
            }
    
           /**
             * Reads in the active profiles for a property set.
             * @param key user settings.xml corresponding property key
             * settings.xml
             * @return the value of the key that is found, or {@link System#getProperty(String)}
             */
            public String findProfileProperty(String key) {
                    for (Profile p : settings.getProfiles()) {
                            for (String name : settings.getActiveProfiles()) {
                                    if (name.equals(p.getId()) &amp;&amp; p.getProperties().containsKey(key)) {
                                            return p.getProperties().getProperty(key);
                                    }
                            }
                    }
                    return System.getProperty(key);
            }
    }

    By this way with an appropriate set of settings.xml files within our network, we can load variables from the user context, which can also use secure encrypted passwords, per-user defined properties, platform dependent parameters etc.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.