1. 程式人生 > >Android Studio上傳專案到Maven倉庫的方法

Android Studio上傳專案到Maven倉庫的方法

1.在專案根目錄下新建uploadConfig.properties配置檔案:

USERNAME=username
PASSWORD=password
GROUP_ID=com.test.library
MAVEN_REPO_SNAPSHOT_URL=http://192.168.86.130:8081/repository/{snapshot倉庫名}/
MAVEN_REPO_RELEASE_URL=http://192.168.86.130:8081/repository/{release倉庫名}/
TYPE=aar

在專案根目錄的build.gradle檔案中載入配置檔案:

def parseLocalProperties(){
    File file = rootProject.file('uploadConfig.properties')
    if(file.exists()){
        InputStream inputStream = rootProject.file('uploadConfig.properties').newDataInputStream();
        Properties properties = new Properties()
        properties.load(inputStream)

        if(properties.containsKey("MAVEN_REPO_SNAPSHOT_URL")){
            ext.MAVEN_REPO_SNAPSHOT_URL = properties.getProperty("MAVEN_REPO_SNAPSHOT_URL")
            ext.MAVEN_REPO_RELEASE_URL = properties.getProperty("MAVEN_REPO_RELEASE_URL")
            ext.GROUP_ID = properties.getProperty("GROUP_ID")
            ext.TYPE = properties.getProperty("TYPE")
            ext.USERNAME = properties.getProperty("USERNAME")
            ext.PASSWORD = properties.getProperty("PASSWORD")
        }
    }
}

allprojects {
    ...
    parseLocalProperties()
}

2. 在要上傳maven的module下面新建上傳指令碼:

apply plugin: 'maven'
apply plugin: 'signing'

def VERSION="1.0.0"
def ARTIFACT_ID="test"

configurations {
    deployerJars
}

repositories {
    mavenCentral()
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment {
                MavenDeployment deployment -> signing.signPom(deployment)
            }

            pom.version = VERSION
            pom.artifactId = ARTIFACT_ID
            pom.groupId = project['GROUP_ID']

            repository(url: project['MAVEN_REPO_RELEASE_URL']) {
                // maven授權資訊
                authentication(userName: project['USERNAME'], password:  project['PASSWORD'])
            }
            snapshotRepository(url: project['MAVEN_REPO_SNAPSHOT_URL']) {
                authentication(userName: project['USERNAME'], password:  project['PASSWORD'])
            }
        }
    }
}

// 生成sources.jar
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

// 產生相關配置檔案的任務
artifacts {
    archives androidSourcesJar
}

// 進行數字簽名
signing {
    required { gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

3.在要上傳maven的module下面的build.gradle中應用指令碼:

apply from: 'maven_push.gradle'

4.重新整理同步一下gradle,右邊的gradle任務當中多了一個upload任務:


雙擊執行或者右鍵run即可。

上傳成功後就可以使用了:

implementation "com.test.library:test:1.0.0"

記得把倉庫地址配置到repositories當中。

上傳過程中還有個問題就是,如果你要上傳的module還依賴了本地其它module,那麼需要把本地依賴改成遠端依賴後再上傳:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //implementation project(':libraryProjectA')
    implementation "com.test.lib:test:1.0.0"
}

把implementation project方式引用的統統都要換成遠端依賴的方式,不然最終使用的時候會報錯,找不到你本地依賴的庫。這也就意味著你可能要先上傳本地依賴的那個庫,拿到maven依賴後再上傳依賴它的庫。

另外就是在上傳的時候,如果你是有多個模組要傳,注意當前傳哪個module就在哪個module中的gradle應用上傳指令碼,上傳完了把apply那句註釋掉,其它的不用上傳的module都註釋掉這句,否則當你執行upload任務時所有的模組都會給你傳一遍。