Settings. XML file, which is an XML file, can be used for self-declaration purposes.

  1. Specifies the address of the local repository
  2. Apache’s official Maven repository is too slow to download, so I added an image like Ali Cloud
  3. The configuration of the company’s LAN mirror resource library, the main two purposes, the first because the company’s image library is LAN, so the speed is obviously faster than Ali cloud, the second company’s internal development JAR package belongs to the company’s intellectual property rights is impossible to put on the Internet.

Setting. XML can be configured in a variety of ways. Even the POP.xml file can specify the maven repository for this project. I found a good article (90% of articles on the Internet are really poor quality). Reprinted from the search order relied on in the Maven project


Depends on how the warehouse is configured

The Maven project uses repositories in the following ways:

  • Central repository, this is the default repository
  • Mirror repository, configured via settings.mirrors in sttings. XML
  • Global profile warehouse, through the Settings. In XML Settings. The repositories. The repository configuration
  • Project warehouse by pom. The project in the XML. Repositories. The repository configuration
  • Project profile warehouse by pom. XML in the project. The profiles. The profile. The repositories. The repository configuration
  • The local repository local_repo, if all configurations were present, would have a very complicated search order for dependencies.

Analysis relies on search order

Start with the simplest and slowly add the configuration to see what happens.

Preparing the test environment

Install JDK and Maven.

Use the following command to create the test project:

yes | mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=true- DgroupId = com. Pollyduan - DartifactId = myweb - 1.0 Dpackage Dversion = = com. PollyduanCopy the code

After the creation is complete, compile is executed once to avoid interference from subsequent tests.

cd myweb
mvn compile
Copy the code

Finally, modify the POM.xml file to change the junit version number to 4.12. We will use this JAR to test the search order of dependencies.

The default

First make sure junit4.12 does not exist:

Rm - rf ~ / m2 / repository/junit/junit 4.12Copy the code

By default, no repository is configured, that is, neither $M2_HOME/conf/settings. XML nor ~/.m2/settings.xml is added

Perform compilation to see the repository where junit was pulled from the logs.

mvn compile

...
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.12/junit-4.12.pom (24 kB at 11 kB/s)

Copy the code

As you can see, the default jar is pulled from the central central repository.

Configure the repository settings_mirror

Create ~/.m2/setttings. XML as follows:

<settings>
 <mirrors>
  <mirror>
   <id>settings_mirror</id>
   <url>https://maven.aliyun.com/repository/public</url>
   <mirrorOf>central</mirrorOf>
  </mirror>
 </mirrors>
</settings>

Copy the code

Retest:

Rm - rf ~ / m2 / repository/junit/junit 4.12 / MVN compileCopy the code

View the repository on which the download depends in the log:

Downloaded from settings_mirror: https://maven.aliyun.com/repository/public/junit/junit/4.12/junit-4.12.pom (24 kB at 35 kB/s)
Copy the code

As you can see from the jar downloaded from settings_Mirror: Settings_Mirror has priority over Central

Configure the repository Pom_repositories in the POM

Add the following configuration to Project:

<repositories>
 <repository>
  <id>pom_repositories</id>
  <name>local</name>
  <url>http://10.18.29.128/nexus/content/groups/public/</url>
  <releases>
   <enabled>true</enabled>
  </releases>
  <snapshots>
   <enabled>true</enabled>
  </snapshots>
 </repository>
</repositories>

Copy the code

Since we changed the name of the ID, the warehouse address does not matter, and using the same address does not affect the test. Perform tests:

Rm - rf ~ / m2 / repository/junit/junit 4.12 / MVN compileCopy the code

View the repository on which the download depends in the log:

Downloaded from pom_repositories: http://10.18.29.128/nexus/content/groups/public/junit/junit/4.12/junit-4.12.pom (24 kB at 95 kB/s)
Copy the code

As can be seen from the displayed warehouse ID:

  • The JAR was downloaded from Pom_Repositories.
  • Pom_repositories takes precedence over settingS_Mirror

Configure the global profile repository settingS_profile_repo

Add the following to the Settings node in ~/.m2/settings. XML:

<profiles>
 <profile>
 <id>s_profile</id>
 <repositories>
  <repository>
   <id>settings_profile_repo</id>
   <name>netease</name>
   <url>http://mirrors.163.com/maven/repository/maven-public/</url>
   <releases>
    <enabled>true</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
 </repositories>
 </profile>
</profiles>
Copy the code

Perform tests:

Rm - rf ~ / m2 / repository/junit/junit 4.12 / MVN compile - Ps_profileCopy the code

View the repository on which the download depends in the log:

Downloaded from settings_profile_repo: http://mirrors.163.com/maven/repository/maven-public/junit/junit/4.12/junit-4.12.pom (24 kB at 63 kB/s)
Copy the code

As can be seen from the displayed warehouse ID:

  • The JAR was downloaded from settingS_profile_repo.
  • Settings_profile_repo has a higher priority than settings_mirror.
  • The settingS_profile_repo priority is higher than pom_repositories. Configure the project profile repository pom_profile_repo
<profiles>
 <profile>
  <id>p_profile</id>
  <repositories>
   <repository>
    <id>pom_profile_repo</id>
    <name>local</name>
    <url>http://10.18.29.128/nexus/content/groups/public/</url>
    <releases>
     <enabled>true</enabled>
    </releases>
    <snapshots>
     <enabled>true</enabled>
    </snapshots>
   </repository>
  </repositories>
 </profile>
</profiles>
Copy the code

Perform tests:

Rm - rf ~ / m2 / repository/junit/junit 4.12 / MVN compile - Ps_profile p_profile MVN compile - Pp_profile s_profileCopy the code

View the repository on which the download depends in the log:

Downloaded from settings_profile_repo: http://mirrors.163.com/maven/repository/maven-public/junit/junit/4.12/junit-4.12.pom (24 kB at 68 kB/s)
Copy the code

As can be seen from the displayed warehouse ID:

Jar downloaded from settingS_profile_repo settingS_profile_repo has a higher priority than pom_profile_repo

Rm - rf ~ / m2 / repository/junit/junit 4.12 / MVN compile - Pp_profileCopy the code

View the repository on which the download depends in the log:

Downloaded from pom_profile_repo: http://10.18.29.128/nexus/content/groups/public/junit/junit/4.12/junit-4.12.pom (24 kB at 106 kB/s)
Copy the code

As can be seen from the displayed warehouse ID:

  • The JAR was downloaded from settingS_profile_repo
  • Local_repo ~/.m2/repository is not a test, it is only a conclusion, can be any test.

As long as ~/.m2/repository contains dependencies, the jar in the local repository will be used in preference regardless of configuration.

The final conclusion

  • Settings_mirror has a higher priority than Central
  • Settings_profile_repo has a higher priority than settings_mirror
  • The settingS_profile_repo priority is higher than pom_repositories
  • Settings_profile_repo has a higher priority than pom_profile_repo
  • Pom_profile_repo has a higher priority than Pom_repositories
  • Pom_repositories takes precedence over SETTingS_Mirror to obtain the complete search chain from the comparison above:

local_repo > settings_profile_repo > pom_profile_repo > pom_repositories > settings_mirror > central

When a dependent address with a higher priority cannot be found (for reasons such as non-existence or network timeout), it will be searched in descending order of priority according to this search chain.