(11) configuration of Maven remote repository, maven Repository
1. Remote repository Configuration
During normal development, we often do not use the default central warehouse. The access speed of the default central warehouse is relatively slow. There may be a lot of visitors, and sometimes it cannot meet the needs of our projects, some components may not exist in the central repository of the project, but are in other remote repositories, such as the JBoss Maven repository. In this case, you can configure the repository in pom. xml. The Code is as follows:
1 <! -- Configure remote repository --> 2 <repositories> 3 <Repository> 4 <id> jboss </id> 5 <name> JBoss repository </name> 6 <url> http://repository.jboss.com/maven2/ </ url> 7 <releases> 8 <enabled> true </enabled> 9 <updatePolicy> daily </updatePolicy> 10 </releases> 11 <snapshots> 12 <enabled> false </ enabled> 13 <checksumPolicy> warn </checksumPolicy> 14 </snapshots> 15 <layout> default </layout> 16 </repository> 17 </repositories>
Repository:Under the repositories element, you can use the repository sub-element to declare one or more remote repositories.
Id:The unique id of the Repository Declaration. In particular, the id of the central repository that comes with Maven is central. If other repository declarations use this id, the configuration of the central repository will be overwritten.
Name:The repository name makes it intuitive and convenient for us to know which warehouse is, and we have not found any other meaning.
Url:The address pointing to the repository. Generally, the repository address is based on the http protocol. Maven users can open the repository address browsing component in the browser.
Releases and snapshots:Used to control Maven's permission to download release and snapshot components. Note thatEnabledSub-element. In this example, the enabled value of releases is true, indicating that the JBoss repository's Release Version Download support is enabled, and the enabled value of snapshots is false, indicates that the JBoss repository snapshot Version Download is disabled. According to this configuration, Maven only downloads the components of the released version from the JBoss repository, rather than those of the snapshot version.
Layout:The element value default indicates that the repository layout is the default layout of Maven2 and Maven3, rather than the layout of maven1. The Maven1 layout is basically not used.
Others:For releases and snapshots, apart from enabled, they also contain two sub-elements, updatePolicy and checksumPolicy.
ElementUpdatePolicyUsed to configure how often Maven checks updates from a distant repository. The default value is daily, indicating that Maven checks updates once a day. Other available values include: never-never check for updates; always-check for updates each build; interval: X-check for updates every X minutes (X is any integer ).
ElementChecksumPolicyThis policy is used to configure the Maven check checksum file. When the build is deployed to the Maven repository, the corresponding check and file will be deployed at the same time. When downloading the component, Maven will verify the checksum file. If the checksum verification fails, when the checksumPolicy value is the default warn, Maven will output a warning when executing the build, other available values include: fail-Maven causes the build to fail when a checksum error is encountered; ignore-causes Maven to ignore the checksum error completely.
2. Remote repository Authentication
Most public remote repositories can be accessed directly without authentication. However, during normal development, we often set up our own Maven remote repository. For security considerations, we need to provide authentication information to access such remote warehouses. Different From configuring a remote repository, you can configure the authentication information directly in pom. xml, but the authentication information must be configured in the settings. xml file. This is because pom is often submitted to the code repository for access by all members, and settings. xml generally only exists on the local machine. Therefore, it is safer to configure authentication information in settings. xml.
1 <settings> 2 2... 3 3 <! -- Configure remote repository authentication information --> 4 4 <servers> 5 5 <server> 6 6 <id> releases </id> 7 <username> admin </username> 8 8 <password> admin123 </password> 9 9 </server> 10 10 </servers> 11 11... 12 12 </settings>
The above code configures a remote repository authentication information with the id of releases. Maven uses the servers element in the settings. xml file and its sub-element server to configure repository authentication information. The authentication username is admin and the authentication password is admin123. The key here is the id element. The server element id in settings. xml must be exactly the same as the id of the repository element to be authenticated in pom. xml. This id associates the authentication information with the repository configuration.
3. Deploy components to a remote warehouse
The purpose of using our own remote repository is to deploy the components of our own project and some components that cannot be directly obtained from the external repository. In this way, it can be used by other team members during development.
In addition to compiling, testing, and packaging a project, Maven can also deploy the components generated by the project to a remote repository. First, edit the project's pom. xml file. Configure the distributionManagement element. The Code is as follows:
1 <distributionManagement> 2 <repository> 3 <id>releases</id> 4 <name>public</name> 5 <url>http://59.50.95.66:8081/nexus/content/repositories/releases</url> 6 </repository> 7 <snapshotRepository> 8 <id>snapshots</id> 9 <name>Snapshots</name>10 <url>http://59.50.95.66:8081/nexus/content/repositories/snapshots</url>11 </snapshotRepository>12 </distributionManagement>
DistributionManagement contains the repository and snapshotRepository sub-elements. The former indicates the repository of the released version (stable version) component, and the latter indicates the repository of the snapshot version (Development test version. The id, name, and url must be configured for both elements. id is the unique identifier of the remote repository. name is used to facilitate reading. The key url represents the address of the repository.
When deploying components to a remote warehouse, authentication is often required. The configuration authentication method is the same as above.
After the configuration is correct, run the command mvn clean deploy, and Maven will deploy the components built and output by the project to the remote repository corresponding to the configuration. If the current version of the project is a snapshot version, the repository address of the snapshot version. Otherwise, the repository address of the released version is deployed.
For details about the differences between the snapshot version and the released version, refer to the data on a hundred-level basis.
4. Configure the remote repository Image
If warehouse X can provide all the content stored in warehouse Y, it can be considered that X is an image of Y. In other words, any component that can be obtained from Warehouse Y can be obtained from its image. For example, http://maven.oschina.net/content/groups/public/ is a central warehouse http://repo1.maven.org/maven2/ image in China, which is often able to provide faster services than a central warehouse due to geographical location. Therefore, you can configure Maven to use this image to replace the central repository. Edit settings. xml with the following code:
1 <mirrors>2 <mirror>3 <id>maven.oschina.net</id>4 <name>maven mirror in China</name>5 <url>http://maven.oschina.net/content/groups/public/</url>6 <mirrorOf>central</mirrorOf>7 </mirror>8 </mirrors>
In this example, if mirrof is set to central, it indicates that the image is configured as a central warehouse. Any request to the central warehouse is forwarded to this image, you can also configure images of other repositories in the same way. Id indicates the unique identifier of the image, name indicates the image name, and url indicates the image address.
A more common use of images is to combine private servers. Because private servers can represent any external public warehouse (including the central warehouse), for Maven users in the Organization, using a private server address is equivalent to using all the required external warehouses, this allows you to centralize configurations to private servers to simplify Maven configurations. In this case, any required component can be obtained from the private server, which is the image of all warehouses. You can configure an image as follows:
1 <! -- Configure private server image --> 2 <mirrors> 3 <mirror> 4 <id> nexus </id> 5 <name> internal nexus repository </name> 6 <url> http: // 183.238.2.182: 8081/nexus/content/groups/public/</url> 7 <mirrorOf> * </mirrof> 8 </mirror> 9 </mirrors>
In this example, the value of <mirrof> is an asterisk, indicating that the configuration is the image of all Maven repositories. Any requests to remote repositories are forwarded to http: // 183.238.2.182: 8081/nexus/content/groups/public /. If the image repository requires authentication, configure an authentication information with the id of nexus.
It should be noted that, because the image repository is completely shielded from the image repository, Maven will still be unable to access the image repository when the image repository is unstable or the service is stopped, therefore, the component cannot be downloaded.
5. Available Maven image repository
1 <mirror> 2 <id>repo2</id> 3 <mirrorOf>central</mirrorOf> 4 <name>Human Readable Name for this Mirror.</name> 5 <url>http://repo2.maven.org/maven2/</url> 6 </mirror> 7 8 <mirror> 9 <id>ui</id> 10 <mirrorOf>central</mirrorOf> 11 <name>Human Readable Name for this Mirror.</name> 12 <url>http://uk.maven.org/maven2/</url> 13 </mirror>14 15 16 <mirror> 17 <id>ibiblio</id> 18 <mirrorOf>central</mirrorOf> 19 <name>Human Readable Name for this Mirror.</name> 20 <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> 21 </mirror>22 23 <mirror> 24 <id>jboss-public-repository-group</id> 25 <mirrorOf>central</mirrorOf> 26 <name>JBoss Public Repository Group</name> 27 <url>http://repository.jboss.org/nexus/content/groups/public</url> 28 </mirror>29 30 <mirror> 31 <id>JBossJBPM</id> 32 <mirrorOf>central</mirrorOf> 33 <name>JBossJBPM Repository</name> 34 <url>https://repository.jboss.org/nexus/content/repositories/releases/</url> 35 </mirror>
The above warehouse is accessible after testing.
6. repository search service address
Sonatype Nexus: https://repository.sonatype.org/
MVNrepository: A http://mvnrepository.com/
For dependency search, I think these two are the best.
Conclusion: To get it, you must pay and learn to stick to it. If you really find it difficult, you should give up, but if you give up, don't complain, the world is truly balanced. I think this is how life is like. Everyone decides their way of life through their own efforts.