I am using Gradle to build an Android library project and deploy it to maven repository as an aar.
The library has some dependencies, which should be included in the POM
With apply plugin: 'maven'
no POM file is present, just the artifact
With apply plugin: 'maven-publish'
a POM file is generated, but it does not include any dependencies
Any ideas? Is this just not supported?
Gradle 2.2 and Android Gradle Plugin 1.1.0
First approach:
configurations {
archives {
extendsFrom configurations.default
}
}
afterEvaluate { project ->
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: "http://nexus-url") {
authentication(userName: nexusUsername, password: nexusPassword)
pom.groupId = 'com.example'
pom.version = '123-SNAPSHOT'
pom.artifactId = 'foo'
pom.packaging = 'aar'
pom.project {
artifactId = 'bar'
packaging 'aar'
description 'baz'
}
}
}
}
Also tried it without wrapping it in afterEvaluate
Second approach:
publishing {
publications {
sdk(MavenPublication) {
groupId 'com.example'
artifactId 'foo'
version = "0.123-SNAPSHOT"
artifact("$buildDir/outputs/aar/app-sdk-debug.aar")
}
}
repositories {
maven {
url "http://nexus-url"
credentials {
username 'foo'
password 'bar'
}
}
}
}
Update
The root cause of the problem is that this project uses flavors. Without flavors the pom is generated properly when using apply plugin: 'maven'
To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.
Add a Java Maven Dependency to the Utility Projectfield (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK. Expand the utility project, right-click the pom. xml file, and select Run As>Maven Install to install the file into the local repository.
Go to File > Project Structure in Android Studio. Select the app module in the Modules list on the left. Select the Dependencies tab. Click the + button on the lower left to add the dependency.
The dependencyResolutionManagement repositories block accepts the same notations as in a project, which includes Maven or Ivy repositories, with or without credentials, etc. By default, repositories declared by a project will override whatever is declared in settings.
This is the solution that worked for me in the end:
publishing {
publications {
sdk(MavenPublication) {
artifactId libName
artifact "${project.buildDir}/outputs/aar/${libName}-${project.version}.aar"
//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
// for dependencies and exclusions
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.withType(ModuleDependency) { ModuleDependency dp ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dp.group)
dependencyNode.appendNode('artifactId', dp.name)
dependencyNode.appendNode('version', dp.version)
// for exclusions
if (dp.excludeRules.size() > 0) {
def exclusions = dependencyNode.appendNode('exclusions')
dp.excludeRules.each { ExcludeRule ex ->
def exclusion = exclusions.appendNode('exclusion')
exclusion.appendNode('groupId', ex.group)
exclusion.appendNode('artifactId', ex.module)
}
}
}
}
}
}
repositories {
maven {
name 'myrepo'
url 'https://maven.foo.com'
credentials {
username mavenUsername
password mavenPassword
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With