Nexus Getting Started Guide

Source: Internet
Author: User
Tags maven central sonatype
Nexus Introduction

Nexus is the maven Repository Manager. If you use Maven, you can download the required components (artifact) from the maven central repository. However, this is generally not a good practice, you should set up a Maven repository server locally and maintain the local repository while maintaining the remote repository as a proxy to save bandwidth and time. nexus can meet this requirement. In addition, he provides powerful warehouse management and component search functions. It is based on rest, and the friendly UI is an extjs rest client, which occupies less memory, based on a simple file system rather than a database. These advantages have increasingly become the most popular Maven Repository Manager.

 

Download and install

You can download the latest version of nexus from the http://nexus.sonatype.org/downloads/, I use 1.3.0.

Nexus provides two installation methods: one is jetty-embedded bundle, which can be run directly as long as you have a JRE. The second method is war. You only need to simply publish it to a Web container.

 

Bundle Installation

ExtractNexus-webapp-1.3.0-bundle.zipTo any directory, suchD: \ dev_toolsAnd then open CMD and CD to the directory.D: \ dev_tools \ nexus-webapp-1.3.0 \ bin \ JSW \ windows-x86-32, RunNexus. bat. You will see the Nexus startup log, when you see "started SelectChannelConnector@0.0.0.0: 8081", it indicates that nexus started successfully, then open the browser, access http: // 127.0.0.1: 8081/nexus, you will see the following page:

To stop nexus, press Ctrl + C, and install nexus. BAT can be used to install nexus into a Windows service. Other scripts are used to start, stop, pause, resume, and uninstall the Nexus service.

 

War Installation

You need to have a Web container that can run. Here, taking Tomcat as an example, add the installation directory of TomcatD: \ dev_tools \ apache-Tomcat-6.0.18.First, we will downloadNexus-webapp-1.3.0.warRenameNexus. WarAnd then copyD: \ dev_tools \ apache-Tomcat-6.0.18 \ webapps \ nexus. WarAnd then start cmd, CDD: \ dev_tools \ apache-Tomcat-6.0.18 \ bin \
Directory, runStartup. bat. Everything is okay. Now you can open the browser to access http: // 127.0.0.1: 8080/nexus, and you will get the same interface.

 

Proxy external Maven Repository Login

To manage nexus, log in as an administrator, click Login in the upper-right corner of the page, and enter the default logon name and password: admin/admin123. After successful login, you will see a lot added in the left navigation bar:

Here, you can manage the repository, configure the nexus system, manage tasks, manage users, roles, permissions, view the RSS source of the system, and manage and view system logs. You will see that nexus has rich and powerful functions. In this article, I will only introduce some of the most basic management and operations.

 

Proxy Maven central repository

Click repositories in the left-side Navigation Pane. the main panel of the interface displays a list of all the repositories and warehouse groups. You can see that their type field values include group, hosted, proxy, and virtual. Here we do not care about virtual. We only introduce the other three types:

  • Hosted, a local repository. We usually deploy our own components to this type of repository.
  • Proxy, which is used to represent a remote public repository, such as a Maven central repository.
  • A repository group is used to merge multiple hosted/Proxy repositories. We usually configure Maven dependency repository groups.

From this we know that we need to configure a Maven central repository proxy. In fact, nexus has built-in Maven Central, but we need to do some configuration. Click Maven central in the repository list and you will notice that its policy is release. This indicates that it does not proxy the snapshot component of the remote repository. This is for some reason, the Snapshot version components of the remote repository are unstable and not controlled by you. Using such components may pose potential risks. Then we found that there are three tabs under the main panel: browse, configuration and mirrors. We click Configuration to configure. Now you need to care about two configuration items: "remote
The storage location is the address of the remote repository. For Maven Central, It is http://repo1.maven.org/maven2/##download remote indexes. As the name suggests, it indicates whether to download the remote index file. The default value of this field in Maven Central is false, this is to prevent a large number of nexus servers from consuming the bandwidth of the central warehouse (the central warehouse has a large number of components and its index file is also large ). Here we need to set it to true and then click Save. After the central repository index file downloaded by nexus, we can search all the components of the central repository locally. The configuration we just covered is shown as follows:

 

Add a proxy Repository

Here is another example. We want to proxy the public repository of sonatype at http://repository.sonatype.org/content/groups/public /. Follow these steps: click Add on the top of the repositories panel and select proxy repository. In the configuration section below, enter the following information: Repository ID-sonatype; repository name-sonatype repository; remote storage location-http://repository.sonatype.org/content/groups/public /. Repository
Policy. We do not want to proxy the snapshot component. The reason is described above. Click Save. The configuration page is as follows:

 

Manage local Maven Repositories

