gradle maven私服构建aar

 gradle  对maven仓库的依赖非常好,所以在使用aar之前,需要将aar上传到maven仓库中,(一般都是部门自己搭建的私有maven仓库,如何搭建此处跳过)。 

1.首先构建push的gradle 脚本  xxx-gradle-mvn-push.gradle


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

def isReleaseBuild() {
    return VERSION_NAME.contains("SNAPSHOT") == false
}

def getReleaseRepositoryUrl() {
    return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL :
            'http://172.16.49.190:8081/nexus/content/repositories/xxx-android-release/'
}

def getSnapshotRepositoryUrl() {
    return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL :
            'http://172.16.49.190:8081/nexus/content/repositories/xxx-android-snapshot/'
}

def getRepositoryUsername() {
    return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ''
}

def getRepositoryPassword() {
    return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ''
}

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

                pom.groupId = POM_GROUP_ID
                pom.artifactId = POM_ARTIFACT_ID
                pom.version = VERSION_NAME
                pom.packaging = POM_PACKAGING

                repository(url: getReleaseRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
                snapshotRepository(url: getSnapshotRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                pom.project {
                    name POM_NAME
                    description POM_DESCRIPTION

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            name POM_DEVELOPER_NAME
                            id POM_DEVELOPER_ID
                            email POM_DEVELOPER_EMAIL
                        }
                    }
                }
            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task sourceCode(type: Zip) {
        appendix = extension = 'src'
        from 'AndroidManifest.xml'
        into('res') {
            from 'res'
        }
        into('src') {
            from 'src'
        }
        into('libs') {
            from 'libs'
        }
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath += files(ext.androidJar)
    }

    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 sourceCode
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}


2.gradle.properties配置pom以及Setting(这里从我的工程上copy示例,标签大家自己去查,就不一一注释了)

# POMject-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# Specifies the JVM arguments used for the daemon POMcess.
# The setting is particularly useful for tweaking memory settings.
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled POMjects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_POMject_builds.html#sec:decoupled_POMjects

# gradle build setting

# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# org.gradle.configureondemand=true
# org.gradle.parallel=true
# org.gradle.daemon=true
# android.useDeprecatedNdk=true

# Android build setting

BUILD_MIN_SDK_VERSION=9
BUILD_TARGET_SDK_VERSION=23
BUILD_SDK_VERSION=23
BUILD_TOOLS_VERSION=23.0.1

# Nexus maven setting

#signing.keyId=XXXXXXXXX
#signing.password=XXXXXXXXX
#signing.secretKeyRingFile=/Users/.../secring.gpg
NEXUS_USERNAME=xxx
NEXUS_PASSWORD=xxx
#NEXUS_USERNAME=xxx
#NEXUS_PASSWORD=xxx

#VERSION_NAME=2.1.2
VERSION_NAME=2.1.9-SNAPSHOT
VERSION_CODE=152

POM_GROUP_ID=xxx
POM_ARTIFACT_ID=xxx
POM_PACKAGING=aar
POM_NAME=xxx
POM_DESCRIPTION=xx base dependences library to android program.

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=xxx

POM_DEVELOPER_ID=xxx
POM_DEVELOPER_NAME=xxx
POM_DEVELOPER_EMAIL=xxx


3.在子moudle 的build.gradle中关联刚刚的配置 

apply from: './dfjk-gradle-mvn-push.gradle'


4.执行发布(记得先刷新一下)

gradle maven私服构建aar_第1张图片


5.最后你就可以在新的工程中使用刚才的aar了

  在新工程的build.gradle文件中加上:

    compile 'com.xxx.xxx.'  

你可能感兴趣的:(gradle)