Maven <repositories> Tag,<pluginrepositories> tags

Source: Internet
Author: User
Tags repetition maven central

In the case of MAVEN, for example, when we used ant to build a project, under the project directory, we would often see a subdirectory called/lib, where all kinds of third-party dependent jar files, such as Log4j.jar,junit.jar and so on, are stored. For each project you build, you need to create such a/lib directory, and then copy a pair of jar files, which is obviously duplicated. Repetition is always the starting point for nightmares, and multiple projects that do not share the same jar files will not only result in wasted disk resources, but also make consistency management of versions difficult. Also, if you use version management tools such as SVN (you are not using the version management tool?). Try SVN now, it can help you solve a lot of headaches, you need to submit a large number of jar files to the code base, but the version management tool in the processing of binary files is not good.
The Maven repository is where all the jar files (war,zip,pom, etc.) are placed, and all MAVEN projects can get the dependency jar they need from the same MAVEN repository, which saves disk resources. In addition, because all jars in the Maven repository have their own coordinates, the coordinates tell maven its group ID, artifact ID, version, packaging method, and so on, so MAVEN projects can be easily dependent on version management. Neither do you. To submit the jar file to the SCM repository, you can create an organization-level MAVEN repository for all members to use.
In short, maven repositories can help us manage artifacts (primarily jars).

Local warehouses (. m2) vs. remote warehouses (Networking)
When you run MAVEN, any artifacts required by MAVEN are obtained directly from the local repository. If the local repository does not, it will first attempt to download artifacts from the remote repository to the local repository and then use the artifacts from the local warehouse.
For example, your project is configured with junit-3.8 dependencies, and when you run MVN test, MAVEN needs to use the junit-3.8 jar file, which first locates the local repository based on coordinates and uses it directly if found. If not, MAVEN checks the available remote warehouse configurations and then tries each of these remote warehouses to download the junit-3.8 jar file, and if the remote repository exists, MAVEN downloads it to the local repository and then uses it. If Maven has not been able to download the file after trying all the remote repositories, it will error.
The MAVEN default local warehouse address is ${user.home}/.m2/repository. In other words, a user will have a local repository.
You can also customize the location of the local warehouse, modify${user.home}/.m2/Settings.xml:

  <!--localrepository   | The path to the local repository maven would use to store artifacts.   Default: ${user.home}/.m2/repository-->  <localRepository>D:/.m2/repository</localRepository>

You can also specify the local warehouse location at run time:
MVN clean install-dmaven. repo.local=/home/juven/myrepo/

It is also important to understand that when we run the install, MAVEN actually installs the artifacts generated by the project to the local repository, which means that the artifacts generated by this project will be available to other projects only after the install is installed.

Then look at the MAVEN default remote repository, the MAVEN central repository:
After installing MAVEN, we can build a simple project, configure some simple dependencies, and then run the mvn clean install, and the project is built. We did not download any jar files by hand, all because of the presence of the MAVEN central repository, which would look for a remote repository when Maven could not find the required jar file in the local repository, and an original MAVEN installation comes with a remote repository--maven central repository.
Where is the MAVEN central repository defined? On my machine, I installed the maven-2.0.10, I can find this file: ${m2_home}/lib/maven-2.0.10-uber.jar,

However, after the 3.xxx version in:/lib/maven-model-builder-${version}.jar in the MAVEN installation directory:

To open the file, you can find the Super Pom:\org\apache\maven\model\pom-4.0.0.xml, which is the parent pom of all maven Pom, all MAVEN projects inherit this configuration, you can find the following configuration in this pom:

  <repositories>    <repository>      <id>central</id>      <name>central Repository </name>      <url>https://repo.maven.apache.org/maven2</url>      <layout>default</ layout>      <snapshots>        <enabled>false</enabled>      </snapshots>    </ Repository>  </repositories>

About the configuration of the remote warehouse, I will explain in detail here, as long as we know that the Central warehouse ID is centrally, the remote URL address is http://repo.maven.apache.org/maven2, it turned off support for snapshot version component download.

Configuring the remote warehouse in Pom
Before we saw the super Pom configured with a remote repository with ID Central, we can configure other remote repositories in the POM. There are many reasons for this, such as you have a remote repository of LAN , using the warehouse can greatly improve the download speed, and then improve the build speed, it is possible that you rely on a jar in central can not find, it exists only in a specific public warehouse, This way you also have to add the configuration of that remote repository.
Here I configure a remote repository to point to the central warehouse of the Chinese Mirror:

