preface

In Java EE development, we use the Maven build tool primarily to manage the dependencies of third-party libraries for the project, as well as other project services within the company. Therefore, Maven private server is indispensable. This article mainly introduces three aspects of Maven private server:

  • What is Maven Private server
  • How to set up Maven private server
  • How do I use Maven Private server

This article is intended for readers who have experience with Maven and are familiar with some of the basic concepts of Maven. If not, refer to the resources at the end of this article.

What is Maven Private server

Before introducing Maven private server, let’s first clarify what Maven repository is.

Maven repository: The place where Maven stores all Maven project dependencies, plug-ins, and uploaded projects.

Maven repositories also fall into two categories: local and remote.

  • The Maven repository is installed in the configuration file setting. XMLlocalRepositoryThe folder where you need to specify the directory.
  • A remote repository is a server dedicated to storing Java libraries, Maven plug-ins, etc., with urls specified on the external network (including the LAN). For example, a central repository is a typical remote repository, which contains most of the open source Java libraries in the world, as well as many other public libraries

The private server mentioned in this article is another special kind of remote warehouse, which is usually set up on a server in the LAN, used to proxy all external remote warehouses, and can accept internal project upload and access.

And what are these warehouses for? When a Maven project needs to rely on Java libraries from other projects, Maven first looks for them in the local repository and uses them if they exist. If the local warehouse does not exist, I would have gone to remote Maven warehouse search, find the corresponding Java library download to a local warehouse after use, so that the next time we need to the Java library, direct use of dependent libraries can be cached in the local warehouse, eliminating the repeated search and download time, through the network for the Maven plugin is also the same process.

The central repository is the default remote repository for Maven. If the local repository does not find the required repository, the central repository will be searched from the private repository first.

Here we take a look at the role of the private server:

  • Intranet access, saving Internet bandwidth.
  • Once downloaded from the Internet, all Intranet users can download only the private server cache, speeding up Maven project construction.
  • Allows private libraries to be uploaded and downloaded without external access for greater security.
  • Reduce external network factors and provide stability for project construction.
  • Facilitate dependency references to internal project services without requiring complete source code for other projects.

Here’s a description of what the last bit does: When we have separate two Maven project, such as order service projects and membership service project, including the order services need membership service, relying on the membership service API package, through private servers can be compiled membership service API package to upload, then order service program directly download the reference private servers on the membership service API package. In this way, there is no need to import the code of member service project, and there is no need to care about the specific implementation of member service. It plays the role of mild reference of internal service project, which can be described in the following figure.

Build Maven private server

Now that we know about Maven private, we’ll learn more. Let’s start by building a Maven private server. Here we use the most popular open source Maven repository management software, Nexus, to quickly build Maven private server. The traditional way to build Maven is to download the open source Version of Nexus OSS from the Nexus official website. www.sonatype.com/nexus-repos… Docker Nexus installation is not only fast and easy, but also consumes less machine resources.

If you are not familiar with Docker, you can learn the necessary basic knowledge of Docker in 10 minutes. It is also very easy to use Docker simply.

Download the Nexus Image

Use Docker command to download Docker image from Nexus:

The image download is complete when you see the result below, which can be viewed via Docker Images.

Start the Nexus container

Start the Nexus container with the following command:

Here is a brief description of the parameters of the input Docker command:

  • -d enables the container to run in the background.

  • -p 8081:8081 indicates that the internal port 8081 (the latter 8081) maps the host port 8081 when the container is started. That is, localhost:8081 can access the services provided by the Nexus container.

  • – v/Users/One/Desktop/Nexus/Nexus – data: / Nexus – said the data within the container/var/Nexus – data is mounted to the current host specified directory, it is important to note that the -v external path must be specified for the full path.

  • –restart=always: when the docker service is started, the container is also started.

After executing the above command, wait a moment, visit http://localhost:8081/, see the corresponding Nexus background means that the private server installation is complete, isn’t it very simple?

During the container startup process, the startup may be slow due to different machine configurations. You only need to wait.

Once the startup is complete, you can view the files generated by running the Nexus container in the mounted local directory.

Configure the Maven private server

After the private server is installed, we first log In, click the Sign In button In the upper left corner of the page, and the prompt like the following will appear:

The default Nexus login account is admin, and the password is in the file /nexus-data/admin.password. You only need to view the file in the locally configured mount directory.

After entering the default account password and successfully logging in, the Nexus will force you to change the password of admin and set the basic access rights. After completing this, you will officially enter the Nexus private server background.

Here is a brief introduction to interface elements:

  1. The default browsing interface allows you to search for and view components in the repository, as well as upload operations.
  2. Used to manage the configuration of private server applications.
  3. View account information, allowing you to change passwords.

Enter the configuration interface of the private server program, we need to make some adjustments to the default configuration.

Proxy path Settings for the central repository

