Maven study notes (8): Use nexus to create a private server

Source: Internet
Author: User
Tags maven central sonatype

By setting up your own private server, you can reduce the load on the central repository, save Internet bandwidth, accelerate Maven building, and deploy components on your own, so as to effectively use Maven. Nexus is currently the most popular Maven repository management software.

Nexus introduction:Nexus is divided into open-source and professional versions. The open-source version is based on the gplv3 license, and its features are sufficient to meet the needs of most Maven users. The following are some features of the open-source nexus version:
  • Small memory usage (at least 28 m)
  • Extjs-based friendly interface
  • Restlet-based full rest API
  • Support for Agent repositories, host repositories, and warehouse groups
  • Based on the file system, no database is required
  • Support warehouse index and search
  • Support uploading Maven components from the interface
  • Fine-grained Security Control
The Nexus Professional Edition is paid for. In addition to all features of the open-source version, it mainly includes features required for enterprise security control, release process control, and so on.
Install nexus:Nexus is a typical Java Web application. It has two installation packages: one is the bundle package containing the jetty container, and the other is the war package that does not contain the Web container. Download nexus:Slave. Install nexus in bundle mode:Nexus bundle comes with the jetty container, so you can directly start nexus without the need for additional web containers. First, decompress the bundle file, and the following two subdirectories will be obtained:
  • Nexus-2.9.1-02/: This directory contains the files required for running nexus, such as startup scripts and dependent jar packages.
  • Sonatype-work/: This directory contains configuration files, log files, and repository files generated by nexus.
The first directory is required to run nexus, and the contents of all nexus instances of the same version are the same. The second directory is not required. Nexus stores different configurations and repository content of nexus used by different users on different machines. When you need to back up nexus, the sonatype-work/directory is backed up by default, because the directory contains user-specific content. On a Windows operating system, you need to enter the Nexus-2.9.1-02/bin/JSW/windows-x86-32/sub-project and then run the console-nexus.bat script directly to start nexus. Then, open the browser to access http: // localhost: 8081/nexus/and you will be able to see the Nexus interface. To stop nexus, press Ctrl + C on the command line. There are other scripts in the Nexus-2.9.1-02/bin/JSW/windows-x86-32/directory:
  • Install-nexus.bat: Install nexus as a Windows Service
  • Uninstall-nexus.bat: uninstall neuxs Windows Services
  • Start-nexus: Start the neuxs Windows Service
  • Stop-nexus: Stop the neuxs Windows Service
With the help of the Windows service, you can make nexus automatically start with windows, which is very convenient. A common problem with Bundle installation is port conflict. The default port used by nexus bundle is 8081. If this port has been occupied by other applications, or you want to use port 80 to develop the Nexus service, edit the Nexus-2.9.1-02/CONF/plexus file. properties, find the property application-port, change the default value 8081 to another port number as needed, save the file, and restart nexus. Install nexus in war mode:Nexus also provides a war package that can be directly deployed to a Web container. This package supports mainstream web containers, such as Tomcat, glassfish, Jetty, and resin. Take Tomcat as an example. Place the war package in the webapps directory of Tomcat and start Tomcat. Then visit http: // localhost: 8080/nexus/To see the Nexus interface. Logon to nexus: the permissions of nexus are well controlled. By default, Nexus access is anonymous, while anonymous users only have some basic permissions to obtain higher permissions, you must log on as an administrator. Click Log in the upper-right corner of the page to log on. The default administrator username and password of nexus are admin/admin123.

Nexus repository and repository group:Nexus contains various types of warehouse concepts, including Agent warehouse, host warehouse, and warehouse group. Each Warehouse provides a wide range of practical configuration parameters for users to customize as needed. Nexus built-in Repository:Click the repository link in the left-side navigation pane of the nexus page to view all the repositories contained in the current nexus. There are four types of warehouses: Group, hosted, proxy, and virtual ). In addition, the repository has a policy, indicating whether the repository is a release repository or a snapshot version repository. The values in the last two columns are the status and path of the repository. The purpose of each warehouse is explained below:
  • Maven central: this repository acts as a proxy for the maven central repository. Its policy is release. Therefore, only the release component in the central repository is downloaded and cached.
  • Releases: This is a type repository of releases, which is used to deploy release component within the Organization.
  • Snapshots: This is a snapshot repository of the host type used to deploy snapshot version components within the Organization.
  • 3rd party: This is a host type repository with the release policy. It is used to deploy third-party release components that cannot be obtained from the public repository.
  • Apache snapshots: this policy is the proxy repository of snapshots, used to represent the snapshot version component of the Apache Maven repository.
  • Codehaus snapshots: This is a proxy repository with a policy of snapshots. It is used to represent the snapshot version component of the cohehaus Maven repository.
  • Google Code: This is a release agent repository used to represent the release component of the Google Code Maven repository.
  • Java. Net-Maven 2: This is a release agent repository used to represent the release component of the java.net Maven repository.
  • Public Repository: this repository group aggregates all repositories whose policies are release and provides services through consistent addresses.
  • Public snapshot repositories: this repository group aggregates all the above-mentioned repositories whose policies are snapshot and provides services through consistent addresses.
