Build Maven private server with nexus

Source: Internet
Author: User
Tags maven central

1. Why is nexus used?

If there is no private server, all the components we need must be downloaded to the local machine through the maven central repository and third-party Maven repository, the repeated download of components from a Maven repository by everyone in a team undoubtedly increases the load on the repository and wastes Internet bandwidth. If the network speed is slow, the project process will be affected. In many cases, project development is performed on the Intranet. What can I do if I cannot connect to the maven repository? How can I develop public components for other projects? At this time, we had to build our own Maven private server for our team, which saved both network bandwidth and accelerated the project building process, of course, the prerequisite is that your private server has all the components required by the project.

2. Download nexus

: Http://www.sonatype.org/nexus/go

3. Start nexus

I downloaded the zip package, decompress it, and enter \ nexus-2.1.2-bundle \ nexus-2.1.2 \ bin \ JSW \. Select the folder Based on the operating system type. I chose the windows-x86-32 folder, the following BAT file is displayed.

Figure (1) double-click the console-nexus.bat to run. Enter http: // 127.0.0.1: 8081/nexus/in the browser. The result shown in figure (2) indicates that the nexus is successfully started.

Figure (2)

8081 is the default port number. to modify the port number, go to nexus-2.1.2-bundle \ nexus-2.1.2 \ conf \ to open the nexus. properties file and modify the application-port attribute value.

Default username and password: admin/admin123. After logging on, you can see figure (3:

Figure (3) 4. Nexus Repository

There are four types of nexus warehouses:

GROUP: Repository Group

Hosted: Host

Proxy: proxy

Virtual: Virtual

 

After logging on to nexus for the first time, you can see the next warehouse group and multiple warehouse.

Figure (4)

Public repositories: Repository Group

3rd party: component repository of third-party Release versions that cannot be obtained from public warehouses

Apache snapshots: component repository that uses the snapshot version of the proxy apachemaven Repository

Central: The repository used to release version components in the maven central repository.

Central M1 shadow: used to provide the component image repository of the M1 version in the central repository.

Codehaus snapshots: The repository used to represent the snapshot version component of the codehausmaven Repository

Releases: the host type repository used to deploy and manage internal release component

Snapshots: the host type repository used to deploy and manage internal snapshot version components.

4.1 create a nexus host Repository

Click Add on the menu bar on the repositories option page, as shown in the following figure. Select the repository type to be added.

Figure (5)

Here, I click "add a repository of the host type". The configuration of the new repository appears below the repository list, as shown below:

Figure (6)

Click Save to view the newly added repository in the repository list.

4.2 create a nexus proxy Repository

Click Add on the menu bar and select proxy repository. The configuration page is shown as follows:

Figure (7)

4.3 create a nexus repository Group

The relationship between a warehouse group and a warehouse is one-to-multiple. A warehouse group can point to multiple warehouses.

Click the Add button on the menu bar and select repository group to view the repository group configuration interface, as shown in the following figure (8)

Click Save to view the added repository group in the repository list. To download components in the project, the repository group URL is generally used in the configuration file.

5. Modify the maven configuration file to download components from nexus

1)To configure Maven for all users in the operating system, you only need to modify Maven \ conf \ setting. XML file. If you only want to configure Maven separately, you only need to configure conf \ setting. copy the XML file to c: \ Documents ents and Settings \ Administrator \. m2 folder (I suppose the system is attached to drive C and the user is administrator ).

 

2)Open the setting. xml file and you can see the following code:

<!-- localRepository   | The path to the local repository maven will use to store artifacts.   |   | Default: ~/.m2/repository   <localRepository></localRepository> -->

If localrepository is not set, Maven creates the local repository to the/. m2/repository folder by default.

The following code sets localrepository:

<localRepository>F:\myCenterRepository</localRepository>

Create a local repository in the mycenterrepository folder. We recommend that you do not use the default repository address. If there are many projects, the disk space occupied by the local repository will be large. Therefore, it is easier to specify the repository address to other disks.

5.2 configure the Nexus repository in the POM file.

Add the following code to the project pom file:

<repositories>       <repository>         <id>nexus</id>         <name>my-nexus-repository</name>         <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>         <releases>           <enabled>true</enabled>         </releases>         <snapshots>           <enabled>false</enabled>         </snapshots>       </repository>     </repositories>     <pluginRepositories>       <pluginRepository>         <id>nexus</id>         <name>my-nexus-repository</name>         <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>         <releases>           <enabled>true</enabled>         </releases>         <snapshots>           <enabled>false</enabled>         </snapshots>            </pluginRepository>     </pluginRepositories>   

The configuration in the POM file is only valid for the current project, but it is impossible to repeat the configuration information in each project in actual development. Therefore, we do not recommend that you configure the configuration in the POM file.

5.3 configure the Nexus repository in the setting. xml file

1) Maven provides a profile to configure repository information, as shown below:

<profiles>    <profile>      <id>myprofile</id>      <repositories>    <repository>        <id>central</id>                                           <url>http://central</url>                              <releases>            <enabled>true</enabled>        </releases>        <snapshots>            <enabled>true</enabled>        </snapshots>    </repository></repositories>    <pluginRepositories><pluginRepository>  <id>central</id>  <url>http://central</url>  <releases><enabled>true</enabled>  </releases>  <snapshots><enabled>false</enabled>  </snapshots></pluginRepository>    </pluginRepositories>    </profile></profiles>

2) Activate Profile

<activeProfiles>    <activeProfile>myprofile</activeProfile>  </activeProfiles>

3) configure the image

 <mirrors>     <mirror>         <id>nexus</id>          <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>         <mirrorOf>*</mirrorOf>       </mirror>  </mirrors>

Here, the value of mirrof is set to *, which indicates that all Maven access requests are directed to the nexus repository group.

6. Deploy components to the nexus Repository

6.1 Maven deployment

1) modify the POM File

Add the following configuration to the POM file:

<distributionManagement>       <repository>           <id>my-nexus-releases</id>           <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>       </repository>              <snapshotRepository>           <id>my-nexus-snapshot</id>           <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>       </snapshotRepository>  </distributionManagement>

2) Add authentication information to the setting. xml file:

<servers>     <server>      <id>my-nexus-releases</id>      <username>admin</username>      <password>admin123</password>    </server><server>      <id>my-nexus-snapshot</id>      <username>admin</username>      <password>admin123</password>    </server></servers>

In the above configuration, I used a super administrator account. In the development project, I can change it to a user with the permission to deploy components.

 

3) execute deployment

The component project information for the test is as follows:

<groupId>com.ez</groupId>  <artifactId>TestJar</artifactId>  <version>1.0</version>  <packaging>jar</packaging>  <name>TestJar</name>

From the above information, we can see that the component is a release version, so the component will be automatically deployed to the releases repository.

 

Run MVN clean deploy in the command line.

If you have never run this command before, Maven automatically downloads the required plug-ins from the central repository. Finally, the component is successfully deployed as shown in the following command line.

Figure (9)

View the deployed component information in the releases repository of nexus as follows:

Figure (10)

If the deployment fails, check whether the user has the deployment permission, whether the target warehouse allows deployment, and whether components required for deployment are missing.

6.2 Manual deployment

It is easier to manually deploy components. In the Nexus repository list, click the target repository to be deployed, and then click the artifact upload tab:

Figure (11) the private servers built using nexus can basically be used through the above configurations. For more configuration requirements, see nexus book.

Nexus book: http://download.csdn.net/detail/yaoqinzhou1943/4589583

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.