4.2. Locating Maven Artifacts at Build Time
Overview
This section explains how Maven locates artifacts at build time. Essentially, Maven implements a simple caching scheme: artifacts are downloaded from remote repositories on the Internet and then cached in the local repository. Figure 4.1, “How Maven Locates Artifacts at Build Time” shows an overview of the procedure that Maven follows when locating artifacts at build time.
Figure 4.1. How Maven Locates Artifacts at Build Time
Procedure for locating artifacts
While building a project, Maven locates required artifacts (dependencies, required plug-ins, and so on) as follows:
- The first place that Maven looks for artifacts is in the local repository, which is the local cache where Maven stores all of the artifacts it has downloaded or found elsewhere. The default location of the local repository is the
.m2/repository/
directory under the user's home directory. - If an artifact is not available in the local repository, Maven has an ordered list repositories, from which it can try to download the artifact. This list of repositories can include both internal and remote repositories. Normally, any internal repositories (that is, repositories maintained in the local network) should appear at the head of the repository list, so that they are consulted first.
- If the artifact is not available from local or internal repositories, the next repositories to try are the remote repositories (which are accessible, for example, through the HTTP or the HTTPS protocols).
- When a Maven project is built using the
mvn install
command, the project itself is installed into the local repository.
Configuration
You can configure the following kinds of repository for locating Maven artifacts at build time:
Local repository
Maven resolves the location of the local repository, by checking the following settings:
- The location specified by the
localRepository
element in the~/.m2/settings.xml
file (UNIX and Linux) orC:\Documents and Settings\UserName\.m2\settings.xml
(Windows). - Otherwise, the location specified by the
localRepository
element in theM2_HOME/conf/settings.xml
file. - Otherwise, the default location is in the user's home directory,
~/.m2/repository/
(UNIX and Linux) orC:\Documents and Settings\UserName\.m2\repository
(Windows).
Internal repositories
Maven enables you to specify the location of internal repositories either in your
settings.xml
file (which applies to all projects) or in a pom.xml
(which applies to that project only). Typically, the location of an internal repository is specified using either a file://
URL or a http://
URL (assuming you have set up a local Web server to serve up the artifacts) and you should generally ensure that internal repositories are listed before remote repositories. Otherwise, there is nothing special about an internal repository: it is just a repository that happens to be located in your internal network.
For an example of how to specify a repository in your
settings.xml
file, see the section called “Adding the Red Hat JBoss Fuse repository”.
Remote repositories
Remote repositories are configured in the same way as internal repositories, except that they should be listed after any internal repositories.