android studio發布公共類庫到服務器maven倉庫


在上一篇中提到了怎么創建私有maven庫,這篇主要結合android studio的使用,直接進入正題,看以下步驟

1.創建android項目

創建Project,然后加一個library的module,此處省略一萬字了

2.配置gradle

在項目的gradle.properties里面配置基本信息

GROUP=對應maven的groupId值,如果名字中包含SNAPSHOT字符,項目將會發布到snapshots倉庫,沒有則發布到releases倉庫  
VERSION_NAME=對應maven的version值  
POM_ARTIFACT_ID=對應maven的artifactId值  
SNAPSHOT_REPOSITORY_URL=前面配置的snapshots倉庫地址  
RELEASE_REPOSITORY_URL=前面配置的releases倉庫地址  
NEXUS_USERNAME=登錄nexus oss的用戶名
NEXUS_PASSWORD=登錄nexus oss的密碼

在module的build.gradle末尾加入下面代碼

apply plugin: 'maven'
def isReleaseBuild() {
    return VERSION_NAME.contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                pom.groupId = GROUP
                pom.artifactId = POM_ARTIFACT_ID
                pom.version = VERSION_NAME
                repository(url: RELEASE_REPOSITORY_URL) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
                snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
            }
        }
    }
    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
    }
    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

3.發布公共庫

./gradlew clean build uploadArchives

就一條命令,最后成功就OK了,如果失敗根據具體情況找原因。

然后到nexus上查看是否上傳成功

4.使用

在project中的build.gradle中的allprojects->repositories加入

maven { url "http://192.168.59.103:8081/content/repositories/releases/" }

在module的build.gradle中的dependencies加入

compile 'groupId:artifactId:version'
groupId,artifactId,version換成你自己的

build一切OK。

如有疑問歡迎交流


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM