apply plugin: 'maven-publish'
apply plugin: 'signing'

// 此处不需修改,下面会读取 local.properties 中配置的信息
ext["PUBLISH_VERSION"] = '' //发布的版本
ext["PUBLISH_GROUP_ID"] = ''  //分组ID
ext["PUBLISH_ARTIFACT_ID"] = '' //
ext["signing.keyId"] = '' //签名的密钥后8位
ext["signing.password"] = ''  //签名设置的密码
ext["signing.secretKeyRingFile"] = '' //生成的secring.gpg文件目录
ext["ossrhUsername"] = ''  //sonatype用户名
ext["ossrhPassword"] = ''  //sonatype密码
// 遍历赋值
File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    println "Found secret props file, loading props"
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
    ext["PUBLISH_ARTIFACT_ID"]="oaid_sdk_1.0.25"
    ext["PUBLISH_VERSION"]="1.0.1"
} else {
    println "No props file, loading env vars"
}

afterEvaluate {
    publishing {
        repositories {
            maven {
                //推送至远端的中央仓库,一旦发布release中央仓库版本,旧版本无法修改
                //一般都在  暂存库  中进行测试,然后确认无误后再发布到 release中央仓库
                allowInsecureProtocol = false
                name = PUBLISH_ARTIFACT_ID
                // 暂存库
                def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
                // 快照库(版本名以 SNAPSHOT 结尾,就推送至快照库)
                def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

                credentials {
                    username ossrhUsername
                    password ossrhPassword
                }
            }
            maven {
                // 推送至本地存储库,本机测试的时候可以用
                allowInsecureProtocol = false
                name = 'Local'
                url = uri('../ZrgkLocalRepo')
            }
        }
        publications {
            release(MavenPublication) {
                println("publish-maven Log-------> PUBLISH_GROUP_ID: $PUBLISH_GROUP_ID; PUBLISH_ARTIFACT_ID: $PUBLISH_ARTIFACT_ID; PUBLISH_VERSION: $PUBLISH_VERSION")

                groupId PUBLISH_GROUP_ID
                artifactId PUBLISH_ARTIFACT_ID
                version PUBLISH_VERSION

                // 生成的 aar 路径,修改成自己的aar地址名称
//                artifact "$buildDir/outputs/aar/${project.name}-release.aar"
                artifact "F:\\Work_Space\\Wechat_workSpace\\duoduan_reader_test\\chajian\\android\\LocalRepo\\oaid_sdk\\oaid_sdk_1.0.25.aar"
                // 如果需要将源代码一起打包进aar,就打开此注释。
                //artifact androidSourcesJar

                pom {
                    name = PUBLISH_ARTIFACT_ID
                    description = 'efunboxMaven' //项目描述
                    url = 'https://github.com/FailedToRead/AndroidMaven' //项目github链接
                    licenses {
                        license {
                            //协议类型,一般默认Apache License2.0的话不用改:
                            name = 'The Apache License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            // 修改自己对应的用户名、邮箱
                            id = 'FailedToRead' //你的sonatype用户名
                            name = 'FailedToRead' //你的sonatype用户名
                            email = 'zhangoq9@gmail.com' //你的sonatype注册邮箱
                        }
                    }
                    // Version control info, if you're using GitHub, follow the format as seen here
                    scm {
                        //修改成你的Git地址:
                        connection = 'scm:https://github.com/FailedToRead/AndroidMaven.git'
                        developerConnection = 'scm:https://github.com/FailedToRead/AndroidMaven.git'
                        //分支地址:
                        url = 'https://github.com/FailedToRead/AndroidMaven/tree/test'
                    }

//                    pom.withXml {
//                        def dependenciesNode = asNode().appendNode('dependencies')
//                        configurations.implementation.allDependencies.each {
//                            // 避免出现空节点或 artifactId=unspecified 的节点
//                            if (it.group != null && (it.name != null && "unspecified" != it.name) && it.version != null) {
//                                println "dependency=${it.toString()}"
//                                def dependencyNode = dependenciesNode.appendNode('dependency')
//                                dependencyNode.appendNode('groupId', it.group)
//                                dependencyNode.appendNode('artifactId', it.name)
//                                dependencyNode.appendNode('version', it.version)
//                                dependencyNode.appendNode('scope', 'implementation')
//
//                            }
//                        }
//                    }


                }
            }
        }
    }
    signing {
        sign publishing.publications
    }
}


configurations.maybeCreate("default")
artifacts.add("default", file('oaid_sdk_1.0.25.aar'))