banner
jzman

jzman

Coding、思考、自觉。
github

Building a Maven private repository from scratch in the Gradle series

In the previous articles, we learned about Gradle-related knowledge. Today's content is about using Nexus Repository Manager to build a Maven private server. This allows us to extract some common library projects for use in other projects, such as utility libraries and basic component libraries. Below, we will start from scratch to build a Maven private server.

  1. Install Nexus service
  2. Run Nexus service
  3. Publish library projects
  4. Use library projects
  5. Summary

Install Nexus service#

Download the Nexus installation package at the following address:

http://www.sonatype.com/download-oss-sonatype

Choose the version that suits you for download. Here, we choose nexus-3.13.0-01-win64. After downloading, unzip it. There are two folders as follows:

// Nexus service
nexus-3.13.0-01
// Nexus related configuration files, such as log, repository, index, cache, and other configuration files
sonatype-work

Then open the command prompt as an administrator and go to the bin directory under the Nexus service extraction directory to execute the following command to install the Nexus service, as shown below:

nexus.exe/install

After the execution is completed, the Nexus service is installed. The successful execution result of installing the Nexus service is as follows:

E:\Gradle\Nexus\nexus-3.13.0-01-win64\nexus-3.13.0-01\bin>nexus.exe/install
Installed service 'nexus'.

Run Nexus service#

After installing the Nexus service, execute the following command to run the Nexus service, as shown below:

nexus.exe/run

Executing this command will start the Nexus service. The key information for successful execution is as follows:

E:\Gradle\Nexus\nexus-3.13.0-01-win64\nexus-3.13.0-01\bin>nexus.exe/run
Preparing JRE ...
2018-09-10 23:02:21,265+0800 INFO  [FelixStartLevel] *SYSTEM org.sonatype.nexus.pax.logging.NexusLogActivator - start
//...
-------------------------------------------------

Started Sonatype Nexus OSS 3.13.0-01

-------------------------------------------------
//...

After the Nexus service starts normally, you can use the following address to access the Nexus service:

http://localhost:8081/

The running result is shown below:

image

Create repository#

After successfully logging in with the default username "admin" and default password "admin123", select "Settings" (gear icon), select "Security"->"Users" on the left, and then select "Create local user" to enter the relevant information to create an account. Then use the newly created account to log in, and you can create a repository under your own account, as shown in the following figure:

image

Publish library projects#

After creating the repository, you can publish projects to the repository. Here, we first use Android Studio to create a library project named "test". Then, in its build.gradle file, configure the Task for uploading the library. Let's take a look at the plugins used by this library:

// Use the Android Library plugin
apply plugin: 'com.android.library'
// Use the Maven plugin
apply plugin: 'maven'

// Upload Task for publishing
uploadArchives{
    repositories{
        mavenDeployer{
            // Official release repository
//            repository(url:"http://localhost:8081/repository/jzman-releases/"){
//                authentication(userName:"jzman",password:"nexus2410.")
//            }
            // Snapshot repository
            snapshotRepository(url:"http://localhost:8081/repository/jzman-snapshots/"){
                authentication(userName:"jzman",password:"nexus2410.")
            }

            pom.project {
                // Version number, if it is a snapshot version, the version number should be followed by "-SNAPSHOT", otherwise it cannot be recognized and uploaded correctly
                version '1.0.0-SNAPSHOT'
                // Just write the project name
                artifactId 'testWidget'
                // Group ID, similar to package name, to ensure uniqueness
                groupId 'com.manu.test'
                // Packaging format
                packaging 'aar'
                // Description
                description 'Test Maven private server setup'
            }
        }
    }
}
//...

After configuring the task for uploading the library project, you can upload it to the corresponding repository created earlier as needed. The upload task can be executed using the shortcut provided by Android Gradle, as shown in the following figure:

image

After the uploadArchives is completed, the corresponding library project will be published to the corresponding repository. The following figure shows the publication to the official repository:

image

For the official release, the version number must be changed each time, otherwise it cannot be published successfully. The following figure shows the publication to the snapshot repository:

image

If the library project is published to the snapshot repository, it can be uploaded and published multiple times without changing the version number each time. It will automatically increment based on the original version number, such as 1.0.0-timestamp-1, 1.0.0-timestamp-2, etc. When using it, Maven will automatically download the latest snapshot version, which is the snapshot version with the largest serial number. This method facilitates the timely release of issues discovered in library projects and helps with rapid iteration. When there are no issues, the official version can be released.

Use library projects#

After the library project is successfully published, it can be used in any project. In the build.gradle file of the project, configure the Maven repository used by the project:

//...

allprojects {
    repositories {
        //...
        // Configure Maven repository
        maven {
            url 'http://localhost:8081/repository/jzman-releases/'
        }
    }
}

//...

Then, in the specific project, you can configure the corresponding dependency to use this library project. The dependency configuration is as follows:

dependencies {
    //...
    // Configure dependency library project
    compile 'com.manu.test:testWidget:1.0.0'
}

Summary#

The main content of this article is to build a usable Maven private server locally. In actual development, you only need to replace the corresponding repository address with your own company's repository address based on the above content. The construction of jcenter repository is similar. The content of this article ends here.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.