如何使用artifactoryPublish发布发布和debugging工件

我有Android Studio项目,在发行版和debugging版本中构buildAAR或APK。 我想在Artifactory服务器上将这些发布到不同的存储库。 JFrog的例子似乎没有涵盖这种情况。

这是否意味着最好的做法是仅仅构build版本或者只是构builddebugging版本,然后根据构build的typesselect要上传的内容和上传的位置?

我configuration了我的android库build.gradle文件,编译的aar文件可以上传到不同的仓库,依赖于构buildtypes。
例如,您想要将debuggingartifats发布到“libs-debug-local”存储库,并将工件发布到“libs-release-local”存储库。

//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'] } } } 

就这样。 现在如果你运行命令

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

aar文件将被上传到“libs-release-local”版本库。
如果你跑了

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

它会被上传到“libs-debug-local”仓库

这个configuration的一个减号是,你应该总是使用assembleDebug / Release任务运行artifactoryPublish任务

对于快照+发行版本,您可以使用sonatype(又名mavencentral)。 几个星期前,我做了一个简短的指导,你可能会发现它有一些用处。 如何发布Android AAR – 快照/发布到Mavencentral

无法使用@cooperok答案发布工作,否则它帮助我很多。

这是我的代码:

 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' } 

尝试这个:-

 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 } } } 

其中artifactory_repository_release = libs-release-local和artifactory_repository_debug = libs-debug-local

你想发布你的库arr的artifactory回购。

gradle更新为' com.android.tools.build:gradle:3.xx '后, 这不再适用于我。

我的最终解决scheme是:

 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's 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") }