How to use artifactoryPublish to publish releases and debug artifacts

I have Android Studio projects that create AAR or APK in release and debug versions. I would like to publish them differently in different repositories on my Artifactory server. JFrog's examples do not seem to cover this case.

Does this mean that it is considered best practice to simply create either just a version or just a debug version, and choose what and where to load based on the type of assembly?

+7
android maven artifactory gradle
source share
5 answers

I configured my build.gradle file for the Android library, which compiled the aar file, can be downloaded in different repositories, depending on the type of assembly.
For example, you want to publish debug artifacts to the libs-debug-local repository and release the artifacts to the libs-release-local repository.

//First you should configure all artifacts you want to publish publishing { publications { //Iterate all build types to make specific //artifact for every build type android.buildTypes.all { variant -> //it will create different //publications ('debugAar' and 'releaseAar') "${variant.name}Aar"(MavenPublication) { def manifestParser = new com.android.builder.core.DefaultManifestParser() //Set values from Android manifest file groupId manifestParser.getPackage(android.sourceSets.main.manifest.srcFile) version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) artifactId project.getName() // Tell maven to prepare the generated "*.aar" file for publishing artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar") } } } } //After configuring publications you should //create tasks to set correct repo key android.buildTypes.all { variant -> //same publication name as we created above def publicationName = "${variant.name}Aar" //new task name def taskName = "${variant.name}Publication" //in execution time setting publications and repo key, dependent on build type tasks."$taskName" << { artifactoryPublish { doFirst { publications(publicationName) clientConfig.publisher.repoKey = "libs-${variant.name}-local" } } } //make tasks assembleDebug and assembleRelease dependent on our new tasks //it helps to set corrent values for every task tasks."assemble${variant.name.capitalize()}".dependsOn(tasks."$taskName") } //Inside artifactory block just set url and credential, without setting repo key and publications artifactory { contextUrl = 'http://artifactory.cooperok.com:8081/artifactory' publish { repository { username = "username" password = "password" } defaults { publishArtifacts = true // Properties to be attached to the published artifacts. properties = ['qa.level': 'basic', 'dev.team': 'core'] } } } 

It's all. Now if you run the command

Win: gradlew assembleRelease artifactoryPublish Mac: ./ gradlew assembleRelease artifactoryPublish

The aar file will be uploaded to the 'libs-release-local' repository.
And if you run

Win: gradlew assembleDebug artifactoryPublish Mac: ./ gradlew assembleDebug artifactoryPublish

it will be uploaded to the 'libs-debug-local' repository

One minus of this configuration is that you should always run the artifactoryPublish task with assembleDebug / Release tasks

+8
source share

For snapshots + release versions, you can use the sonatype (aka mavencentral). I made a short trip a couple of weeks ago that you could find. How to Publish Android AARs - Snapshots / Release in Mavencentral

0
source share

Unable to get publication of work with @cooperok answer, otherwise it will help me a lot.