Nexus predefines three local warehouses: releases, snapshots, and 3rd party. Each of these three warehouses has a specific purpose. Releases is used to deploy our own release components, snapshots is used to deploy our own snapshot components, and 3rd party is used to deploy third-party components. some components, such as the JDBC driver of Oracle, we can't download it from a public repository, so we need to deploy it to our own repository.

Of course, you can also create your own local repository. The procedure is similar to creating a proxy repository. Click Add at the top of the Repository panel and select hosted repository, enter the ID and name in the configuration panel below. Note that you do not need to enter the remote repository address here. The repository type is the hosted that cannot be modified, and the repository policy, you can select release or snapshot as needed,

 

Manage Maven repository groups

The concept of repository group in nexus is not available in maven. In Maven's opinion, whether you are hosted or proxy or group, it is the same for me. I just follow the groupid, artifactid, version, and other information to ask you for components. To facilitate Maven configuration, nexus can combine multiple repositories, hosted, or proxy into one group. In this way, Maven only needs to depend on one group, you can use all the repository content contained in the group.

Nexus predefines the "Public repositories" and "public snapshot repositories" warehouse groups. The former merges all pre-defined release warehouses by default, and the latter merges all pre-defined snapshot warehouses by default. In the previous section, we created a repository named "sonatype repository" and now merged it into "Public repositories.

Click "Public repositories" in the repository list, and select the "configuration" tab below. In the configuration panel, drag "sonatype repository" in "avaiable repositories" on the right to "ordered group repository" on the left,

Creating a repository group is similar to creating a proxy and a hosted repository. Note that the format field must be set to "maven2" and add the repository you are interested in.

 

Search Component

Click the link in the repository of a large Maven repository and browse the path to find the components you are interested in. This is very depressing. Nexus provides component search based on Nexus-indexer. To search for a repository, whether it is hosted, proxy, or group, you must confirm that the index file exists. This is especially important for Agent repositories. Some remote repositories may have no indexes at all, so you cannot search for these agent repositories. Some remote repositories have very large remote indexes. For example, if the central repository reaches about 70 MB, it takes a lot of time to download the index for the first time. Therefore, you need to expect the search results to see the following files:

Once your nexus has local or remote repository index files, you can enjoy the Nexus component search function. Whether you log on or not, you can use keywords for fuzzy search. For example, I enter JUnit in the search box in the upper navigation bar on the left, and then click the search button, more than 500 pieces of JUnit-related component information are displayed on the right. For more information, you can also search by specifying the groupid, artifactid, and version, click "Advanced Search" in the navigation bar, and click the drop-down box in the upper left corner of all pages on the right, select "Gav search ". Enter JUnit: 4.4 here, and press Enter:

Select a search result. The "artifact information" panel is displayed at the bottom of the page. You can click "artifact" or "pom" to download the corresponding file, A Maven dependency configuration is displayed on the right of the Panel. You can directly copy the configuration to the maven pom, which is a very convenient feature.

Additionally, Nexus supports classname-based search. You only need to click the drop-down box in the upper-right corner of the search page, select "classname search", and enter the class name, I will not go into details here.

 

Configure Maven to use nexus

By default, Maven depends on the central repository. This is to make Maven out-of-the-box, but this is obviously incorrect. This will cause a large waste of time and bandwidth. Now that we have discussed how to install and configure nexus, We need to configure Maven to use the local nexus to save time and bandwidth resources.

We can configure the repository to Pom, but this is not a good practice in general. The reason is very simple. You need to repeat the configuration for all Maven projects. Therefore, here I put the repository configuration in $ user_home/. m2/settings. xml:

XML Code
  1. <Settings>
  2. ...
  3. <Profiles>
  4. <Profile>
  5. <ID> Dev </ID>
  6. <Repositories>
  7. <Repository>
  8. <ID> Local-nexus </ID>
  9. <URL> http: // 127.0.0.1: 8080/nexus/content/groups/public/</URL>
  10. <Releases>
  11. <Enabled> true </enabled>
  12. </Releases>
  13. <Snapshots>
  14. <Enabled> true </enabled>
  15. </Snapshots>
  16. </Repository>
  17. </Repositories>
  18. </Profile>
  19. </Profiles>
  20. <Activeprofiles>
  21. <Activeprofile> Dev </activeprofile>
  22. </Activeprofiles>
  23. ...
  24. </Settings>
<Settings>... <profiles> <profile> <ID> Dev </ID> <repositories> <repository> <ID> Local-nexus </ID> <URL> http: // 127.0.0.1: 8080/nexus/content/groups/public/</URL> <releases> <enabled> true </enabled> </releases> <snapshots> <enabled> true </enabled> </snapshots> </Repository> </repositories> </profile> </profiles> <activeprofiles> <activeprofile> Dev </activeprofile> </activeprofiles>... </Settings>

