Docker easily builds Nexus private warehouse and realizes Maven private server

I. Introduction

I believe that domestic partners have experienced the situation that the pull-down speed of Gradle, Maven, and NPM is too slow. Our general approach is to configure a central warehouse such as Alibaba Cloud . The acceleration problem can be solved in this way, but if the library within the team wants to be uploaded and distributed, it is not suitable to upload it to a shared Maven warehouse such as Maven Central. That way confidentiality and timeliness will be compromised. Therefore, within the team, we generally build a private central warehouse in the LAN. The software that supports this kind of private warehouse is Nexus . This article will introduce in detail how to build from scratch, upload the library, and pull down the entire process of using the library.

Two, Nexus build

Now that everything can be Docker, we don't need to destroy the native installation of the local system. Create a new file directly docker-compose.ymland write the following content:

version: '3.1'
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
      - 6005:8081
    volumes:
      - /data/Docker/nexus/data:/nexus-data   

Pay attention to the location of the mapping in the last line volumens. If you need to backup and restore, you only need to package and restore this folder.
After writing, run docker-compose up -dand run. Then visit 6005the port of the server and see this interface, even if the construction is successful.
insert image description here

3. Configure the warehouse

After building, there will be some default warehouses:

maven-central: maven central library, pull jar from https://repo1.maven.org/maven2/ by default
maven-releases: private library release version jar maven-snapshots: private library snapshot (debug version) jar
maven-public: Warehouse grouping, combining the above three warehouses together to provide external services

Concept Note:

group: This is a concept of warehouse aggregation. If the user selects the address of the Group as the warehouse address, he can access the warehouse configured in the Group, which is convenient for developers to set. maven-public is a warehouse of Group type. Multiple warehouses are set inside. The access order depends on the configuration order. 3.x defaults to Releases, Snapshots, and Central. Of course, you can also set it yourself.
hosted: private warehouse, the release warehouse of internal projects, which is specially used to store the jar files generated by ourselves snapshots: the snapshot warehouse of local projects
releases: the official version released by local projects
proxy: proxy type, the warehouse that looks for data from the remote central warehouse (You can click the Configuration page of the corresponding warehouse and check the value of the Remote Storage attribute, which is the path of the remote warehouse to be proxied). For example, you can configure the Alibaba Cloud maven warehouse
central: central warehouse

3.1 Configure Acceleration Library

We learned from the above that Nexus will pull the public libraries we need from maven.org by default. We need to configure an accelerated library of Alibaba Cloud, so that anyone who needs a public library will be downloaded from Alibaba Cloud first. In Settings, click Create Repository.
insert image description here
As you can see, we can create a variety of warehouses, including Docker, Maven, NPM, and Yum. This article takes Maven as an example, and other warehouses are similar.
insert image description here
You can write a name, and then enter Alibaba Cloud’s Maven warehouse in the address bar of Remote Storage: http://maven.aliyun.com/nexus/content/groups/public/
insert image description here
After writing, you can save it. Then enter the settings of the maven-public group, add aliyun-repositorythe proxy library we just created to the right, and adjust it to the first position. In this way, some public libraries such as Spring will be downloaded from Alibaba Cloud by default, and Nexus will have its own caching function, which only needs to be downloaded for the first time. Subsequent downloads will be directly from the Nexus in the LAN, greatly speeding up the download speed. (Very suitable for teams without external network development)
insert image description here

3.2 Create a private warehouse

After configuring Maven's basic acceleration library, let's create a Maven repository for internal use by the team. We continue to click to create a warehouse, select here maven2(hosted).
insert image description here
There are three options under Version policy: Release (official version), Snapshots (beta version), and Mixed (mixed). In fact, it is the same as we usually refer to third-party libraries, except that we generally refer to the official version of third-party libraries. For the Deployment policy, I selected Allow redeploy to overwrite the upload, and you can adjust the following parameters according to your own situation.
insert image description here
Once created, it can be used directly. But before that, we'd better configure the development account, because the account password is required when uploading the library. It would be more dangerous to directly give the account of the system administrator to the developer, after all, there are many people who delete the database and run away.

4. Configure permissions

Create a role first in the Roles of the settings page:
insert image description here
then only give this role to the developer, and fix the browsing and editing permissions of the warehouse.
insert image description here
Finally, create a user, and then give the user name, password, and the address of the warehouse to the developer.
insert image description here
The warehouse address can be obtained by clicking Copy in the list:
insert image description here

5. Upload the Jar package

There are actually many ways to upload the Jar package to the private warehouse. We generally use Maven or Gradle scripts in general projects. Everyone should be familiar with Maven, so I will use Gradle, which is ecstatic for Android people, to demonstrate it. Write the following script in build.gradle to upload.
insert image description here
It is convenient for the big guys to copy:

group 'com.niubi'
version '1.0-SNAPSHOT'
//加载插件
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
//配置打Jar包的参数
javadoc {
    
    
    failOnError false
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addStringOption('encoding', 'UTF-8')
    options.addStringOption('charSet', 'UTF-8')
}
//打包文档
task javadocJar(type: Jar, dependsOn: javadoc) {
    
    
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}
//打包源代码
task sourcesJar(type: Jar, dependsOn: classes) {
    
    
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    
    
    archives jar
    archives javadocJar
    archives sourcesJar
}
uploadArchives {
    
    
    repositories {
    
    
        mavenDeployer {
    
    
            // 开发版
            snapshotRepository(url: "http://XXX:6005/repository/GfptLib/") {
    
    
                authentication(userName: "xxx", password: "xxx")
            }
            /*// 正式版
            repository(url: "http://XXX:8081/repository/maven-releases/") {
    
    
                authentication(userName: "xxx", password: "xxx")
            }*/
			// 只发布jar包
            addFilter('jar') {
    
    artifact, file ->
                artifact.ext == 'jar'
            }
            pom.groupId = "$project.group"
            pom.artifactId = "$project.name"
            pom.version = "$project.version"
        }
    }
}

Like Maven, after we finish writing, we can see this command directly in the Gradle window of IDEA, double-click to run and upload:
insert image description here
after running successfully, we can see the Jar package in the Maven warehouse, and can use the jar package Referenced to the project.

6. Use the Jar package

Just like the usage of referencing a third-party library, you only need to write the coordinates of the jar package just uploaded and use it:

compile("com.niubi:util:latest.integration")

Of course, in the warehouse dependency address, we need to add our own Maven private server address:

// 【仓库设置】
repositories {
    
    
    // 使用本地仓库
    mavenLocal()
    // 使用团队私服仓库
    maven {
    
    
        url "http://xxx:6005/repository/GfptLib/"
    }
    maven {
    
    
        url "http://xxx:6005/repository/maven-public/"
    }
    // 使用 Gradle 的自身默认配置加载仓库
    mavenCentral()
}

After recompiling, you can download the Jar package we just uploaded.

It's over, sprinkle flowers~

Guess you like

Origin blog.csdn.net/u012558210/article/details/122696428