Here is my code:

 apply plugin: 'com.android.library' apply plugin: 'com.jfrog.artifactory' apply plugin: 'maven-publish' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { minSdkVersion 9 targetSdkVersion 23 versionCode 1 versionName "0.0.1" } publishNonDefault true buildTypes { debug { minifyEnabled false debuggable true } release { minifyEnabled false debuggable false } snapshot { minifyEnabled false debuggable false } } } task androidJavadocs(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { classifier = 'javadoc' from androidJavadocs.destinationDir } task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } artifacts { archives androidSourcesJar archives androidJavadocsJar } publishing { publications { android.buildTypes.all { variant -> "${variant.name}"(MavenPublication) { def manifestParser = new com.android.builder.core.DefaultManifestParser() groupId manifestParser.getPackage(android.sourceSets.main.manifest.srcFile) if("${variant.name}".equalsIgnoreCase("release")){ version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) }else if ("${variant.name}".equalsIgnoreCase("debug")){ version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile).concat("-${variant.name}".toUpperCase().concat("-SNAPSHOT")) }else{ version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile).concat("-${variant.name}".toUpperCase()) } artifactId project.getName() artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar") artifact androidJavadocsJar pom.withXml { def dependencies = asNode().appendNode('dependencies') configurations.getByName("_releaseCompile").getResolvedConfiguration().getFirstLevelModuleDependencies().each { def dependency = dependencies.appendNode('dependency') dependency.appendNode('groupId', it.moduleGroup) dependency.appendNode('artifactId', it.moduleName) dependency.appendNode('version', it.moduleVersion) } } } } } } android.buildTypes.all { variant -> model { tasks."generatePomFileFor${variant.name.capitalize()}Publication" { destination = file("$buildDir/publications/${variant.name}/generated-pom.xml") } } def publicationName = "${variant.name}" def taskName = "${variant.name}Publication" task "$taskName"() << { artifactoryPublish { doFirst { tasks."generatePomFileFor${variant.name.capitalize()}Publication".execute() publications(publicationName) clientConfig.publisher.repoKey = "${variant.name}".equalsIgnoreCase("release") ? "libs-release-local" : "libs-snapshot-local" } } } tasks."assemble${variant.name.capitalize()}".dependsOn(tasks."$taskName") } artifactory { contextUrl = 'http://172.16.32.220:8081/artifactory' publish { repository { username = "admin" password = "password" } defaults { publishPom = true publishArtifacts = true properties = ['qa.level': 'basic', 'dev.team': 'core'] } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' } 
0
source share

try the following: -

 def runTasks = gradle.startParameter.taskNames artifactory { contextUrl = "${artifactory_contextUrl}" publish { repository { if ('assembleRelease' in runTasks) repoKey = "${artifactory_repository_release}" else if ('assembleDebug' in runTasks) repoKey = "${artifactory_repository_debug}" username = "${artifactory_user}" password = "${artifactory_password}" maven = true } defaults { publishArtifacts = true if ('assembleRelease' in runTasks) publications("${artifactory_publication_release}") else if ('assembleDebug' in runTasks) publications("${artifactory_publication_debug}") publishPom = true publishIvy = false } } } 

where artifactory_repository_release = libs-release-local and artifactory_repository_debug = libs-debug-local

artifactory repo where you want to publish your arr library.

0
source share

After upgrading gradle to com.android.tools.build: gradle: 3.xx ' this one no longer works for me.

My final decision was:

 artifactory { contextUrl = ARTIFACTORY_URL //The base Artifactory URL if not overridden by the publisher/resolver publish { repository { File debugFile = new File("$buildDir/outputs/aar/${SDK_NAME}-debug.aar"); if ( debugFile.isFile() ) repoKey = 'libs-snapshot-local' else repoKey = 'libs-release-local' username = ARTIFACTORY_USER password = ARTIFACTORY_PWD maven = true } defaults { File debugFile = new File("$buildDir/outputs/aar/${SDK_NAME}-debug.aar"); if ( debugFile.isFile() ) publications("debugAar") else publications("releaseAar") publishArtifacts = true // Properties to be attached to the published artifacts. properties = ['qa.level': 'basic', 'dev.team': 'core'] // Is this even necessary since it TRUE by default? // Publish generated POM files to Artifactory (true by default) publishPom = true } } } publishing { publications { //Iterate all build types to make specific //artifact for every build type android.buildTypes.all {variant -> //it will create different //publications ('debugAar' and 'releaseAar') "${variant.name}Aar"(MavenPublication) { writeNewPom(variant.name) groupId GROUP_NAME artifactId SDK_NAME version variant.name.endsWith('debug') ? VERSION_NAME + "-SNAPSHOT" : VERSION_NAME // Tell maven to prepare the generated "*.aar" file for publishing artifact("$buildDir/outputs/aar/${SDK_NAME}-${variant.name}.aar") } } } } def writeNewPom(def variant) { pom { project { groupId GROUP_NAME artifactId SDK_NAME version variant.endsWith('debug') ? VERSION_NAME + "-SNAPSHOT" : VERSION_NAME packaging 'aar' licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' } } } }.withXml { def dependenciesNode = asNode().appendNode('dependencies') configurations.api.allDependencies.each {dependency -> if (dependency.group != null) { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', dependency.group) dependencyNode.appendNode('artifactId', dependency.name) dependencyNode.appendNode('version', dependency.version) } } }.writeTo("$buildDir/publications/${variant}Aar/pom-default.xml") } 
0
source share

All Articles