<project>,  ..... <repositories>    <repository>      <id>maven-net-cn</id>      <name>maven China mirror</name>      <url>http://maven.net.cn/content/groups/public/</url>      <releases>        <enabled>true</enabled>      </releases>      <snapshots>        <enabled>false </enabled>      </snapshots>    </repository>  </repositories>  < pluginrepositories>    <pluginRepository>      <id>maven-net-cn</id>      <name> Maven China mirror</name>      <url>http://maven.net.cn/content/groups/public/</url>      < releases>        <enabled>true</enabled>      </releases>      <snapshots>        < enabled>false</enabled>      </snapshots>        </pluginRepository>  </ Pluginrepositories>...</project>

Let's take a look at <repositories> 's configuration, you can add multiple <repository> under it, each <repository> has its unique ID, a descriptive name, and, most importantly, The URL of the remote repository. In addition,,<releases><enabled>true</enabled></releases> tells Maven to download the releases version of the widget from this repository, while < Snapshots><enabled>false</enabled></snapshots> tells maven not to download the snapshot version of the widget from this repository. It is recommended that you prohibit the download of snapshot artifacts from public repositories, because these artifacts are unstable and are not under your control and should be avoided. Of course, you can activate snapshot support if you want to use a warehouse inside your local area network.

For more detailed configuration of <repositories> and related explanations, please refer to: http://www.sonatype.com/books/maven-book/reference_zh/apas02s08.html.
As for <pluginrepositories>, this is where MAVEN is configured to download the plugin widget (all of Maven's actual behavior is done by its plug-in). The internal configuration of the element is exactly the same as the <repository>, no longer explained.

Configuring the remote warehouse in settings.xml
We know how to configure a remote repository in Pom, but consider a situation in which 3 projects are carried out at the same time within a company, and more and more projects will begin as the projects end, and a MAVEN repository is built within the company. We unified the repository for all of these projects, so we had to provide the same configuration for each project. The problem arises, this is repetition!
In fact, we can only configure once, where to configure it? Is settings.xml.
But it's not that simple, it's not simple. Copy <repositories> and <pluginRepositories> elements in Pom to settings.xml, setting.xml not directly support These two elements. But we still have a not-so-complicated solution, which is to use profile, as follows:

<settings> ...  <profiles>    <profile>      <id>dev</id>      <!--repositories and pluginrepositories here-->    </profile>  </profiles>  <activeProfiles>    <activeprofile>dev </activeProfile>  </activeProfiles>  ...</settings>

Here we define a profile with the ID of Dev, put all repositories and pluginrepositories elements into this profile, and then, using <activeProfiles> Element activates the profile automatically. This way, you will no longer have to configure the warehouse for each Pom repeatedly.
Using the profile to add a warehouse to Settings.xml provides a user-wide warehouse configuration.

Using mirroring
If you have a faster central image near your geographic location, or if you want to overwrite the central repository configuration, or if you want to use only one remote repository for all POM (all the other warehouses necessary for this remote Repository agent), You can use the mirror configuration in Settings.xml.
The following mirror configuration uses maven.NET.cn to cover the MAVEN's own central:

<settings>,  ..... <mirrors>    <mirror>      <id>maven-net-cn</id>      <name>maven China mirror</ name>      <url>http://maven.net.cn/content/groups/public/</url>      <mirrorOf>central< /mirrorof>    </mirror>  </mirrors>...</settings>

The only thing that needs to be explained here is <mirrorof> Here we configure the central image, and we can configure a mirror of all the warehouses to ensure that the image is the only repository used by Maven:

  <mirrors>    <!--mirror     | Specifies a repository mirror site to use instead of a given repository. The repository that | This mirror serves have an ID, that matches, the     mirrorof element of this mirror. IDs is used     | For inheritance and direct lookup purposes, and must is unique across the set of mirrors.     |     -    <mirror>      <id>nexus</id>      <mirrorOf>*</mirrorOf>      <name >nexus mirror</name>      <url>http://xx.xx/nexus/content/groups/public</url>    </ Mirror>  </mirrors>
<settings>,  ..... <mirrors>    <mirror>      <id>my-org-repo</id>      <name>repository in my orgnization</name>      <url>http://192.168.1.100/maven2</url>      <mirrorof>*</ mirrorof>    </mirror>  </mirrors>...</settings>

Maven <repositories> Tag,<pluginrepositories> tags

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.