优化的maven上传任务
2020-11-25 本文已影响0人
jxcyly1985
ext {
algo_groupId = 'com.ext.3rdalgointerface'
algo_artifactId = 'algo-common'
algo_version = '1.0.1.1'
baseName = "GoogleCameraSDK"
groupId = "com.example.camera"
artifactId = "gcamsdk-temp"
version = '1.0.1.20'
abbreviatedCommitId = ''
fullCommitId = ''
pom = [name : "GoogleCameraSDK",
packaging : "aar",
description: "camera util camera easy usage"]
repository = [
[name : 'Vivo', url: 'http://maven.example/nexus/content/repositories/public/',
credentials: [username: 'prefix', password: 'prefix']]
]
}
apply plugin: 'maven-publish'
def deleteVersionFile() {
def path = getCommitInfoFilePath()
println("deleteVersionFile path " + path)
def versionFile = file(path)
if (versionFile.exists()) {
versionFile.delete()
}
}
def getAssetsPath() {
def appExtension = project.getExtensions().getByName('android')
def sourceSets = appExtension.getProperties().get('sourceSets')
def androidSourceSet = sourceSets.getByName('main')
def assets = androidSourceSet.assets.toString()
String innerPath = "${projectDir}" + "/" + assets.substring(1, assets.length() - 1)
println('getAssetsPath =' + innerPath)
return innerPath
}
def getCommitInfoFilePath() {
def assetsPath = getAssetsPath()
def path = assetsPath + "/" + project.ext.baseName + "_" + project.ext.artifactId + "_" + project.ext.version + ".txt"
println("createVersionFile path " + path)
return path
}
def createVersionFile() {
project.ext.fullCommitId = "git log -n 1 --pretty=%H".execute().text.trim()
def path = getCommitInfoFilePath()
println("createVersionFile path " + path)
def versionFile = file(path)
if (!versionFile.getParentFile().exists()) {
versionFile.getParentFile().mkdirs()
}
versionFile.createNewFile()
versionFile.append(project.ext.fullCommitId.getBytes())
}
task('checkCodeClean') {
doLast {
'git pull --rebase'.execute()
def status = 'git status'.execute().text.trim()
println("checkCodeClean git status =" + status)
if (status.contains('branch is up to date with') && status.contains('working tree clean')) {
println("Your branch is up to data and working tree clean")
} else {
throw new RuntimeException('check your code is sync with gerrit!')
}
}
}
project.afterEvaluate {
//上传 Algo SDK AAR 配置
publishing.publications.create('VivoAlgoSDKRelease', MavenPublication) {
groupId = project.ext.algo_groupId
artifactId = project.ext.algo_artifactId
version = project.ext.algo_version
println('version ' + version)
artifact file("./libs/Vivo3rdAlgoInterface.aar")
pom {
name = 'VivoAlgoSDK'
packaging = 'aar'
description = 'vivo algo sdk for vivo camera sdk use'
}
}
def debugVariants = android.libraryVariants.findAll { element ->
//println("element " + element.properties)
element.buildType.name == 'debug'
}
println("debugVariants size " + debugVariants.size())
def ReleaseVariants = android.libraryVariants.findAll { element ->
//println("element " + element.properties)
element.buildType.name == 'release'
}
println("ReleaseVariants size " + ReleaseVariants.size())
def libVariants = debugVariants + ReleaseVariants
def repoCount = project.ext.repository.size()
println("repoCount " + repoCount);
for (int i = 0; i < repoCount; ++i) {
publishing.repositories.maven {
name = project.ext.repository[i]['name']
url = project.ext.repository[i]['url']
credentials {
username = project.ext.repository[i]['credentials']['username']
password = project.ext.repository[i]['credentials']['password']
println('username ' + username + ' password ' + password)
}
authentication {
basic(BasicAuthentication)
}
println('name ' + name + ' url ' + url)
}
}
def flavors = android.productFlavors
flavors.all { flavor ->
println("flavor " + flavor.name)
}
project.ext.abbreviatedCommitId = "git log -n 1 --pretty=%h".execute().text.trim()
//针对不同的版本添加不同的出版物
def publishingTaskNames = new HashMap<Object, Object>()
def artifactFile = new HashMap<Object, Object>()
for (variant in libVariants) {
def buildType = variant.buildType.name
buildType = buildType.substring(0, 1).toUpperCase() + buildType.substring(1)
def flavorName = variant.flavorName
if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
flavorName = flavorName.substring(0, 1).toUpperCase() + flavorName.substring(1)
}
def publicationName = project.ext.baseName + flavorName + buildType
// Android打包Aar的任务
def taskName = 'bundle' + flavorName + buildType + 'Aar'
artifactFile.put(publicationName, variant.outputs[0].outputFile)
// to support 1.0.+
def publicationVersion = ''
if (buildType == 'Release') {
if (flavorName.isEmpty()) {
publicationVersion = project.ext.version
} else {
publicationVersion = flavorName + '_' + project.ext.version
}
} else {
if (flavorName.isEmpty()) {
publicationVersion = project.ext.abbreviatedCommitId + '_' + buildType + "_" + project.ext.version
} else {
publicationVersion = project.ext.abbreviatedCommitId + '_' + flavorName + "_" + buildType + "_" + project.ext.version
}
}
publishing.publications.create(publicationName, MavenPublication) {
groupId = project.ext.groupId
artifactId = project.ext.artifactId
version = publicationVersion
println('version ' + version)
artifact tasks.getByName(taskName)
pom {
name = project.ext.pom['name']
packaging = project.ext.pom['packaging']
description = project.ext.pom['description']
}
pom.withXml {
def addNode = { rootNode, list ->
list.each {
if (null != it.group) {
def dependency = rootNode.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
dependency.appendNode('scope', 'compile')
}
}
}
def rootNode = asNode().appendNode('dependencies')
// 查询所有 implementation, api, compile 依赖
def implementations = configurations.findByName("implementation").allDependencies
// 查询所有 variantNameImplementation, api, compile 依赖
//def variantNameImplementations = configurations.findByName("${variantName}Implementation").allDependencies
// 遍历添加依赖节点
addNode(rootNode, implementations)
//addNode(rootNode, variantNameImplementations)
def dependency = rootNode.appendNode('dependency')
dependency.appendNode('groupId', project.ext.algo_groupId)
dependency.appendNode('artifactId', project.ext.algo_artifactId)
dependency.appendNode('version', project.ext.algo_version)
dependency.appendNode('scope', 'compile')
}
}
def appDeleteVersionTask = 'merge' + buildType + 'JniLibFolders'
tasks.getByName(appDeleteVersionTask) {
doLast {
println("deleteVersionFile for task:" + taskName)
deleteVersionFile()
}
//强制每次都运行
outputs.upToDateWhen { false }
}
for (int i = 0; i < repoCount; ++i) {
def publishTaskName = 'publish' + publicationName + 'PublicationTo' + project.ext.repository[i]['name'] + 'Repository'
println("publishTaskName " + publishTaskName)
publishingTaskNames.put(publishTaskName, publicationName)
}
def publishLocalTaskName = 'publish' + publicationName + 'PublicationTo' + 'MavenLocal'
publishingTaskNames.put(publishLocalTaskName, publicationName)
}
//在android原生preBuild基础上添加到assets内部增加commit文件
tasks.getByName("preBuild").doFirst {
createVersionFile()
println("preBuild createVersionFile")
}
def pubToVivoTasks = tasks.withType(PublishToMavenRepository) {
onlyIf {
println("PublishToMavenRepository publication.name " + publication.name)
publication.name.contains('Release')
}
dependsOn('checkCodeClean')
mustRunAfter('checkCodeClean')
}
for (taskName in publishingTaskNames.keySet()) {
println("taskName:" + taskName)
def aarFile = artifactFile[publishingTaskNames[taskName]]
println("addVersionFile aarFile:" + aarFile)
tasks.getByName(taskName).doLast {
println("deleteVersionFile for task:" + taskName)
deleteVersionFile()
}
}
def pubToLocalTasks = tasks.withType(PublishToMavenLocal) {
onlyIf {
println("PublishToMavenLocal publication.name " + publication.name)
publication.name.contains('Debug')
}
}
pubToVivoTasks.all {
if (publication.name.contains('Release') && publication.name.contains(project.ext.baseName)) {
setGroup('uploadVCamSDK-Vivo')
}
}
pubToLocalTasks.all {
if (publication.name.contains('Debug') && publication.name.contains(project.ext.baseName)) {
setGroup('uploadVCamSDK-Local')
}
}
tasks.getByName('publishVivoAlgoSDKReleasePublicationToVivoRepository').setGroup('uploadAlgoSDK-Vivo')
}