Build a Maven private server using nexus

Source: Internet
Author: User
Tags maven central sonatype

The advantage of using Maven is that you can manage various dependencies of the project in a unified manner. You can download the dependency from the maven central library or third-party library by setting the dependency in the POM file. However, you may also encounter some problems in internal use of the enterprise. Each user needs to download the corresponding dependency package or plug-in, which is inefficient. Therefore, it is necessary to set up private servers within the enterprise.

After a private server is set up, all dependencies can be downloaded from the private server. The private server automatically determines that if the private server does not have this resource, the private server will automatically download it online. If the private server already contains the required resources, this can be provided to users through the Intranet, greatly improving work efficiency.

Nexus is a common Maven private server, which is easy to install and use. It is used to build internal Maven private servers.

Install nexus1 and download

Download address: http://nexus.sonatype.org/downloads/, which lists all regions and selects the latest region for use. 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.

Select the bundle version for convenience. This document downloads nexus-oss-webapp-1.9.2.3-bundle.tar.gz.

2. Installation

Decompress the downloaded file in the specified directory.

Tar xvf nexus-oss-webapp-1.9.2.3-bundle.tar.gz

After decompression, two folders are displayed, namely nexus-oss-webapp-1.9.2.3 and sonatype-work. The former contains the running environment of nexus and the application procedure, the latter contains your own configuration and storage components. In the nexus-oss-webapp-1.9.2.3/CONF/plexus. properties, you can modify the path of the port information that is already in the workspace.

3. Start nexus

Enter the nexus-oss-webapp-1.9.2.3/bin/JSW/directory, and then enter the corresponding directory according to the OS version, in Linux, run. /nexus start starts the service and runs it directly. /nexus will see the prompt message. The default listening port of neuxs is 8081. In this case, you can run http: // nexus-server-IP: 8081/nexus in the browser to view the neuxs interface.

4. Configure nexus to enable automatic startup

Take centos as an example:

CP/opt/nexus-2.0.3/bin/nexus/etc/init. d/

Open/etc/init. d/nexus and modify

# Set this to the root of the nexus Installation
Nexus_home = "/opt/nexus-2.0.3"

Chkconfig nexus on

For Ubuntu

Run after copying: update-rc.d nexus ults

Note:

-A input-M state -- state new-m tcp-p tcp -- dport 8081-J accept

Configure nexus to enable remote Indexing

The new neuxs environment is just an empty repository and needs to be manually synchronized with the remote central database. By default, Nexus disables remote index download. The most important thing is to enable remote index download. Log on to the nexus system. The default username and password are admin/admin123.

Click Repositories under the Administration menu on the left, find the three repositories Apache snapshots, codehaus snapshots, and Maven central in the repository list on the right, and then change download remote indexes to true without the repository configuration. Right-click the three repositories and select repari index. Then, Nexus downloads the remote index file.

Create an internal warehouse

Create a new company's internal repository. The procedure is repositories-> Add-> hosted repository. Enter the repository ID and repository name in the lower part of the page, for example, enter myrepo and my repository respectively, set deployment policy to allow redeploy, and click Save to complete the creation.

Modify the neuxs repository Group

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.

In neuxs-1.9.2.3, a group named "Public repositories" is provided by default. You can click this group to adjust the warehouse he protects and add the company's internal warehouse just created to it, in this way, you do not need to specify the internal repository address in Maven. Create a group with the group ID public-snapshots and group name public snapshots repositories.
Snapshots and snapshots are added.

The installation and configuration of neuxs are complete. The following describes how to use your private server in Maven.

Project uses Maven private server configuration to download components from nexus private server

After Maven is installed, no repository information is configured by default. MAVEN downloads the dependent components from the remote central repository by default. Since the private server is used, you need to clearly tell Maven where to download the component. You can configure it in three places, namely, settings under conf In the maven installation directory. XML file. The global configuration is
USERPROFILE /. settings. XML. If the file does not exist, copy settings. XML, this is for the current user, and another is in pom. it is specified in XML and only valid for this project. The priority of the three files is Pom. XML is greater. m2 ,. m2 is greater than Conf.

