Appendix C. Maven Global Settings

Maven's configuration can be easily customized by working with the settings.xml file. There are two locations where we can find the file:

  • The Maven global settings: $M2_HOME/conf/settings.xml
  • The user's settings: ${user.home}/.m2/settings.xml

If both locations exist, the content will be merged, but the user's settings get the highest priority.

The settings.xml file

Generally, the settings.xml file holds the following elements:

  • The location of the local repository
  • The default user interaction policy
  • The servers' configurations
  • The profiles to use
  • Other issues about plugins and mirrors not discussed in this book

The following code shows a simple user's 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 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <localRepository>
    ${user.home}/.m2/repository
  </localRepository>
  <interactiveMode>true</interactiveMode>
  <usePluginRegistry>false</usePluginRegistry>
  <offline>false</offline>

</settings>

Sometimes, especially on a remote organization's build server, you might need to change the default local repository location since the partitioning for the current user is too small. The following code shows how to change our default settings.xml file:

[…]
<localRepository>
        /my-high-space-disk/maven-repo
</localRepository>

<interactiveMode>false</interactiveMode>
[…]

It is also a good idea to set the interactiveMode tag to false so as to prevent any request from Maven on a build server (check out the Continuous integration and delivery with Hudson or Jenkins section of Chapter 5, Continuous Integration and Delivery with Maven).

Servers

In the servers section, we can specify some important deployment settings such as the username and password of the remote repository (refer to Chapter 5, Continuous Integration and Delivery with Maven). The following lines define the username and password as per the nexus server:

[…]
<servers>
  <server>
    <id>nexus</id>
    <username>admin</username>
    <password>admin123</password>
  </server>
</servers>
[…]

Proxies

Maven needs an Internet connection to download dependent artifacts or plugins. If the network of your organization is controlled by a proxy, you need to define the proxies on settings.xml:

[…]
<proxies>
  <proxy>
    <active>true</active>
    <protocol>http</protocol>
    <host>myproxy</host>
    <port>8080</port>
    <username>mydomainmyuser</username>
    <password>mysecret</password>
    <nonProxyHosts>localhost,my-server</nonProxyHosts>
  </proxy>
  <proxy>
    <active>true</active>
    <protocol>https</protocol>
    <host>myproxy</host>
    <port>8081</port>
    <username>mydomainmyuser</username>
    <password>mysecret</password>
    <nonProxyHosts>localhost,my-server</nonProxyHosts>
  </proxy>

</proxies>
[…]

Tip

The actual version of Maven doesn't support automatic proxy through the PAC script.

Profiles

The settings.xml file provides a fine-grained mechanism to control profiles. Profiles are extensively described in Chapter 4, Managing the Code; a profile can be easily activated through the –P parameter:

$ mvn clean install –P myprofile

A profile can also be defined using the activeByDefault element. It is possible to activate a profile through the settings.xml file. The following code activates the build-jdk5 profile when you run Maven under JDK 5:

[…]
<profiles>
  <profile>
    <id>build-jdk5</id>
    <activation>
      <activeByDefault>false</activeByDefault>
      <jdk>1.5</jdk>
    </activation>
  </profile>
</profiles>
[…]

The following code sets the delivery-host variable if a target-env property is dev:

[…]
<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <property>
        <name>target-env</name>
        <value>dev</value>
      </property>
    </activation>
    <properties>
      <delivery-host>my-dev-host</delivery-host>
    </properties>
  </profile>
</profiles>
[…]

Prior to Maven's compile phase, you can test the active profiles launching with the following command line:

$ mvn help:active-profiles

We should get the following output:

Active Profiles for Project 'com.packt.examples:transp-acq-ear:1.0':
 Dev
 Prod
The following profiles are active:
 Dev
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset