Starting from scratch: a complete guide to building Nexus private servers and configuring Maven warehouses

 I. Introduction

There are many advantages to using Nexus to build a private server, the most important of which is that you can control the access and deployment of components. If there is no Nexus private server, all the components we need need to be downloaded locally through Maven’s central warehouse and third-party Maven warehouse, and everyone in a team repeatedly downloads components from the Maven warehouse. Undoubtedly, the load on the warehouse is increased. And the bandwidth of the external network is wasted. If the network speed is slow, it will also affect the progress of the project. After using Nexus to build a private server, you only need to download it once on the private server, and then you can use it in the entire team, which greatly improves the efficiency.

In addition, Nexus also has many other advantages, such as small footprint, has an ExtJs-based operation interface, uses a full Restlet-based REST API to support proxy warehouses, host warehouses and warehouse groups, is based on file systems, does not need to rely on databases, and supports warehouse management. , Support component search, support component upload on the interface, etc.

2. Nexus Private Server Construction

2.1. Introduction to Nexus

Nexus, a product of Sonatype, is a powerful warehouse manager that greatly simplifies the maintenance of internal warehouses and access to external warehouses. It is mainly used to build the company's internal Maven private server to share and use dependencies in the team.

Nexus private servers have many benefits, such as:

  1. speed up builds;
  2. save bandwidth;
  3. Save bandwidth on the central Maven repository;
  4. Stable (to deal with the situation once the central server goes wrong);
  5. controls and audits;
  6. Ability to deploy third-party components;
  7. A local internal warehouse can be established;
  8. Public repositories can be built.

2.2. Download and install Nexus

Nexus official website https://help.sonatype.com/repomanager3/product-information/download

Download the corresponding version according to your system 

2.2.1. Start Nexus

Find the installation package path
Your file directory\nexus-3.20.1-01\bin

 Enter nexus.exe /run in the open black window command operation

 If you see the following words, the table starts successfully

2.2.2. Access Nexus

Don't know your nexus access address? It's okay, I'll teach you.
Your file directory\nexus-3.20.1-01\etc\nexus-default.properties
is your nexus configuration file here

application-port=access Nexus port number (default 8081)
application-host=access Nexus ip address (default localhost local ip)
so your access path is  http://localhost:8081 

If you see the following picture, it means that the startup is successful

2.2.3. Login Nexus

The account is admin, and the password is your file directory\nexus-3.20.1-01\etc\sonatype-work\nexus3\admin.password

Tip: I have changed the password here, so admin.password will disappear (because it is temporary)

Open the file and copy the password directly to log in. After the login is successful, you will be asked to enter the password again. This is the step of changing the password. You will use this password to log in to Nexus in the future.

3. Maven warehouse configuration

3.1. Introduction to Maven

Maven is a Java-based project management tool (POM project object model), which can help developers automate the construction, testing and deployment of Java projects. Maven manages project dependencies through a central warehouse, can automatically download the required dependency libraries, and can automatically generate project build scripts. Using Maven can greatly simplify the construction and management process of Java projects, improve development efficiency and project quality.

Tips: What is an automated build?

Automated build refers to a method of using tools or scripts to automate the project build process. In software development, building is the process of converting source code into executable software, including compiling, linking, packaging, and more. Traditionally, the build process was performed manually, with developers stepping through a series of commands or actions to complete the build. Automated builds use build tools or scripts to automate these operations, reducing the tediousness and errors of manual operations, and improving the efficiency and consistency of builds.

Automated builds can achieve the following functions:

  1. Compile source code: convert source code into an executable binary.
  2. Dependency management: Automatically download and manage the dependent libraries required by the project.
  3. Testing: Automatically execute testing processes such as unit testing and integration testing.
  4. Packaging: Packaging the compiled code into a deployable package.
  5. Documentation Generation: Automatically generate project documentation.
  6. Deployment: Automatically deploy the built package to the target environment.

By automating builds, developers can save time and effort, reduce human errors, and improve the efficiency and quality of software development.

