Now that we have got an idea of the essential elements of a pom file, let us also examine the various setting properties of Maven.
To understand the Maven settings, perform the following steps:
settings.xml
file in the .m2
subfolder of your HOME
folder, if it exists:settings.xml
file in the conf
folder of your Maven installation (as defined in M2_HOME
).Maven has a global settings file called settings.xml
in the conf
folder of the Maven installation. The values in this file can be overridden in the user settings file— the settings.xml
file—that is present in the .m2
subfolder of your HOME
folder.
The settings file contains configurations that are not specific to a project, but are global in nature. It also contains information that is not meant to be distributed (for example, passwords).
Like the pom file, the settings file is also an XML file based on an XML schema. It starts as follows:
<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">
Let us now see some of the typical setting configurations:
The following code represents the localRepository
element in the settings file:
<localRepository>${user.home}/.m2/repository</localRepository>
We have seen this in the Changing the location of the Maven repository recipe in Chapter 1, Getting Started, where we wanted to change the default location where Maven dependencies and plugins are stored.
The following code represents the offline
element in the settings file:
<offline>false</offline>
This setting indicates whether Maven should operate in offline mode; that is, it should not download updates or dependencies if they are not available.
We saw proxies in the Running Maven behind an HTTP proxy server recipe in Chapter 1, Getting Started. The following code represents the proxies
element in the settings file:
<proxies> <proxy> <id>myproxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.myorg.com</host> <port>8080</port> <username>proxyuser</username> <password>somepassword</password> <nonProxyHosts>*.myorg.com </nonProxyHosts> </proxy> </proxies>
This allows us to specify a proxy server to connect to the Internet. This is relevant in enterprises where direct access to the Internet might be blocked due to security or other reasons.
The following code represents the mirrors
element in the settings file:
<mirror> <id>nexus</id> <name>My Company Mirror</name> <url>http://nexus.mycompany.com/pub/maven2</url> <mirrorOf>central</mirrorOf> </mirror>
Instead of downloading dependencies from Maven Central, you can configure Maven to download them from a mirror of the central repository. This is extremely useful in an organization where the repository can be mirrored in a repository manager within an organization and all users can download dependencies from this mirror.
Repositories are remote collections of projects that Maven uses to populate the required dependencies to a local repository. There are two types of repositories—releases
and snapshots
—and Maven allows specific configurations for each, as illustrated in the following code:
<repositories> <repository> <id>codehausSnapshots</id> <name>Codehaus Snapshots</name> <releases> <enabled>false</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> <url>http://snapshots.maven.codehaus.org/maven2</url> <layout>default</layout> </repository> </repositories>
While repositories store dependencies required for the project, the pluginRepositories
element stores plugin libraries and associated files. Maven distinguishes between these two by having separate configurations for both. The elements are the same as that for repositories, except that the parent element is pluginRepositories
.
The repositories for download and deployment are defined by the repositories
and distributionManagement
elements of the pom file. However, settings such as username
and password
cannot be distributed in the pom file for confidentiality reasons. Maven provides a mechanism to specify this in the settings file:
<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>
18.188.190.175