Docker can easily build Nexus private warehouse and realize Maven private server

1, Foreword

It is believed that domestic small partners have experienced too slow pull-down speed of Gradle, Maven and NPM. Our general approach is to configure Alibaba cloud Central warehouse . The acceleration problem can be solved in this way, but if the library within the team wants to upload and distribute, it is not appropriate to transfer it to Maven Central, a common Maven warehouse. Then confidentiality and timeliness will be destroyed. Therefore, within the team, we usually build a private central warehouse in the LAN. The software that supports this private warehouse is Nexus , this article will introduce in detail how to build, upload and pull libraries from scratch.

2, Nexus build

Now everything can dock, so we don't need to destroy the native installation of the native system. Directly create a Docker compose YML file, write the following contents:

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

Note the location of the mapping of volumes in the last line. If you need to backup and restore, you only need to package and restore this folder.
After writing, run docker compose up - D. Then visit the 6005 port of the server and see this interface, even if it is built successfully.

3, Configure warehouse

After setting up, there will be some default warehouses:

Maven central: Maven Central Library, from https://repo1.maven.org/maven2/ Pull jar
Maven releases: private library distribution jar Maven snapshots: private library snapshot (debug version) jar
Maven public: warehouse grouping, combining the above three warehouses to provide external services

Concept description:

Group: This is a concept of warehouse aggregation. Users can access the warehouse configured in the group by selecting the address of the group, which is convenient for developers to set their own warehouse. Maven public is a warehouse of group type. Multiple warehouses are set internally. The access order depends on the configuration order. 3 X defaults to Releases, Snapshots and Central. Of course, you can set it yourself.
hosted: private warehouse, internal project publishing warehouse, which is specially used to store our own generated jar files. snapshots: snapshot warehouse of local projects
releases: the official release of the local project
Proxy: proxy type, which is used to find the data warehouse from the remote central warehouse (you can click the value of the Remote Storage attribute under the Configuration tab of the corresponding warehouse, that is, the path of the proxy remote warehouse), for example, Alibaba cloud maven warehouse can be configured
Central: central warehouse

3.1 configuring acceleration Library

We learned from the above that Nexus will start from maven.com by default Org to get the public library we need. We need to configure an Alibaba cloud acceleration library, so that anyone who needs a public library will give priority to downloading from Alibaba cloud. In settings, click Create warehouse.

As you can see, we can create multiple warehouses, Docker, maven, NPM and Yum. This article takes Maven as an example. Other warehouses are similar.

You can write a name, and then enter the Maven warehouse of Alibaba cloud in the address bar of Remote Storage: http://maven.aliyun.com/nexus/content/groups/public/

After writing, you can save it. Then enter the settings of Maven public group, add the aliyun repository proxy library we just created to the right and adjust it to the first position. Such public libraries as Spring will be downloaded from alicloud by default, and Nexus will have its own caching function, which only needs to be downloaded for the first time. The follow-up will be downloaded directly from Nexus on the LAN, which greatly speeds up the download speed. (very suitable for teams without external network development)

3.2 create a private warehouse

After configuring Maven's basic acceleration library, let's create a maven warehouse for internal use by the team. Let's continue to click Create warehouse and select Maven 2 (hosted) here.

Under the Version policy, there are three options: Release (official version), Snapshots (beta version) and Mixed. In fact, it is the same as that we usually refer to the third-party library, but we generally refer to the official version of the third-party library. Deployment policy here I selected Allow redeploy to overwrite the upload. You can adjust the following parameters according to your own situation.

Once created, you can use it directly. But before that, we'd better configure the development account, because the account password is required when uploading the library. If you directly give the account of the system administrator to the developer, it will be more dangerous. After all, there are many people who delete the database and run away.

4, Configure permissions

Create a role in Roles on the settings page:

Then only the developer is given the role of fixed browsing and editing permissions of the warehouse.

Finally, create a user, and then give the user name, password and the address of the warehouse to the developer.

The warehouse address can be obtained by clicking Copy in the list:

5, Upload Jar package

There are many ways to upload Jar packages to private warehouses. We usually use Maven or Gradle scripts in our projects. Maven should be familiar to everyone. I'll demonstrate it with Gradle, which is the ecstasy of Andrews. In build Write the following script in Gradle to upload.

Easy for big guys to copy:

group 'com.niubi'
version '1.0-SNAPSHOT'
//Loading plug-ins
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
//Configure the parameters of Jar package
javadoc {
    failOnError false
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addStringOption('encoding', 'UTF-8')
    options.addStringOption('charSet', 'UTF-8')
}
//Package document
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}
//Package source code
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}
uploadArchives {
    repositories {
        mavenDeployer {
            // Development version
            snapshotRepository(url: "http://XXX:6005/repository/GfptLib/") {
                authentication(userName: "xxx", password: "xxx")
            }
            /*// Official edition
            repository(url: "http://XXX:8081/repository/maven-releases/") {
                authentication(userName: "xxx", password: "xxx")
            }*/
			// Publish only jar packages
            addFilter('jar') {artifact, file ->
                artifact.ext == 'jar'
            }
            pom.groupId = "$project.group"
            pom.artifactId = "$project.name"
            pom.version = "$project.version"
        }
    }
}

Like Maven, after we write it, we can directly see this command in the Gradle window of IDEA, and double-click to run the upload:

After running successfully, you can see the jar package in Maven warehouse and reference the jar package to the project.

6, Using Jar package

Just like the use of referencing a third-party library, you can use it after writing the coordinates of the jar package just uploaded correctly:

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

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

// Warehouse settings
repositories {
    // Use local warehouse
    mavenLocal()
    // Use team private server warehouse
    maven {
        url "http://xxx:6005/repository/GfptLib/"
    }
    maven {
        url "http://xxx:6005/repository/maven-public/"
    }
    // Load the warehouse using Gradle's own default configuration
    mavenCentral()
}

Recompile and download the Jar package we just uploaded.

End, sprinkle flowers~

Keywords: Java Docker Gradle Maven nexus

Added by jrough on Thu, 27 Jan 2022 22:39:49 +0200