Maven provides a settings file, settings.xml, which allows us to specify which local and remote repositories it will use. We can also use it to store settings that we don’t want in our source code, such as credentials.
In this tutorial, we’ll learn how to use the settings.xml. We’ll look at proxies, mirroring, and profiles. We’ll also discuss how to determine the current settings that apply to our project.
Configurations :
The settings.xml file configures a Maven installation. It’s similar to a pom.xml file but is defined globally or per user.
Let’s explore the elements we can configure in the settings.xml file. The main settings element of the settings.xml file can contain nine possible predefined child elements:
- Plugin Groups
- Proxies
- Mirrors
- Servers
- Profiles
- Properties
- Plugin repositories
- Active Profiles
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> <localRepository/> <interactiveMode/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings> |
Simple value :
Some of the top-level configuration elements contain simple values
The localRepository element points to the path of the system’s local repository. The local repository is where all the dependencies from our projects get cached. The default is to use the user’s home directory. However, we could change it to allow all logged-in users to build from a common local repository.
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> <localRepository>${user.home}/.m2/repository</localRepository> <interactiveMode>true</interactiveMode> <offline>false</offline> </settings> |
Servers :
Defining repositories in the project pom.xml is a good practice. However, we shouldn’t put security settings, such as credentials, into our source code repository with the pom.xml. Instead, we define this secure information in the settings.xml file:
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> … <servers> <server> <id>server001</id> <username>my_login</username> <password>my_password</password> <privateKey>${user.home}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> <configuration></configuration> </server> </servers> … </settings> |
Plugin groups :
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> … <pluginGroups> <pluginGroup>org.eclipse.jetty</pluginGroup> </pluginGroups> … </settings> |
Proxies:
We can configure a proxy for some or all of Maven’s HTTP requests. The proxies element allows a list of child proxy elements, but only one proxy can be active at a time:
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> <proxies> <proxy> <id>baeldung-proxy</id> <active>true</active> <protocol>http</protocol> <host>baeldung.proxy.com</host> <port>8080</port> <username>demo-user</username> <password>dummy-password</password> <nonProxyHosts>*.baeldung.com|*.apache.org</nonProxyHosts> </proxy> </proxies> </settings> |
Mirrors:
Repositories can be declared inside a project pom.xml. This means that the developers sharing the project code get the right repository settings out of the box.
We can use mirrors in cases where we want to define an alternative mirror for a particular repository. This overrides what’s in the pom.xml.
For example, we can force Maven to use a single repository by mirroring all repository requests:
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> <mirrors> <mirror> <id>internal-baeldung-repository</id> <name>Baeldung Internal Repo</name> <url>https://baeldung.com/repo/maven2/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> </settings> |
Profiles :
The profiles element enables us to create multiple profile child elements differentiated by their ID child element. The profile element in the settings.xml is a truncated version of the same element available in the pom.xml.
- Repositories
- Plugin repositories
- Properties
- Activation
- Active Profiles
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd”> <profiles> <profile> <id>adobe-public</id> <repositories> <repository> <id>adobe-public-releases</id> <name>Adobe Public Repository</name> <url>https://repo.adobe.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> <profile> <id>adobe-public</id> <pluginRepositories> <pluginRepository> <id>adobe-public-releases</id> <name>Adobe Public Repository</name> <url>https://repo.adobe.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> <profile> <id>baeldung-test</id> <properties> <user.project.folder>${user.home}/baeldung-tutorials</user.project.folder> </properties> </profile> <profile> <id>baeldung-test</id> <activation> <activeByDefault>false</activeByDefault> <jdk>1.8</jdk> <os> <name>Windows 10</name> <family>Windows</family> <arch>amd64</arch> <version>10.0</version> </os> <property> <name>mavenVersion</name> <value>3.0.7</value> </property> <file> <exists>${basedir}/activation-file.properties</exists> <missing>${basedir}/deactivation-file.properties</missing> </file> </activation> </profile> </profiles> <activeProfiles> <activeProfile>baeldung-test</activeProfile> <activeProfile>adobe-public</activeProfile> </activeProfiles> </settings> |