Establishment and application of Maven private server based on Nexus

I summary

Maven private service is a special Maven warehouse. Through private servers, enterprises / individuals can build a private dependency warehouse to store the dependent jar packages, and even store the internal dependencies separately to avoid disclosure to the public network. Finally, it only needs to be downloaded once through the private server without downloading many times. When multiple people cooperate, they can download directly on the private server through the intranet, so as to save some resources. The schematic diagram is as follows:

Therefore, the function of Nexus is as follows:

  • Similar to the central warehouse, the dependency is directly obtained through the warehouse

  • It is convenient to manage the internal dependence of the company without transmitting to the external network

  • Save resources and use it internally as a maven shared server

At present, there are several kinds of private server building software to choose from, including Apache's Archiva, JFrog's artifacotor, and Sonatype's Nexus. This article will build Maven private server based on Nexus

II Construction and Application

1. Nexus Download

Download Nexus3 from the official website at: https://help.sonatype.com/repomanager3/download

Select Unix and click the link to download

Note: it is difficult to download the Nexus official website by climbing over the wall. Some bloggers have sorted out the latest download address and posted it here. It is necessary to jump directly to the past

Portal: Click me to jump to download

2. Deployment

## 1. Create nexus directory
mkdir -p /data/nexus
## 2. Unzip the jar package
cd /data/nexus
tar -zxvf nexus-3.13.0-01-unix.tar.gz
## 3. Modify the nexus startup port (optional), modify the line application port = 8085, and then wq save it
vim sonatype-work/nexus3/etc/nexus.properties
## 4. Start nexus
cd /data/nexus/nexus-3.13.0-01/bin
./nexus start
## 5. Check the startup status
./nexus status

See the following figure to prove that the maven private server has been started

Accessed through ui interface, address: http://ip:8081 , as shown below, proves successful deployment

Nexus usage notes:

Default port: 8081

Default suffix:/

Default user: admin/admin123

3. Application

After installing Maven private server, the specific application scenarios of Nexus are described below

<1> Nexus configuration dependent warehouse

Nexus configures an agent central warehouse for us by default: https://repo1.maven.org/maven2/

However, we can also specify a domestic warehouse to improve the download speed. The following describes how to add a domestic agent warehouse

Log in through Sign in and enter the account password (admin/admin123 by default)

Click the gear to set

Click Create warehouse

Select maven2 (proxy) and fill in the following parameters:

Names: aliyun-repository

URL: http://maven.aliyun.com/nexus/content/groups/public/


After creation, select Maven public and add aliyun repository to the public warehouse

Since then, the Alibaba cloud warehouse has been added to the Nexus private server

<2> Dependent Download

There are many ways to rely on download. You can choose project configuration and global configuration. Project configuration is mainly specified through pom file, while global configuration is specified by modifying Maven's settings file. Take the second method as an example. The final settings file is modified as follows:

<mirrors>
	<mirror>
		<id>maven-public</id>
		<name>nexus maven</name>
		<url>http://ip: port / repository / Maven public / < / url >
		<mirrorOf>*</mirrorOf>        
	</mirror>
</mirrors>
<profiles>
  <profile>
    <id>nexus-repository</id>
    <repositories>
		<repository>
			<id>maven-release</id>
			<name>Releases Repository</name>
			<url>http://ip: port / repository / Maven releases / < / url >
			<layout>default</layout>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>maven-snapshots</id>
			<name>Snapshot Repository</name>
			<url>http://ip: port / repository / Maven snapshots / < / url >
			<releases>
				<enabled>false</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
    </repositories>
  </profile>
</profiles>
<activeProfiles>
	<activeProfile>nexus-repository</activeProfile>
</activeProfiles>

It is specified mainly through three configurations:

mirrors: the image download address, which points to the previously configured public warehouse (it points to all other warehouses)

Profile: Nexus profile, which is activated through activeProfiles

activeProfiles: used to activate the previously configured profile

<3> Dependent upload

1 ~ configure settings file

It is configured through the server module. User permissions are required for uploading. Note that the id is the same as that of the repository

  <servers>
	<server>
      <id>maven-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
	<server>
      <id>maven-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
2 ~ specify the upload address of POM file

In the actual project, the pom file configuration upload address is as follows:

    <distributionManagement>
        <repository>
            <id>maven-releases</id>
            <url>http://ip: port / repository / Maven releases / < / url >
        </repository>
        <snapshotRepository>
            <id>maven-snapshots</id>
            <url>http://ip: port / repository / Maven snapshots / < / url >
            <uniqueVersion>true</uniqueVersion>
        </snapshotRepository>
    </distributionManagement>
3 ~ upload

Upload through mvn deploy. idea can click deploy through Maven management console

<4> Authority management

Nexus private server implements permission management, which is used for legal permission allocation of Users. Click Users to see that two Users are set by default


Click admin user, and you can see that a user authority is assigned: NX admin

For NX admin permission, NX all (that is, all permissions) is configured in Roles

Therefore, by configuring Users and Roles and realizing the binding relationship between Users and Roles, user fine-grained permission management can be realized

III summary

Finally, through Maven private server, we realized:

  • Unified management of project dependencies: unified management of external dependencies and internal dependencies

  • Security: internal dependency. Upload to private server to ensure necessary security

  • Efficiency: when downloading external dependencies on the intranet, you only need to download them on the private server once, which not only saves bandwidth, but also improves the download speed

Added by webstyler on Thu, 23 Dec 2021 06:06:08 +0200