The concept of nexus repository classification:As follows:
As you can see, Maven can download the component directly from the host repository; Maven can also download the component from the agent repository, and the agent repository will download and cache the component from the remote repository. Finally, for convenience, maven can download components from the repository group, but the repository group does not have actual content. It will switch to the host repository or the agent repository it contains to obtain the actual component content.
Nexus index and component search:For nexus, a common function is to search for a component. By maintaining an index, nexus provides the search function, allowing Maven users to easily locate component coordinates. To search for the maven central database, you must first set the maven central agent repository in nexus to download the remote index. Note that the default value of this configuration is disabled.

With the index, you can search for Maven components. In the left-side Navigation Pane of nexus, you can enter a keyword to search. Click an item in the search results to display a more specific component information panel at the bottom of the interface.
In addition to the component coordinates, this Panel also contains an XML dependency declaration. You can copy and paste the component to the project pom. In addition to simple keyword search, Gav search, class name search, and check and search are also provided in nexus. You can click the drop-down menu in the upper left corner of the search page to select Advanced Search:
  • Gav search: allows users to perform more targeted searches by setting information such as groupid, artifactid, and version.
  • Classname search: allows you to search for components that contain a Java class.
  • Checksum search: allows users to directly use the component inspection and to search for this component.
Configure Maven to download components from nexus:In pom, you can set the project to download the component from the private server. In actual applications, we usually want all the projects to download the component from the private server. In this case, we need to configure the component in the setting. xml file. Setting. XML does not support direct configuration of repositories and pluginrepositories, but Maven can use the profile mechanism to allow users to put repository configuration in the profile of setting. xml. In practical applications, we hope that all download requests will pass through the private server, so we also need to use the image configuration. The configuration example is as follows:
<settings>     ...     <mirrors>          <mirror>               <id>nexus</id>               <mirrorOf>*</mirrorOf>               <url>http://localhost:8081/nexus/content/groups/public</url>          </mirror>     </mirrors>     <profiles>          <profile>               <id>nexus</id>               <repositories>                    <repository>                         <id>central</id>                         <url>http://central</url>                         <release><enabled>true</enabled></release>                         <snapshots><enabled>true</enabled></snapshots>                    </repository>               </repositories>                <pluginRepositories>                    <repository>                         <id>central</id>                         <url>http://central</url>                         <release><enabled>true</enabled></release>                         <snapshots><enabled>true</enabled></snapshots>                    </repository>               </pluginRepositories>          </profile>     </profiles>     <activeProfiles>          <activeProfile>nexus</activeProfile>     </activeProfiles>     ...</settings>
The repository and plug-in repository configurations here, whose IDs are both central. That is to say, they overwrite the configuration of the Super pom central repository and Their URLs are irrelevant, because all requests access the private server address through images. The main purpose of configuring the repository and plug-in repository is to enable support for downloading snapshot versions. When Maven needs to download the release or snapshot version components, it first checks the central, check whether the components of this type are supported. After receiving a positive response, access the private server repository address based on the image matching rules.
Deploy components to nexus:We often Upload components within an organization or third-party components that cannot be obtained from a public warehouse to the host warehouse for your use. You can configure Maven to automatically deploy the component to the host repository of nexus, or upload the component through the interface. Use Maven to deploy components to nexus:The Snapshot version components generated by daily development can be directly deployed to the host repository with a snapshot in nexus. The components officially released by the project should be deployed to the host repository with a release in nexus. The following shows a typical configuration:
<project>     ...     <distributionManagement>          <repository>               <id>nexus-release</id>               <name>Nexus Release Repository</name>               <url>http://localhost:8081/nexus/content/repositories/</url>          </repository>          <snapshotRepository>               <id>nexus-snapshots</id>               <name>Nexus Snapshots Repository</name>               <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>          <snapshotRepository>                                                   </distributionManagement>     ...</project>
The Nexus repository is read-only for anonymous users. To deploy components, you must configure authentication information in settings. XML as follows:
<settings>          ...     <servers>          <server>                  <id>nexus-release</id>               <username>admin</username>               <password>*****</password>          </server>               <id>nexus-snapshots</id>               <username>admin</username>               <password>*****</password>     </servers>                         ...</settings>
Manual deployment:Sometimes, we need to manually deploy the component to the repository. To upload a third-party component, first select a host repository, and then select the artifact upload tab at the bottom of the page. When uploading a component, Nexus asks the user to confirm the maven coordinate. If the component is built using Maven, select from Pom from the GAV definition drop-down list; otherwise, select Gav parameters, you need to define a Maven coordinate for this component.

Maven study notes (8): Use nexus to create a private server

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.