Book Image

Apache Maven Cookbook

Book Image

Apache Maven Cookbook

Overview of this book

Table of Contents (18 chapters)
Apache Maven Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Changing the location of the Maven repository


There are three types of Maven repositories:

  • Local: This is the repository in your computer filesystem

  • Remote: This is the repository from where the required Maven files get downloaded

  • Mirrors: These are repository managers, such as Nexus and Artifactory, that mirror various repositories

You will have seen Maven downloading a number of files (called poms and jars). Let us see where they are located in your computer:

  • Go to your HOME folder (C:\Users\username) in the case of Microsoft Windows, /Users/username for Mac, and, /home/username (or a similar location) for Linux

  • You will notice the .m2 folder and within that, a subfolder called repository

    Tip

    Any folder that starts with a dot (.) is typically hidden from view. You will need to change your folder viewer settings to see it.

  • You will see a number of folders and files that are used by Maven

You may want to change this location for the following reasons:

  • You may want to conserve space in the C drive and store these folders and files in the D drive on Microsoft Windows.

  • You may want to take a back up of the contents. Backup software usually backs up contents in specific folders of the filesystem.

  • Your organization may have a policy for all users to store a local repository in the same folder.

How to do it...

To change the location of the Maven repository, perform the following steps:

  1. Create a file called settings.xml in the .m2 folder.

  2. Add the following contents to the settings.xml file that you just created:

    <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>C:/software/maven</localRepository>
    </settings>

    Notice the highlighted part of the preceding code. We have changed the location of the repository contents to C:\software\maven. You can change it to any valid folder name.

  3. Delete the repository subfolder and run the mvn package command again.

    You will now notice that the repository folder is not created in the .m2 folder. Instead, it is created in C:\software\maven.

How it works...

Maven determines the location of the local repository in the following way:

  • If settings.xml exists in the user's .m2 folder, which contains the <localRepository> tag, then Maven uses its contents to determine the location

  • If not, Maven will check if localRepository is explicitly defined in the default settings.xml, present in the conf folder of the Maven installation

  • If it is not present there, Maven will use the default value for the local repository, which is the user's .m2 folder