A basic introduction to building a Maven private server based on Nexus

What is Nexus? What are its advantages?

To understand why nexus is needed, we might as well take a brief look at the following questions:

Why do you need to build a private server? What problems will occur if there is no private server?

For enterprise development, if there is no private server, all our dependencies need to be downloaded locally from Maven's central warehouse or a third-party Maven warehouse. If everyone in a team repeatedly downloads components from the Maven warehouse, it will undoubtedly increase the load on the warehouse and consume bandwidth. Moreover, if the enterprise's bandwidth resources are tight, introducing dependency waiting times will be even more disastrous.

In addition, in many cases, project development is carried out on the internal network, and it is impossible to publish some of our components to the external network, so we may need a private Maven server of our own.

What are the advantages of building a private server?

The concept of Maven private server is to set up a Maven warehouse server locally to maintain the local warehouse while proxying the remote warehouse. When we need to download some artifacts (artifact), if the local warehouse does not have it, then go to the private server to download it. If the private server does not go to the central warehouse to download it (such as Alibaba Cloud warehouse) and cache it, so that the next user can directly download it in the The local private server gets the resources it needs, which avoids unnecessary bandwidth consumption.

Insert image description here

To summarize, this private server-dependent deployment method has the following advantages:

  1. Reduce network bandwidth traffic.
  2. Speed ​​up Maven builds.
  3. Facilitate access to third-party privacy components.
  4. Improve stability and enhance control.
  5. Reduce load on central warehouse.

What is Nexus?

Nexus is a warehouse management software that specializes in managing Maven, npm and other warehouses. It can not only quickly build private servers, but also has the following advantages, making it increasingly the most popular Maven warehouse manager:

  1. Provides powerful warehouse management functions and component search functions.
  2. It is based on REST and the friendly UI is an ext.js REST client.
  3. It takes up less resources.
  4. Based on a simple file system rather than a database.

Building nexus based on Linux

Prepare nexus

After understanding nexus, we might as well build our own nexus private server based on Linux server. First we need to download the nexus resources from the nexus official website. The download address is:https://help.sonatype.com/repomanager3/product-information/download

I need to add here that the author is not using the latest version of nexus, so I found the resource nexus-3.28.0-01-unix.tar.gz in the old version. The address is:https://help.sonatype.com/repomanager3/product-information/download/download-archives—repository-manager-3

The selected version is shown below:

Insert image description here

After we've finished downloading the resource, we might as well upload it to the server. Taking the author as an example, uploading it to the tmp directory means an experimental deployment.

Insert image description here

Once the upload is complete, let’s unzip it first.

 tar -zxvf nexus-3.28.0-01-unix.tar.gz

Then enter the bin directory of nexus.

cd nexus-3.28.0-01/bin/

Run the following command to start nexus (in order to facilitate troubleshooting, the author first starts nexus frontend)

./nexus run

Soon after, the console outputs the following content, indicating that nexus has started successfully. We might as well go to the browser to visit this page.

Insert image description here

Start nexus and test usability

The default port number of nexus is 8081, so we go to the browser and type ip:8081 to enter the login page. It needs to be initialized when we first enter. Let's wait a moment here.

Insert image description here

After completing the initialization, we click sign in and prepare to log in.

Insert image description here

When entering for the first time, you need to enter your user name and password. The user name is admin. Readers can follow the page prompts to find the corresponding file and complete the input, as shown in the figure below:

Insert image description here

We might as well use the cat command to check it, as shown below, get a string and copy it to log in.

[root@iZ8vb7bhe4b8nhhhpavhwpZ ~]# cat /tmp/sonatype-work/nexus3/admin.password
6f1d326f-b030-4043-ab37-xxxxxxx

Click to log in with the password copied above.

Insert image description here

Then we enter the initialization page, click next directly here.

Insert image description here

Then the page will prompt us to change the password. Here we enter it twice as prompted and click Next.

Insert image description here

On the next settings page, we set up to allow anonymous access.

Insert image description here

This completes all basic configuration.

Configure nexus

Next, we start the configuration steps of our nexus. If we want the dependencies of the private server cache to be specified in a location, we can create a blob ourselves. This is actually not necessary. After the blob configuration is completed, we can add the subsequent warehouse The blob is assigned to this blob. For the sake of simplicity, the author does not configure it here and uses default directly. As shown in the figure below, the author will demonstrate the specific configuration of the blob in a subsequent series of tutorials.

Insert image description here

We hope that the workflow of nexus is to look for it in the local releases warehouse. If it is not available, go to the snapshots warehouse. If the snapshot warehouse is not available, go to Alibaba Cloud to find it. If Alibaba Cloud has it, cache it directly into the blob.

Insert image description here

In order to do this, we first need to configure an Alibaba Cloud warehouse. First click Create Warehouse

Insert image description here

Select maven-proxy to create a proxy warehouse. If the dependent resources are not found locally, this proxy will help us find them.

Insert image description here

As shown in the figure below, readers can set the name, version strategy, original warehouse address and other configurations according to the reader's explanation.

Insert image description here

When finished, click create below.

Insert image description here

After completing the creation of the proxy warehouse, we hope that our custom jars can be uploaded to release and snapshot on demand. For example, I have written a temporary snapshot component for other development purposes. I hope that I can upload it to snapshot. In the warehouse, such classified specifications facilitate the control of subsequent team packages.

So we first configure a snapshot warehouse. During the configuration process, we found that nexus provides a ready-made warehouse. We might as well modify its configuration and use it directly. As shown in the figure below, find maven-snapshots and click the arrow to enter the modification interface.

Insert image description here

When we click in, we can see that its policy is that we only need to store the snapshot package, and then we change the layout policy to permissive.

Insert image description here

Search below and change the deployment policy to allow repeated deployment to prevent us from being able to upload the same version of components to the maven private server again.

Insert image description here

When finished, click save below to save.

Insert image description here

In the same way, maven-release has the same configuration, so I won’t go into details here.

Insert image description here

Since then, we have completed most of the work. Next, we will implement our private server search jar package strategy, namely:

  1. Let’s take a look at the release first, without looking at the snapshots.
  2. If there are no snapshots, go to the agent warehouse to find them.
  3. It is available in the proxy warehouse and cached directly into the blob.

To do this, we still need to create a warehouse, combine the warehouses we configured above, and expose them to the outside world.

Insert image description here

Choose to configure a maven-group and combine the warehouses we configured above in order.

Insert image description here

After setting the group name, we slide down to the bottom to set the dependency search order. Taking the author as an example, you can see that the strategy is configured from top to bottom in the way we mentioned above. After completion, click save.

Insert image description here

Test getting resources from nexus private server

Since then, we have completed the configuration of nexus. Now we might as well try to see whether the nexus private server configuration takes effect. Let's configure maven's setting.xml on our own machine.

First, specify the local warehouse address as E:/repository, so that you can later check whether the private server download is successful.

 <localRepository>E:/repository</localRepository>

Then we need to configure the address of the maven private server, so we go to the nexus page to find the group we configured and click copy.

Insert image description here

At this time, the value of this address will pop up on the page, and we will copy it.

Insert image description here

Then configure this private server address in the mirror to ensure that our subsequent dependencies are downloaded through this private server address, and this private server address is the combined address we configured above, and it will be strictly followed< a i=1>Search in order, the configuration is as follows:release仓库->snapshots仓库->阿里云仓库

 <mirrors>
  
  
   <mirror>  
        <id>nexus</id>  
        <name>nexus repository</name>  
        <url>http://192.168.43.98:8081/repository/my-group/</url>  
        <mirrorOf>central</mirrorOf>  
     
    </mirror>   

  </mirrors>

Additional explanation, if readers who need to download the snapshot package need to comment out the mirror and use the following configuration instead, the reason is very simple. During the test, the author found that maven does not allow pulling of the snapshot package by default, so we keep the id and url. value, use the following configuration instead to ensure that the snapshot package can be pulled normally.

<profile>
		<id>nexus</id>
		<repositories>
			<repository>
				<id>nexus</id>
				<url>http://192.168.43.98:8081/repository/my-group/</url>
				<releases>
					<enabled>true</enabled>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
	</profile>
	
	
	
	
  </profiles>
  
  <activeProfiles>
	<activeProfile>nexus</activeProfile>
</activeProfiles>

When using the private server, we need its account password, so above setting.xml we need to configure the server tag consistent with our mirror's ID to indicate the account and password values ​​of the private server.

 <servers>
  
 	
	<server>
      <id>nexus</id>
      <username>admin</username>
      <password>123456</password>
    </server>
  </servers>

The relationship between these two configurations is as follows, and they are related by ID.

Insert image description here

Then we create a spring boot project, and both maven and setting.xml are set to the maven we modified above. Open the project and click reimport. As shown in the figure below, you can see that our maven will download resources from the maven private server.

Insert image description here

After completion, check the warehouse we configured above, and you can find that the jar packages have been downloaded from the private server.

Insert image description here