Because we cannot directly Insert the <repositories> element in settings. XML, we have compiled a profile, added a profile, and automatically activated the profile using the <activeprofile> element. The local-nexus repository points to the "Public repositories" repository group in the nexus We Just configured. That is to say, all the repositories in this repository group can be used by us. In addition, we use the <releases> and <snapshots> elements to activate Maven's support for downloading all types of component in the repository. Of course, you can also adjust this configuration, for example, Maven is prohibited from downloading the snapshot component from nexus.

With this configuration, MAVEN will download components from your Nexus server. The speed and download from central are not an order of magnitude.

 

Deploy components to nexus

Nexus provides two methods to deploy components. You can directly upload components from the ui or configure Maven deployment components.

 

Deploy through nexus UI

Sometimes you cannot find a jar file from the public Maven repository, but you can get the JAR file (or even POM) from other sources, so you can deploy the file to nexus, make it part of the standard process. The procedure is as follows:

Click "repository" in the left-side navigation bar and select a repository in the warehouse list on the right, for example, "3rd party". Then, four tabs are displayed at the bottom of the page, and the last "Upload" is selected ", you will see the component upload interface. Select the component you want to upload and specify POM (or manually write the GAV information). Then, Click Upload to deploy the component to the "3rd party" repository of nexus.

Deployment through Maven

A more common use case is that a team is developing various modules of a Project. In order to allow its self-developed modules to be quickly used by others, you will want to deploy the components of the snapshot version to the maven repository. Others only need to add a dependency on your development module in the POM to get the latest snapshot at any time.

The following Pom. xml configuration and settings. xml allow you to use Maven to automatically deploy components:

Pom. xml

XML Code
  1. <Project>
  2. ...
  3. <Distributionmanagement>
  4. <Repository>
  5. <ID> nexus-releases </ID>
  6. <Name> nexus release repository </Name>
  7. <URL> http: // 127.0.0.1: 8080/nexus/content/repositories/releases/</URL>
  8. </Repository>
  9. <Snapshotrepository>
  10. <ID> nexus-snapshots </ID>
  11. <Name> nexus snapshot repository </Name>
  12. <URL> http: // 127.0.0.1: 8080/nexus/content/repositories/snapshots/</URL>
  13. </Snapshotrepository>
  14. </Distributionmanagement>
  15. ...
  16. </Project>
<Project>... <distributionmanagement> <repository> <ID> nexus-releases </ID> <Name> nexus release repository </Name> <URL> http: // 127.0.0.1: 8080/nexus/content/repositories/releases/</URL> </Repository> <snapshotrepository> <ID> nexus-snapshots </ID> <Name> nexus snapshot repository </Name> <URL> HTTP: // 127.0.0.1: 8080/nexus/content/repositories/snapshots/</URL> </snapshotrepository> </distributionmanagement>... </Project>

Settings. xml

XML Code
  1. <Settings>
  2. ...
  3. <Servers>
  4. <Server>
  5. <ID> nexus-releases </ID>
  6. <Username> admin </username>
  7. <Password> admin123 </password>
  8. </Server>
  9. <Server>
  10. <ID> nexus-snapshots </ID>
  11. <Username> admin </username>
  12. <Password> admin123 </password>
  13. </Server>
  14. </Servers>
  15. ...
  16. </Settings>
<Settings>... <servers> <Server> <ID> nexus-releases </ID> <username> admin </username> <password> admin123 </password> </Server> <id> nexus-snapshots </ID> <username> admin </username> <password> admin123 </password> </Server> </servers>... </Settings>

Here, we configure all the snapshot version components to be deployed to the snapshots repository of nexus, and all the release components to be deployed to the releases repository of nexus. Login is required for deployment, because we configure the username and password for the corresponding repository ID in settings. xml.

Then, executeMVN deploy, You will see Maven deploy the project component to nexus, browse the corresponding nexus repository, and you will see the component just deployed. When others build their projects, Maven looks for Dependencies from nexus and downloads them.

 

Summary

This article describes the powerful Repository Manager nexus, including how to download and install nexus, configure the Nexus proxy central repository, manage the Nexus proxy repository, local repository, and repository group. It also helps you understand how to search components through nexus. Finally, configure the Nexus repository in Maven and deploy components to the nexus repository. These are the most basic and commonly used functions of nexus. As the usage goes deeper, you will find that nexus has many other features, such as user management and role permission management.

The Nexus OSS version is completely open-source. If you are interested, you can learn its source code and even implement a rest client by yourself.

Hug nexus right away. It's free.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.