The settings. xml configuration is as follows:

Add a profile in the profiles section

    <profile>      <id>nexus</id>      <repositories>        <repository>            <id>nexus</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>            <releases><enabled>true</enabled></releases>            <snapshots><enabled>false</enabled></snapshots>        </repository>        <repository>            <id>nexus-snapshots</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>            <releases><enabled>false</enabled></releases>            <snapshots><enabled>true</enabled></snapshots>        </repository>      </repositories>      <pluginRepositories>        <pluginRepository>            <id>nexus</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>            <releases><enabled>true</enabled></releases>            <snapshots><enabled>false</enabled></snapshots>        </pluginRepository>        <pluginRepository>            <id>nexus-snapshots</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url>            <releases><enabled>false</enabled></releases>            <snapshots><enabled>true</enabled></snapshots>        </pluginRepository>       </pluginRepositories>    </profile>

The ID and name mentioned above can be defined according to your preferences to ensure that the IDs of multiple repository are not repeated, mainly for URL configuration, you can find the corresponding URL in the repositories list of nexus, that is, the repository path of repositories. We have already set the repository group in neuxs. Here we will take the repository path information of the corresponding group.

In addition, warehouses are the home of two main components. The first component is used as the dependency of other components. This is the majority of Component Types stored in the central repository. Another component type is plug-in. If pluginrepositories is not configured, you will still see the plug-in components required to download from the remote Central Library when executing the maven action, so you must add this here.

In settings. XML, the profile cannot be configured. You need to activate it. In the activeprofiles section, add the following Configuration:

<activeProfile>nexus</activeProfile>

The content in activeprofile defines the profile ID above.

In fact, you can only configure the settings in the Pom. xml file of the project. The configuration is as follows:
The configuration of POM. XML is as follows:

  <repositories>        <repository>            <id>nexus</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>            <releases>                <enabled>true</enabled>            </releases>            <snapshots>                <enabled>false</enabled>            </snapshots>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>nexus</id>            <name>local private nexus</name>            <url>http://192.168.1.68:8081/nexus/content/groups/public</url>            <releases>                <enabled>true</enabled>            </releases>            <snapshots>                <enabled>false</enabled>            </snapshots>        </pluginRepository>    </pluginRepositories>

The ID and name do not matter. The key is not to mistake the URL.

With the above configuration, if Maven does not have any dependent components in the local library when executing Maven operations in the project, MAVEN will download them from the private server. If the private server does not, the private server downloads data from the remote central database and caches the data locally in the private server. If the second user needs to use this component again, the private server provides the download directly.

Deploy project to private server

For enterprise projects, you can also submit them to the nexus private server. First, you must configure deploy in the Pom. xml file of the project. The configuration is as follows:

<distributionManagement><repository><id>releases</id><name>Nexus Release Repository</name><url>http://10.1.81.199:8081/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>snapshots</id><name>Nexus Snapshot Repository</name><url>http://10.1.81.199:8081/nexus/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>

This is a submission or an error will be reported because you do not have the permission to submit to nexus.

In the conf directory of Maven, configure setting. xml

    <server>      <id>releases</id>      <username>admin</username>      <password>admin123</password>    </server>  <server>  <id>snapshots</id>  <username>admin</username>  <password>admin123</password>    </server>

Note that the ID here should correspond to the ID of the remote Publishing Management Library configured in pom. Both the user name and password are nexus. Deploy again.

To release a released project with source code to nexus, You need to configure the plug-in. Add the following plug-in to Pom. xml:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin>
In deploy, test is not performed. Because I perform test automatically during deploy, and the environment required by my test may no longer exist, the deploy parameter cannot be added. test. skip = true
You can skip the test step. I used it in eclipse,
Run --- build... input these.


FAQs:

An error occurred while updating:

Was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
Delete all XXX. lastupdated files in the. m2 folder and run Maven again. OK!

Or you can ignore XXX. lastupdated .. by adding the-u parameter when using Maven ..

Reference: http://www.linuxidc.com/Linux/2012-02/54827.htm

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.