Returning to the nexus management page, we can also see that nexus has cached all jars in the proxy warehouse.

Insert image description here

Remember what we mentioned above about caching jar packages into blobs? The default name of this blob is default. We might as well go to this directory to see what changes have been made to default.

cd /tmp/sonatype-work/nexus3/blobs/default/

Enter du to check the size, and you can see that the size changes to 90M. It can be seen that the dependencies of Alibaba Cloud's proxy warehouse have been cached.

[root@localhost blobs]# du -sh *
90M     default

Upload custom components

Next, let's test the upload of components. For this, we first write a maven project. Write a test class.

Insert image description here

Then we add the following configuration to the pom file of the mave project, which means: if it is a releases version, it will be passed to the URL corresponding to releases, if it is snapshots, it will be passed to the URL of snapshots.

<!--    如果是releases版本则传到releases对应的url,如果是snapshots则传到snapshots的url-->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.43.98:8081/repository/maven-releases/</url>
        </repository>

        <snapshotRepository>
            <id>snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.43.98:8081/repository/maven-snapshots/</url>
        </snapshotRepository>

    </distributionManagement>

In order to ensure that the source code can be downloaded, we also need to add the following configuration

<build>
        <plugins>
            <!--   要将源码放上去,需要加入此配置    -->
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Finally, specify our maven project version as 1.0, which means that this project is a release version, so it will be transferred to the release warehouse.

 <groupId>org.example</groupId>
    <artifactId>test-nexus</artifactId>
    <version>1.0</version>

To summarize, the location of each configuration is shown in the figure below. Readers can check it by themselves.

Insert image description here

Note that we specified two new URLs above, and we have to add the account passwords corresponding to their IDs in maven's setting.xml. The content is as follows:

 <server>
      <id>releases</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>123456</password>
    </server>
	
	<server>
      <id>nexus</id>
      <username>admin</username>
      <password>Z120194199</password>
    </server>
  </servers>

Screenshot below:

Insert image description here

After completion, we click clean and then click deploy.

Insert image description here

If deploy outputs the following result, the deployment is successful.

Insert image description here

We might as well go to nexus and click browse to select the release warehouse.

Insert image description here

You can see that our jar package has been uploaded.

Insert image description here

Similarly, we can also find this jar package through precise search in the maven column. The snapshot version jar release method is also the same, so I won’t introduce it here.

Insert image description here

Download custom components

In order to test whether the jar package is available, we might as well create a new maven project and try to introduce it. Here we might as well introduce how to get the coordinates of a custom component through nexus. First, click the arrow.

Insert image description here

Then click on the jar package

Insert image description here

Select usage, you can see the maven coordinates, we copy them.

Insert image description here

Then introduce it into a new project, as shown in the figure below

Insert image description here

Then wait for the project to continuously download dependencies from the private server.

Insert image description here

As time goes by, we can see that our own component is also downloaded successfully.

Insert image description here

This class can also be used normally in projects.

Insert image description here

Enter the source code and click Download to download normally.

Insert image description here

You can see that the source code is downloaded normally.

Insert image description here

Since then, our simple introduction to the Nexus private server has been completed.

Some common operations

Delete jar package

Sometimes we upload many unnecessary jar packages that occupy unnecessary memory, and we want to delete them from nexus. We can select the browser and then select the release repository.

Insert image description here

Find the jar package you want to delete and click Delete. Then the page will prompt that the deletion is performed in the background.

Insert image description here

Then we can see that the dependencies corresponding to this warehouse are empty.

Insert image description here

We go back to the local machine, delete the corresponding snapshot package, and try to pull it from the private server to see if it can be pulled. It can be seen that after the private server is deleted, the jar package cannot be pulled locally.

Insert image description here

Upload jar package

We might as well continue the previous operation to demonstrate the upload of the jar package. Click upload and select mave-release.

Insert image description here

Click this option to find the local jar package

Insert image description here

Then manually enter the coordinates and click upload

Insert image description here

Back to broswer, you can see that we uploaded this jar package.

Insert image description here

Pull it again, the synchronization is successful, and the basic entry of nexus is completed.

Insert image description here

references

Nexus builds Maven private server and uses private server

Linux installation nexus3 (with Linux compressed package)

linux configuration deployment nexus

How to manually upload SNAPSHOT files to Maven private server Nexus

Solve the problem that maven private server nexus cannot download the snapshot version package

Solution to the problem that Maven cannot download the SNAPSHOT package but can download the RELEASE package

Guess you like

Origin blog.csdn.net/shark_chili3007/article/details/123024505