Use Nexus3.16.2 to build maven private server, upload aar


Nexus is a product of Sonatype , it is Maven's private server.

1. Configure Nexus (v3.16.2)

Download the corresponding version of nexus: download Nexus

Insert picture description here
Unzip the file, use cmd, enter ... \ nexus-3.16.2-01-win64 \ nexus-3.16.2-01 \ bin directory, execute the command:

.\nexus.exe /run

Note:
1. Make sure that the 8081 port is not occupied;
2. Run cmd as an administrator;
3. The / start command may report an error: Could not start service. Error code: 1060; use the run command to start;

After the service is started, visit http: // localhost: 8081 / and click Sign in in the upper right corner. The default user name is admin; the password is admin123.

Insert picture description here

2. Configure Gradle

1. Use AS to create a new project and create a new nexusLib module. Write a test class in nexusLib.

2. Add the upload_nexus.gradle file in the root directory of nexusLib, and enter the following content.

Note: The parameter configuration (REPOSITORY_URL, user name, password) requirements are consistent with the server.

  • Writing one (by judging the version number, dynamically specifying the remote path.):

Reference: Android-Nexus build its own Maven repository & Gradle upload dependency package

apply plugin: 'maven'

//def定义的变量才会在project中

def GROUP = 'com.kedacom.nexuslib'
def ARTIFACT_ID = 'nexusLibrary'
def VERSION_NAME = "1.0.3"
//def VERSION_NAME = '1.0.1-SNAPSHOT'


//上传的目标仓库地址
def SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
def RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'
def REPOSITORY_URL = VERSION_NAME.toUpperCase().endsWith("-SNAPSHOT") ? SNAPSHOT_REPOSITORY_URL : RELEASE_REPOSITORY_URL

//Nexus 的私服的用户名称和密码
def NEXUS_USERNAME = 'admin'
def NEXUS_PASSWORD = 'admin123'

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                pom.groupId = GROUP
                pom.artifactId = ARTIFACT_ID
                pom.version = VERSION_NAME
                repository(url: REPOSITORY_URL) {
                    authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
                }
            }
        }
    }
    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }
    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.sourceFiles
    }

    //解决 JavaDoc 中文注释生成失败的问题
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
        options.addStringOption('charSet', 'UTF-8')
    }
    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}
  • Writing two:

Dynamically specify the local warehouse and private server, also refer to LegoArch (BD Netdisk).

The project structure is as follows:
Insert picture description here
3. Add the following code to build.gradle under the nexusLib module (Note the reference location), And sync gradle.

android{
...
}

//必须在android{}后面
apply from: 'upload_nexus.gradle'

4. Find the upload command (uploadArchives) corresponding to the nexusLib module in the gradle window on the right side of the AS:
Insert picture description here

Execute the command and wait for the upload result.

After the upload is successful, refresh http: // localhost: 8081 /, you can see the uploaded file, which corresponds to the configuration information in upload_nexus.gradle.
Insert picture description here

3. Use Library

1. Create a new test project and add the maven repository address in the project's build.gradle:

allprojects {
    repositories {
        maven {
            url "http://localhost:8081/repository/maven-releases/"
        }
       ...
    }
}

2. Add in the build.gradle of the app module:

Depends on the format corresponding to the attributes at the time of publication:implementation ‘groupId:artifactId:verion’

dependencies {
    implementation 'com.kedacom.nexuslib:nexusLibrary:1.0.0'
}

3. Directly reference the resources in aar in the test project code:

Insert picture description here

4. How to publish when there is a module dependency (writing one):

When there is a module dependency relationship in the project (for example: A depends on B; A and B need to be independently released to the remote warehouse), the overall structure of the project:

Insert picture description here

4.1、Under each module directoryCreate a new gradle.properties configuration file with the following content:

# 不可以加单引号或双引号
ARTIFACT_ID=app
VERSION_NAME=1.1.1

4.2、Root directory Create a new general configuration file release-aar.gradle with the following content:

apply plugin: 'maven'

def GROUP_ID = 'com.kedacom.vehiclecheck'
//如果没有module依赖,可以直接定义版本和名称
//def ARTIFACT_ID = 'test'
//def VERSION_NAME = "1.0.3"
//def VERSION_NAME = '1.0.1-SNAPSHOT'

//上传的目标仓库地址
def SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
def RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'
def REPOSITORY_URL = VERSION_NAME.toUpperCase().endsWith("-SNAPSHOT") ? SNAPSHOT_REPOSITORY_URL : RELEASE_REPOSITORY_URL

//Nexus 的私服的用户名称和密码
def NEXUS_USERNAME = 'admin'
def NEXUS_PASSWORD = 'admin123'

afterEvaluate { project ->
    uploadArchives {
//        configuration = configurations.archives
        repositories {
            mavenDeployer {
                repository(url: REPOSITORY_URL) {
                    authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
                    pom.groupId = GROUP_ID
                    //版本号和ARTIFACT_ID取值于gradle.properties
                    pom.artifactId = ARTIFACT_ID
                    pom.version = VERSION_NAME
                }

                pom.whenConfigured { MavenPom pom ->
                    def dependencies = pom.dependencies.findAll { item ->
                        ("unspecified" == item.version)
                        println("--all 1--->>" + item.getArtifactId() + ":" + item.getVersion())
                    }
                    println("--all 2--->>" + dependencies)
                    pom.dependencies.removeAll(dependencies)//每个module独立发布,移除项目内的module依赖。例如:implementation project(path: ':ipw_0430')
                }
            }
        }
    }

//    写法(参考LegoArch):
    if (project.hasProperty("android")) {
        println("--all--->>android")

        task androidJavadoc(type: Javadoc) {
            options {
                encoding "UTF-8"
                charSet 'UTF-8'
                links "http://docs.oracle.com/javase/8/docs/api/"
                linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
            }
            destinationDir = file("./javadoc/")
            source = android.sourceSets.main.java.srcDirs
            classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
            exclude('**/BuildConfig.java',
                    '**/R.java',
                    '**/*.aidl')
            failOnError = false
        }

        task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
            classifier = 'javadoc'
            from androidJavadoc.destinationDir
        }

        task androidSourceJar(type: Jar) {
            classifier = 'sources'
            from android.sourceSets.main.java.srcDirs
        }
        artifacts {
            archives androidSourceJar
            archives androidJavadocJar
        }
    }
}

4.3 The build.gradle of each module refers to the configuration file release-aar.gradle:

android{...}
apply from: '../release-aar.gradle'

4.4. Execute the uploadArchives command separately to publish.

5. How to publish when there is a module dependency (wording two):

Recommend this way of writing

5.1. Add the following configuration to the project's build.gradle:

subprojects {
    apply plugin: 'maven'
}

ext {
    GROUP_ID = 'com.kedacom.vehiclecheck'

//上传的目标仓库地址
    SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
    RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'

//Nexus 的私服的用户名称和密码
    NEXUS_USERNAME = 'admin'
    NEXUS_PASSWORD = 'admin123'

    appVersionName = '1.1.4'
    appModuleName = 'app'

    ipwVersionName = '1.1.0'
    ipwModuleName = 'ipw_0430'
}

5.2、Root directory Create a new general configuration file release-aar.gradle with the following content:

//apply plugin: 'maven' //此处不需要了
def mExt = rootProject.ext

afterEvaluate { project ->

    uploadArchives {
//        configuration = configurations.archives
        repositories {
            mavenDeployer {
                def REPOSITORY_URL =  mExt.appVersionName.toUpperCase().endsWith("-SNAPSHOT")? mExt.SNAPSHOT_REPOSITORY_URL : mExt.RELEASE_REPOSITORY_URL
                println 'REPOSITORY_URL : ' + REPOSITORY_URL

                repository(url: REPOSITORY_URL) {
                    authentication(userName: mExt.NEXUS_USERNAME, password: mExt.NEXUS_PASSWORD)
                    pom.groupId = mExt.GROUP_ID
//                    pom.artifactId = ARTIFACT_ID
//                    pom.version = VERSION_NAME
                }
            }
        }
    }

    if (project.hasProperty("android")) {
        println("--all--->>android")

        task androidJavadoc(type: Javadoc) {
            options {
                encoding "UTF-8"
                charSet 'UTF-8'
                links "http://docs.oracle.com/javase/8/docs/api/"
                linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
            }
            destinationDir = file("./javadoc/")
            source = android.sourceSets.main.java.srcDirs
            classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
            exclude('**/BuildConfig.java',
                    '**/R.java',
                    '**/*.aidl')
            failOnError = false
        }

        task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
            classifier = 'javadoc'
            from androidJavadoc.destinationDir
        }

        task androidSourceJar(type: Jar) {
            classifier = 'sources'
            from android.sourceSets.main.java.srcDirs
//            from android.sourceSets.main.java.sourceFiles//不同写法
        }
        artifacts {
            archives androidSourceJar
            archives androidJavadocJar
        }
    }

    if (project.hasProperty("dependencies")) {
        println("--all--->>dependencies")
    }

}

5.3. Add the following configuration in the build.gradle of each submodule:

Note: Different modules use different versions and moduleName.

android{...}

apply from: '../release-aar.gradle'

uploadArchives {
    repositories {
        mavenDeployer {

            pom.version = "${ipwVersionName}"
            pom.artifactId = "${ipwModuleName}"
            pom.name = "${ipwModuleName}"
            pom.packaging = 'aar'

            pom.whenConfigured { MavenPom pom ->
                def dependencies = pom.dependencies.findAll { item ->
                    ("unspecified" == item.version)
                }

                pom.dependencies.removeAll(dependencies)
            }
        }
    }
}

5.4. Execute the uploadArchives command separately to publish.

6. Reference link:

Warehouse property description and modification configuration

Android Studio upload aar to private maven repository

When the project depends, upload aar

Gradle combat: release aar package to maven repository (similar to LegoArch)

Published 45 original articles · Like 24 · Visits 50,000+

Guess you like

Origin blog.csdn.net/zhijiandedaima/article/details/90779782