I am trying to build a script to publish APK artifacts to Nexus using the "maven-publish" plugin.
In Gradle 4 it was possible to have a configuration like this (this works):
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
url "https://$MY_NEXUS_SERVER$/repository/${project.version.endsWith('-SNAPSHOT') ? 'snapshots' : 'releases'}"
credentials {
username mavenUser
password mavenPassword
}
}
publications {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
if (variant.name == "release") {
create("apk${variant.name.capitalize()}", MavenPublication) {
groupId project.group
artifactId project.name
version project.version
artifact(output.outputFile)
}
}
}
}
}
}
}
In Gradle 5 this is no longer possible
When trying to build the project I get this error:
Cannot create a Publication named 'android' because this container does not support creating elements by name alone. Please specify which subtype of Publication to create. Known subtypes are: MavenPublication
How can I rewrite this code to be compatible with Gradle 5?
Another option for building arbitrary variants (tested on Gradle 5.5.1 / Android Gradle 3.5.0):
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
url "https://$MY_NEXUS_SERVER$/repository/${project.version.endsWith('-SNAPSHOT') ? 'snapshots' : 'releases'}"
credentials {
username mavenUser
password mavenPassword
}
}
publications {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
"maven${variant.name.capitalize()}Apk"(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
artifact(output.outputFile)
}
}
}
}
}
}
Example of resulting publish task when building with debug variant: publishMavenDebugApkPublicationToMavenRepository
I found something that works (equivalent to my code example above):
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
url "https://$MY_NEXUS_SERVER$/repository/${project.version.endsWith('-SNAPSHOT') ? 'snapshots' : 'releases'}"
credentials {
username mavenUser
password mavenPassword
}
}
publications {
create("apkRELEASE", MavenPublication) {
afterEvaluate {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
if (variant.name == "release") {
groupId project.group
artifactId project.name
version project.version
artifact(output.outputFile)
}
}
}
}
}
}
}
}
Related
I made an aar library and uploaded it locally, and then another project gradle went to download it, which prompted that it could not be downloaded
Library, aar file has been found locally
plugins {
id 'com.android.library'
id 'maven-publish'
}
def GroupId = 'intbird.soft.gradle'
def ArtifactId = 'maven-publish'
def Version = '1.0.0-SNAPSHOT'
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = GroupId
artifactId = ArtifactId
version = Version
pom {
name = "task Lib"
description = "task Lib Project Pom."
url = 'http://localhost.net'
}
}
debug(MavenPublication) {
// Applies the component for the debug build variant.
from components.debug
groupId = GroupId
artifactId = ArtifactId
version = Version
}
}
repositories {
maven {
def releasesRepoUrl = "file://C:\\repository"
def snapshotsRepoUrl = "file://C:\\repository"
url = project.hasProperty('release') ? releasesRepoUrl : snapshotsRepoUrl
}
}
}
}
===============================
My new project
Failed to resolve :intbird.soft.gradle:maven-publish:1.0.0-SNAPSHOT
application gradle code blow:
buildscript {
repositories {
maven { url "file://C:\\repository" }
mavenLocal()
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
}
}
app gradle code blow:
dependencies {
implementation 'intbird.soft.gradle:maven-publish:1.0.0-SNAPSHOT'
}
WHY???
How to publish all flavor variants with Android Gradle Plugin 7+ and maven-publish plugin?
This solution works for me:
apply plugin: 'maven-publish'
afterEvaluate {
publishing {
repositories {
maven {
url = "https://yourdomain/repository-${isSnapshot ? 'snapshots/' : 'releases/'}"
credentials {
username mavenUser
password mavenPassword
}
}
}
}
components.all((component) -> {
def componentName = component.getName()
if (componentName.endsWith("_apk")) {
println("componentName: ${componentName}")
publishing.publications.create("publication-$componentName", MavenPublication) {
from component
groupId = "com.group.id"
artifactId = "$componentName"
version = "${android.defaultConfig.versionName}"
}
}
})
}
I am using Gradle 4.1 with Gradle-Android plugin 3.0.1 on Android Studio 3.2
I have 2 flavors 'production' and 'staging' and I am unable to build my project as a library with different build variants.
app build.gradle:
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android {
...
productFlavors {
production {
}
staging {
}
}
defaultPublishConfig "productionRelease"
publishNonDefault true
}
if( android.productFlavors.size() > 0 ) {
android.libraryVariants.all { variant ->
if( android.publishNonDefault && variant.name == android.defaultPublishConfig ) {
def bundleTask = tasks["bundle${name.capitalize()}"]
artifacts {
archives(bundleTask.archivePath) {
classifier name.replace('-' + variant.name, '')
builtBy bundleTask
name name.replace('-' + variant.name, '')
}
}
}
...
Then I run: ./gradlew clean install, errors I got is:
Execution failed for task ‘:app:install’.
Could not publish configuration ‘archives’
A POM cannot have multiple artifacts with the same type and classifier. Already have MavenArtifact app:aar:aar:null, trying to add MavenArtifact app:aar:aar:null.
And to get this code to compile, I need to swap android.publishNonDefault with true, otherwise I will get an error of: Cannot get the value of write-only property 'publishNonDefault'
Any suggestions or hint would be really helpful, the aim is to build the library module on jitpack, where we can import it in project with build variants. thanks!
After digging into this for 2 days, and emailing Jitpack support, the issue is because the lib has updated and publishNonDefault is deprecated. you just need to change your app build.gradle to:
apply plugin: 'com.github.dcendents.android-maven'
dependencies {...}
group = 'com.github.your-group'
if (android.productFlavors.size() > 0) {
android.libraryVariants.all { variant ->
if (variant.name.toLowerCase().contains("debug")) {
return
}
def bundleTask = tasks["bundle${variant.name.capitalize()}"]
artifacts {
archives(bundleTask.archivePath) {
classifier variant.flavorName
builtBy bundleTask
name = project.name
}
}
}
}
problem was to create multiple flavors using jitpack
so what I do is, create a variable which stores the flavor name, after that loop through the available variant, pass the flavorBuild to artifact so when u push the code to GitHub and create artifact via jitpack then jitpack create the required implementation based on your build flavor and then u can use it. You need to just change build flavor
publishing {
publications {
def flavorBuild ='production'
android.libraryVariants.all { variant ->
"maven${variant.name.capitalize()}Aar"(MavenPublication) {
from components.findByName("android${variant.name.capitalize()}")
groupId = 'com.examplesdk'
artifactId = 'examplesdk'
version "1.0.0-${variant.name}"
artifact "$buildDir/outputs/aar/examplesdk-${flavorBuild}-release.aar"
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.api.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}
}
}
}
repositories{
maven {
url "$buildDir/repo"
}
}
}
In my pom file I have one artifact like below:
<artifactItem>
<groupId>com.xyz</groupId>
<artifactId>xyz.abc</artifactId>
<version>1.0</version>
<classifier>pqr</classifier>
<type>js</type>
<overWrite>true</overWrite>
<outputDirectory>/opt/test</outputDirectory>
<destFileName>test.js</destFileName>
</artifactItem>
How to implement this in my build.gradle. I am using gradle 2.6.
Sorry, I cannot post original code so put some unrealistic values. Please reply
WIth gradle you can use the maven plugin.
You can find the full doc for the 2.6 here.
I don't know if you can set all attributes, but you can use a script like this:
apply plugin: 'maven'
apply plugin: 'signing'
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION_NAME
repository(url: "...") {
authentication(userName: "user", password: "pass")
}
snapshotRepository(url: "...") {
authentication(userName: "user", password: "pass")
}
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}
}
signing {
required { true }
sign configurations.archives
}
artifacts {
archives xxxx
}
}
i created a gradle task
task test <<{
copy {
from --dir---
into --dir--
rename { String fileName ->
fileName.replace("xyz.abc-$1.0-pqr","user-prefs")
}
}
}
It worked for me.
Hi am getting the following error while adding facebook sdk to my project
Tried the following post for configuring the gradle script for facebook sdk
link
Error:(111) A problem occurred evaluating project ':facebook'.
> Cannot call getBootClasspath() before setTargetInfo() is called.
This is my facebook module gradle script
apply plugin: 'com.android.library'
repositories {
mavenCentral()
}
project.group = 'com.facebook.android'
dependencies {
compile 'com.android.support:support-v4:[21,22)'
compile 'com.parse.bolts:bolts-android:1.1.4'
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
minSdkVersion 18
targetSdkVersion 22
}
lintOptions {
abortOnError false
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
apply plugin: 'maven'
apply plugin: 'signing'
def isSnapshot = version.endsWith('-SNAPSHOT')
def ossrhUsername = hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
def ossrhPassword = hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
task setVersion {
// The version will be derived from source
project.version = null
def sdkVersionFile = file('src/com/facebook/FacebookSdkVersion.java')
sdkVersionFile.eachLine{
def matcher = (it =~ /(?:.*BUILD = \")(.*)(?:\".*)/)
if (matcher.matches()) {
project.version = matcher[0][2]
return
}
}
if (project.version.is('unspecified')) {
throw new GradleScriptException('Version could not be found.', null)
}
}
uploadArchives {
repositories.mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
authentication(userName: ossrhUsername, password: ossrhPassword)
}
pom.project {
name 'Facebook-Android-SDK'
artifactId = 'facebook-android-sdk'
packaging 'aar'
description 'Facebook Android SDK'
url 'https://github.com/facebook/facebook-android-sdk'
scm {
connection 'scm:git#github.com:facebook/facebook-android-sdk.git'
developerConnection 'scm:git#github.com:facebook/facebook-android-sdk.git'
url 'https://github.com/facebook/facebook-android-sdk'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'https://github.com/facebook/facebook-android-sdk/blob/master/LICENSE.txt'
distribution 'repo'
}
}
developers {
developer {
id 'facebook'
name 'Facebook'
}
}
}
}
}
uploadArchives.dependsOn(setVersion)
signing {
required { !isSnapshot && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
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.sourceFiles
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
afterEvaluate {
androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}
Please help....
Your issue: Cannot call getBootClasspath() before setTargetInfo() is called., I believe has been solved here:
http://tools.android.com/tech-docs/new-build-system
Android Build tools 1.1.0 created this problem and they fixed it in 1.1.1:
1.1.1 (2015/02/24)
Only variants that package a Wear app will now trigger building them.
Dependency related issues now fail at build time rather than at debug time.
This is to allow running diagnostic tasks (such as 'dependencies') to help resolve the conflict.
Calling android.getBootClasspath() is now possible again.
Please upgrade to: classpath 'com.android.tools.build:gradle:1.2.3', for example:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}