Will private servers configured in the central warehouse agent path update agent for ali cloud warehouse address: maven.aliyun.com/repository/… This allows faster access to dependencies and plug-ins needed on the central repository.

Create a custom warehouse

Click Create Repository and select Type maven2(Hosted)

Then enter the repository name private-release and select Release in the Version Policy column to indicate that the repository’s storage depends on officially published components. Then select Allow Redeploy in the Deployment Policy column. Allows components to be deployed and updated on private servers.

After clicking the blue button -Create Repository, you can see the custom repository in the repository list. With the release repository, you can add a snapshot repository in the same way. Simply change to Snapshot in the Version Policy column.

Adding roles

By default, the Nexus has only two roles: nX-anonymous and nX-admin. The former has only browsing privileges, and the latter has administrator privileges. Normally, we also need to create a role for the developer. Click Create Role to add a custom Role whose Role ID is Developer, and only add permissions to the custom repository, except for deletion.

After saving, the new role is displayed in the list, and the role can be associated with users and assigned permissions.

Add user

Similar to permissions, there are only two default types of users: admin and Anonymous, and we also need to create user objects belonging to developers. Click Create Local User, fill in the username, password and other required information, associate with our previously created role, and save.

After the user is created, we can log in the private server with the new user and view the content of the corresponding permissions. For example, we can log in with the new user and only view and search the customized private server warehouse.

In this way, we have created our own private server repository, which can be configured and used by developers in Maven projects.

Use Maven private server

With a private server and an account for development, we need to associate it with the local Maven configuration file setting.xml.

Setting server account information:

Set the private server warehouse address :(the address here can be copied automatically through the cpoy button of the warehouse page on the Nexus backend)

Maven project deployed to private server

After the configuration is complete, we can create a Maven project and try to package it into Maven private server. Add a distributionManagement node to the POM file and specify the corresponding private server repository ID and address as follows:

Finally, just run the MVN Clean deploy command or use the IDE deployment plug-in. When BUILD SUCCESS appears on the console, the deployment is complete.

It can be seen from the log that our project was uploaded to the private-snapshot repository. At this time, we can view the uploaded JAR in the private-snapshot repository on the private server website.

If the message “Access denied to” is displayed during the deployment, the user permission configured in setting. XML is insufficient and the JAR cannot be deployed to the private server repository.

Note that for a Maven project, if the version number of the project contains “SNAPSHOT”, it means that the project is in development and Maven will publish to the address of the snapshotRepository node. Otherwise, Maven considers this a release and publishes the program to the repository node address. Since the version of the sample project is 1.0.0-snapshot, the final project is uploaded to a SNAPSHOT type repository called private-snapshot.

Then, if other projects rely on this JAR, they simply import the corresponding coordinates in their POM files.

Deploy third-party JAR packages to Maven private server

There are jar packages from other third-party systems, not open source libraries, but jars from other third-party systems. It is not convenient to use the project import method because of the project needs. The better solution is to manually upload the jar packages from third-party systems to Maven private server. The items you need to use can be pulled directly from the private server warehouse.

This method is also easy to implement, divided into two types: visual interface operation and command line operation.

Visual interface operation: it is to enter on the back of the Nexus, and upload operation must be performed only with the correct upload permission. Generally, the administrator account is used for operation. After selecting the warehouse, enter the upload page, specify the local project to be uploaded, and customize the coordinate information to save.

Cli mode: Directly enter the following command on the terminal:

  • -dgroupid and -dartifactid form the coordinates of the JAR package in PUM.xml, that is, the corresponding dependenciesgroupIdartifactId
  • DfileIndicates the absolute path of the JAR package to be uploaded
  • -Durl indicates the exact URL of the private server warehouse
  • -DrepositoryId Specifies the ID of the private server repository
  • -Dversion Specifies the version number
  • -Dpackaging Specifies the packaging method

When BUILD SUCCESS is displayed in the console log, it indicates that the package is successfully packaged. If the package fails, it is likely that the user permissions under the Server element in the Maven configuration file are insufficient, and the Nexus background needs to assign upload permissions to the user roles.

conclusion

In Java enterprise project development, the establishment and maintenance of Maven private server is an essential step to use Maven. This paper, from a simple introduction of Maven, to a quick construction and use to further master Maven private server, paving the way for the subsequent enterprise micro-service architecture.

Recommended reading

  • Learn Spring Boot Profiles
  • How to gracefully close the Spring Boot application
  • Need interface management you know?
  • Java Lombok is a must
  • Nacos for the next generation of Java microservices

The resources

  • To get started, read this article: juejin.cn/post/684490…

  • Maven of actual combat: book.douban.com/subject/534…

  • Private servers maven command warehouse upload jar package: www.jianshu.com/p/ccd7b6436…

  • What is a Maven Repository:medium.com/@chris.shel…