In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about what is Maven. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Preface
In Java EE development, we use the Maven build tool to manage the dependencies of third-party libraries of the project, as well as other project services within the company. Therefore, Maven private service is an indispensable part.
What is Maven private server?
Before introducing Maven, let's first make clear what a Maven warehouse is.
Maven repository: Maven uniformly stores all Maven project dependencies, plug-ins, and uploaded projects.
And Maven warehouses are divided into two categories: local warehouses and remote warehouses.
The local warehouse is the folder where localRepository needs to specify the directory in the configuration file setting.xml after we install Maven on our local computer.
Remote repository, that is, a server designated by URL to store Java libraries, Maven plug-ins, etc., in an external network (including local area network), such as a central warehouse, is a typical remote repository, which contains most of the world's open source Java libraries, as well as many other public libraries.
The private server mentioned in this article is another special remote warehouse, which is usually set up on a server in the local area network, which is used to represent all external remote warehouses, and can accept the upload and access of internal projects.
And what is the use of these warehouses? When a Maven project needs to rely on the Java libraries of some other projects, Maven will first look for it in the local warehouse. If there is a corresponding library, use the If the local warehouse does not exist, Maven will go to the remote warehouse to find the corresponding Java library and download it to the local warehouse for re-use, so that the next time you need the Java library, you can directly use the dependent library cached in the local warehouse, saving the time of repeatedly searching and downloading through the network, and it is the same process for Maven plug-ins.
In general, the central warehouse is the default remote warehouse of Maven. When private service is introduced, when the local warehouse finishes searching and cannot find the required dependent database, the search starts from the private server warehouse. If it is not found yet, the central warehouse is searched at last. For more information, please see the figure below:
Here let's take a look at the functions of private services:
Access to the internal network to save the bandwidth of the external network.
Once downloaded from the public network, all users in the internal network can only download the private server cache to speed up the construction of the Maven project.
It is more secure to allow private libraries to be uploaded and downloaded without external access.
Reduce external network factors and provide stability of project construction.
Facilitate dependent references to internal project services without the need for complete source code for other projects.
Here we add a specific description of the function of the last little point: when we have two independent Maven projects, such as the order service project and the member service project, in which the order service project requires member service and relies on the API package of the member service, the compiled API package of the member service can be uploaded through the private service, and then the order service program can download and reference the member service API package on the private service directly. In this way, there is no need to import the code of the member service project, and there is no need to care about the specific implementation of the member service, which plays the role of a slight reference to the internal service project. For a description, please see the figure below.
Set up Maven private server
After learning about Maven, we learn further. Let's first build a Maven private server. Here we use the most popular open source Maven warehouse management software-Nexus to quickly build Maven private servers. The traditional way of building is to download the open source version of Nexus OSS on the official website of Nexus: https://www.sonatype.com/nexus-repository-oss. In this paper, Docker will be used to install Nexus, which is not only fast and simple, but also takes up less machine resources.
If you are not familiar with Docker, you can see 10 minutes to quickly master the basic knowledge of Docker. It is also very easy to use Docker simply.
Download Nexus image
Use the Docker command on the terminal console to download the official Docker image provided by Nexus:
When you see the result of the following figure, it means that the image download is complete and can be viewed through docker images.
Start the Nexus container
Use the following instructions to start the Nexus container:
Here is a simple description of the parameters of the entered Docker instruction:
-d means to let the container run in the background.
-p 8081 8081 indicates that the container opens internal port 8081 (8081 of the latter) to map 8081 of the host port at startup, that is, the services provided by the Nexus container can be accessed through localhost:8081.
-v / Users/One/Desktop/Nexus/nexus-data:/nexus-data means to mount the internal / var/nexus-data of the container to the specified directory of the current host. Note that the external path specified by-v must be the full path.
-- restart=always is relatively simple, which means that when the docker service starts, the container starts with it.
After executing the above instructions, wait a moment, visit the http://localhost:8081/ and see the corresponding Nexus backend to indicate that the installation of the private server is complete. Is it very simple?
During the container startup process, slow startup may occur due to different machine configurations. You just need to wait patiently.
After the startup is complete, we can view the files generated by the operation of the Nexus container in the local directory where we are mounted.
Configure Maven private server
After installing the private server, we first log in, click the Sign In button in the upper left corner of the page, and the prompt is the same as the figure below:
It shows that the default login account of Nexus is admin, and the password is located in the file / nexus-data/admin.password. We only need to view the file in the locally configured mount directory.
After entering the default account password and logging in successfully, Nexus will be forced to change the admin password and set basic access permissions, and then officially enter the Nexus private server backend.
Here is a brief introduction to the interface elements:
The default browsing interface allows you to search and view components of the warehouse, as well as upload operations.
Used to manage the configuration of private server programs.
Check the account information and allow you to change the password.
To enter the configuration interface of the private server program, we need to make several adjustments to the default configuration.
The proxy path setting of the central warehouse
Update the proxy warehouse path of the central warehouse in the private server configuration to the proxy address provided by Aliyun warehouse: https://maven.aliyun.com/repository/central so that you can more quickly access the dependencies and plug-ins needed on the central warehouse.
Create a custom warehouse
Click Create repository, and then select type maven2 (hosted)
Then enter the warehouse name private-release, and select Release in the Version policy column to indicate that the storage dependency of the repository is the officially released component, and then select Allow redeploy in the Deployment policy column to allow deployment and update of the components on the private server.
Finally, after clicking the blue button-Create repository, you can see the custom warehouse in the warehouse list. With release warehouse, we can add a snapshot warehouse in the same way, just adjust to Snapshot in the Version policy column.
Add Rol
By default, there are only two roles for Nexus: nx-anonymous and nx-admin, the former with browsing rights and the latter with administrator permissions. In general, we also need to create a role for developers. Click Create Role, add a custom role with Role ID as developer, and add only the permission to use the custom repository, except for the delete operation.
After saving, the new roles are displayed in the list, and with the roles, you can associate users and assign permissions.
Add user
Similar to permissions, there are only two default users: admin and anonymous, and we also need to create user objects that belong to developers. Click Create local user, fill in the required information such as user name and password, associate with our previously created role, and save it.
After the user is created, we can log in to the private server with the new user and view the content of the corresponding permissions. For example, if we log in with the new user, all we can do is to view and search the custom private server repository.
In this way, we have created our own private server repository, and after the configuration is complete, developers can use it in the development of Maven projects.
Use Maven private server
With the private server and the account for development, we need to associate it in the local Maven configuration file setting.xml.
Set server account information:
Set the address of the private server warehouse: (the address here can be automatically copied from the cpoy button of the warehouse page on the Nexus backend)
Maven project is deployed to private server.
After the configuration is completed, we can create a Maven project and try to package it to Maven private server. Quickly generate a Maven project using IDE, add a distributionManagement node in the POM file, and specify the corresponding private server repository id and address, as follows:
Finally, you only need to execute the deployment command mvn clean deploy or use the deployment plug-in of IDE. When BUILD SUCCESS appears in the console, the deployment is completed when the figure is similar to the following figure.
As can be seen from the log, our project has been uploaded to the private- snapshot warehouse. At this time, we can look up the uploaded jar in the private- snapshot warehouse on the private server website.
If the following prompt appears during the deployment process: with Access denied to, the user configured in setting.xml is not authorized enough to deploy jar to the corresponding private server repository.
It should be noted that for a Maven project, if there is the word "SNAPSHOT" in the version number of the project, it means that you are currently in the development version, and Maven will publish it to the corresponding address of the snapshotRepository node. Otherwise, Maven believes that this is a release version and publishes the program to the address corresponding to the repository node. Because the version of the sample project is 1.0.0-SNAPSHOT, the final project is uploaded to private-snapshot, a repository of type Snapshot.
Next, if other projects rely on this jar, you just need to introduce the corresponding coordinates in their POM file.
Deploy third-party jar packages to Maven private servers
Here we talk about the third-party jar package, which is not the open source library mentioned commonly, but more jar packages from other third-party systems. Due to the needs of the project, it is very inconvenient to use the project import method. A better way to deal with it is to manually upload the jar package of the third-party system to the Maven private server, and the items you need to use can be pulled directly from the private server warehouse.
This method is also convenient to implement, which can be divided into two types: visual interface operation and command line operation.
Visual interface operation: when you enter the Nexus backend, you must have the upload permission for the upload operation. Generally, the administrator account performs the operation. Select the warehouse, enter the upload page, specify the projects that need to be uploaded locally, and save the coordinate information after customization.
Command line mode: enter the following instructions directly in the terminal:
-DgroupId and-DartifactId constitute the pom.xml coordinates of the jar package, that is, the corresponding dependent groupId and artifactId
-Dfile indicates the absolute path of the jar package to be uploaded
-Durl is the exact url address of the private server warehouse.
-id defined by DrepositoryId as a private server repository
-Dversion specifies the version number
-Dpackaging specifies the packaging method
When the BUILD SUCCESS message appears in the console log, the package is successful. If the packaging fails, it is likely due to insufficient user permissions under the server element in the Maven configuration file, which requires the Nexus backend to assign upload permissions to the user roles.
In the Java enterprise project development, the establishment and maintenance of private services is an indispensable step to use Maven. This paper starts from a brief introduction of Maven to rapid construction and use to further master Maven private services, laying the groundwork for the follow-up enterprise micro-service architecture.
The above is what the editor shares with you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.