3.2. Download and install Maven

For more details, please move to the Maven installation and configuration tutorial I wrote

Maven installation and eclipse configuration and creation of Maven project tutorial [the most detailed in history] http://t.csdn.cn/NbNDK

Fourth, use Nexus as a Maven repository

4.1. Package and upload Jar

① Find the maven installation directory
file path/maven/version number/libexec/conf/settings.xml

 ②servers node

        <server><!-- 发布版本-->
             <id>nexus</id> <!-- 随意填写,但项目中repository的id得与这里一致-->
              <username>admin</username> <!-- 账户-->
           <password>admin123</password>  <!-- 密码-->
        </server>
        <server><!-- 发布版本-->
      	   <id>nexus-releases</id> <!-- 随意填写,但项目中repository的id得与这里一致-->
       	   <username>admin</username> <!-- 账户-->
           <password>admin123</password>  <!-- 密码-->
        </server>
        <server><!-- 发布版本-->
             <id>nexus-snapshots</id> <!-- 随意填写,但项目中repository的id得与这里一致-->
              <username>admin</username> <!-- 账户-->
           <password>admin123</password>  <!-- 密码-->
        </server>

 ③mirrors node

    <mirror>
      <id>nexus</id>
      <!--表示访问哪些工厂时需要使用镜像-->
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>

You need to go to the Nexus website to copy the URL

 It is best to add a mirror image of Alibaba Cloud here

 <!-- 阿里云镜像-->
<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>
<mirror>  
 <id>alimaven</id>  
 <mirrorOf>central</mirrorOf>  
 <name>aliyun maven</name>  
 <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>  
</mirror> 

Here we take idea as an example, other development tools are similar

④pom.xml configures your Nexus warehouse address information

  <!-- 本地nexus私有库-->
  <distributionManagement>
    <snapshotRepository>
      <id>snapshots</id>
      <name>snapshots</name>
      <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
    <repository>
      <id>releases</id>
      <name>release</name>
      <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
  </distributionManagement>

 The URL is also the same, you need to go to the Nexus website to copy it yourself

⑤Right click on IDEA's pom.xml ➡Run Maven➡deploy

If you see BUILD SUCCESS, it means the upload is successful.

4.2. Reference Nexus library jar

① Find the maven installation directory
file path/maven/version number/libexec/conf/settings.xml

②profiles node

            <profile>
              <!--profile 的 id-->
              <id>nexus</id>
              <repositories>
                  <repository>
                      <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
                      <id>nexus</id>
                      <!--仓库地址,即 nexus 仓库组的地址-->
                      <url>http://localhost:8081/repository/maven-public/</url>
                      <!--是否下载 releases 构件-->
                      <releases>
                          <enabled>true</enabled>
                      </releases>
                      <!--是否下载 snapshots 构件-->
                      <snapshots>
                          <enabled>true</enabled>
                      </snapshots>
                  </repository>
              </repositories>
              <pluginRepositories>
                  <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
                  <pluginRepository>
                      <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
                      <id>public</id>
                      <name>Public Repositories</name>
                      <url>http://localhost:8081/repository/maven-public/</url>
                      <!--是否下载 releases 构件-->
                      <releases>
                          <enabled>true</enabled>
                      </releases>
                      <!--是否下载 snapshots 构件-->
                      <snapshots>
                          <enabled>true</enabled>
                      </snapshots>
                  </pluginRepository>
              </pluginRepositories>
          </profile>

 ③activeProfiles node

  <!-- 激活下载模板-->
    <activeProfiles>
      <activeProfile>nexus</activeProfile>
    </activeProfiles>

④Download dependencies

repositories in the POM

<!--获取公共的Maven依赖库-->
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://localhost:8081/repository/maven-public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>`1

  At this point, your project can use Nexus normally.

My sharing is over here, welcome to the comment area to discuss and communicate! !

If you find it useful, please give it a thumbs up ♥ ♥

Guess you like

Origin blog.csdn.net/weixin_74318097/article/details/132579773