构建

时间:2016-04-01 11:28:54

标签: android maven gradle android-gradle

我有uploadArhives到Maven存储库.aar发布。

但我必须始终从控制台运行gradlew uploadArhives,如何编写代码以使其在每次构建或发布版本时调用?

uploadArchives {
    repositories {
        mavenDeployer {
            def credentials = [
                    userName: NEXUS_USERNAME,
                    password: NEXUS_PASSWORD
            ]
            repository(url: MAVEN_REPO_URL, authentication: credentials)
            pom.artifactId = 'aaa'
            pom.version = version
            pom.packaging = 'aar'
            pom.groupId = 'bbb'

        }
    }
}

编辑

我认为,我们可以定义函数:

def uploadToMaven = {
    uploadArchives
}

但是如何在每次构建时执行它?

2 个答案:

答案 0 :(得分:3)

我有一个包含许多模块和一个主要应用程序的复杂项目。 我添加了#34; uploadArchives"在其中两个模块上(因为是android库)。通过这种方式,我可以在Maven上发布我的库,只需从我的主应用程序运行任务uploadArchives,或者使用gradle并调用此任务" uploadArchives"。

您可以在build.gradle(您要发布的库中)和#34; build.finalizedBy(uploadArchives)"。

中使用它。

例如:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 2
        versionName "2.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    lintOptions {
        abortOnError false
    }


}


build.finalizedBy(uploadArchives)

task wrapper(type: Wrapper) {
    gradleVersion = "2.8"
}

dependencies {
    compile project(':spfshared')
    compile 'com.google.code.gson:gson:2.4'
}

//task for Sonatype Nexus OSS
uploadArchives {
    repositories {
        mavenDeployer {
            repository(
                    url: "${nexusUrl}/content/repositories/releases") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(
                    url: "${nexusUrl}/content/repositories/snapshots") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }

            pom.version = "2.0.0.1"
            pom.artifactId = "spflib"
            pom.groupId = "it.polimi.spf"
        }
    }
}

每次构建后,uploadArchives都会启动。

我尝试了这个解决方案并且有效。

我还尝试了一些解决方案" build.dependsOn myTaskName"没有成功。如果你想要,你可以尝试,但在我的AndroidStudio上,它是第一个有效的解决方案。

PS:我使用命令" gradlew -q build"测试了我的解决方案。并且还专门运行任务" build"来自我在Android Studio中的主要模块(它是我的主要应用程序)。

如果你想打电话" uploadArchives"在每个版本中,只需替换" build"与发布任务。

<强>更新 我也试过这些代码行:

defaultTasks 'uploadArchives'

clean.finalizedBy(uploadArchives)
assembleDebug.finalizedBy(uploadArchives)
assembleRelease.finalizedBy(uploadArchives)

但有时他们会打电话给#34; uploadArchives&#34;很多次,我认为这不是一个好主意。

你问的是非常具有挑战性的......我试了整整一个小时:)

答案 1 :(得分:2)

Just add this line to your build.gradle:

build.finalizedBy(uploadArchives)

This creates a task dependency between build task and uploadArchives task, such that uploadArchives is automatically called everytime build executes successfully.