本地部署Sonatype nexus实现本地module上传到私有maven仓库

168 阅读1分钟

本地部署好Sonatype nexus后,环境已经好了。现在需要验证编译module并上传到本地maven仓库。

apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
    publishing {
        publications {
            // studyMaven 可以自定义名字
            studyMaven(MavenPublication) {
                // maven格式:com.example.maventest:common:1.1.0-SNAPSHOT
                groupId = "com.example.maventest"
                artifactId = "common"
                version = "1.0.0"
                // components.all包含了 debug、release
                from components.release
                // 如果要同时生成jar包,可使用下面注释
                //  artifact sourceJar
            }
        }

        repositories {
            maven {
                allowInsecureProtocol = true
                url 'http://localhost:8081/repository/maven-releases/'
                credentials {
                    username = nexusUsername
                    password = nexusPassword
                }
            }
        }
    }
}

这里上传没有问题,一切正常。

但是拉取依赖的时候,没有反应。原因是部署Sonatype nexus的时候,Configure Anonymous Access如果选择的是否的话,就代表不允许所有的用户下载,所以需要在工程根目录的build.gradle中,对应的本地maven仓库地址处(包括allprojects配置),增加用户名username和password的配置:

maven {
    url 'http://localhost:8081/repository/maven-releases/'
    credentials {
        username = nexusUsername
        password = nexusPassword
    }
}

重新拉取,正常了。