banner
jzman

jzman

Coding、思考、自觉。
github

Building a Maven private repository from scratch in the Gradle series.

The previous articles have covered the knowledge of Gradle. Today's content is about using Nexus Repository Manager to build a Maven private repository. This allows extracting common library projects for use in other projects, such as utility libraries and basic component libraries. The following steps will guide you through building a Maven private repository from scratch.

  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 from the following address:

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

Choose the version that suits you and download it. In this case, we choose nexus-3.13.0-01-win64. After downloading, extract the package, which contains two folders:

// Nexus service
nexus-3.13.0-01
// Nexus related configuration files, such as logs, repositories, indexes, caches, etc.
sonatype-work

Open the command prompt as an administrator and navigate to the "bin" directory under the extracted Nexus service directory. Run the following command to install the Nexus service:

nexus.exe/install

After the installation is complete, the Nexus service is installed successfully. The installation result should be 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, run the following command to start the Nexus service:

nexus.exe/run

This command will start the Nexus service. The key information indicating a 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

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

Once the Nexus service is successfully started, you can access it using the following address:

http://localhost:8081/

The result should be as shown below:

image

Create a repository#

After logging in with the default username "admin" and default password "admin123", select "Settings" (gear icon), then select "Security" -> "Users" on the left side. Create a new user by clicking "Create local user" and entering the relevant information. After creating the new account, log in with the newly created account and you can create repositories under your own account. Refer to the following image for guidance:

image

Publish library projects#

Once the repository is created, you can publish projects to the repository. First, create a library project named "test" using Android Studio. Then, configure the task for uploading the library in its build.gradle file. Take a look at the plugins used by the 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 properly for upload
                version '1.0.0-SNAPSHOT'
                // Project name
                artifactId 'testWidget'
                // Group ID, similar to package name, ensuring uniqueness
                groupId 'com.manu.test'
                // Packaging format
                packaging 'aar'
                // Description
                description 'Test Maven private repository setup'
            }
        }
    }
}
//...

After configuring the task for uploading the library project, you can upload it to the corresponding repository you created earlier. You can use the shortcut provided by Android Gradle to execute the upload task, as shown in the following image:

image

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

image

For official releases, the version number must be changed each time, otherwise it cannot be successfully published. The following image shows the project published 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. It will automatically increment the version number based on the original version number, such as 1.0.0-timestamp-1, 1.0.0-timestamp-2, etc. When using Maven, it will automatically download the latest snapshot version, which is the snapshot version with the highest serial number. This approach facilitates the timely release of discovered issues in library projects, helping with rapid iteration. Once there are no issues, the official version can be published.

Use library projects#

After a library project is successfully published, it can be used in any project by configuring the Maven repository used by the project in its build.gradle file:

//...

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

//...

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

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

Summary#

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

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